231 lines
6.2 KiB
C#
231 lines
6.2 KiB
C#
///
|
|
/// Copyright (c) 2013-2015 Sensus Metering Systems
|
|
///
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Text;
|
|
using log4net;
|
|
using TBF.UiBridge;
|
|
|
|
namespace TBF.BenchControl.Cameras.IdcCamera
|
|
{
|
|
///
|
|
/// File I/O implementation
|
|
///
|
|
public class SerialFileIO
|
|
{
|
|
static readonly ILog log = LogManager.GetLogger(typeof(SerialFileIO));
|
|
|
|
static TBF.Tools.ByteFormatter byteFormatter = new TBF.Tools.ByteFormatter();
|
|
|
|
const int BUFSIZE = 1024;
|
|
const int MAXFNAMESIZE = 62;
|
|
|
|
int cameraIdx;
|
|
string imagesPath;
|
|
|
|
StringBuilder fioFilenameBuilder;
|
|
string fioFullPathname;
|
|
int packetSize;
|
|
byte[] fioBuffer;
|
|
BinaryWriter fioWriter;
|
|
|
|
enum FioState
|
|
{
|
|
Command, /// 'o', 'c' or 'w'
|
|
Open, /// 'p'
|
|
Open2, /// 'e'
|
|
Open3, /// 'n'
|
|
Open4, /// 1
|
|
Filename,
|
|
Close, /// 'l'
|
|
Close2, /// 'o'
|
|
Close3, /// 's'
|
|
Close4, /// 'e'
|
|
Close5, /// 1
|
|
Write, /// 'r'
|
|
Write2, /// 'i'
|
|
Write3, /// 't'
|
|
Write4, /// 'e'
|
|
Write5, /// 1
|
|
PktSize1,
|
|
PktSize2,
|
|
PktSize3,
|
|
Data,
|
|
}
|
|
|
|
FioState state;
|
|
|
|
public string TransferredFile
|
|
{
|
|
get { return fioFullPathname; }
|
|
}
|
|
|
|
public SerialFileIO(int cameraIdx, string imagesPath)
|
|
{
|
|
this.cameraIdx = cameraIdx;
|
|
this.imagesPath = imagesPath;
|
|
state = FioState.Command;
|
|
fioFullPathname = string.Empty;
|
|
fioFilenameBuilder = new StringBuilder(16);
|
|
fioBuffer = new byte[BUFSIZE];
|
|
}
|
|
|
|
public void ProcessByte(byte b)
|
|
{
|
|
///
|
|
/// Reaching break in any case of the following switch is an error
|
|
///
|
|
if (state == FioState.Data)
|
|
{
|
|
if (packetSize > 0)
|
|
{
|
|
if (fioWriter != null)
|
|
{
|
|
fioWriter.Write(b);
|
|
}
|
|
if (--packetSize == 0)
|
|
{
|
|
state = FioState.Command;
|
|
}
|
|
return;
|
|
}
|
|
|
|
log.ErrorFormat("{0}: FIO error 17", cameraIdx);
|
|
}
|
|
else
|
|
{
|
|
switch (state)
|
|
{
|
|
case FioState.Command:
|
|
if ((char)b == 'o') { state = FioState.Open; return; }
|
|
if ((char)b == 'c') { state = FioState.Close; return; }
|
|
if ((char)b == 'w') { state = FioState.Write; return; }
|
|
log.ErrorFormat("{0}: FIO error 1, fullPathName={1}", cameraIdx, fioFullPathname);
|
|
break;
|
|
case FioState.Open:
|
|
if ((char)b == 'p') { state = FioState.Open2; return; }
|
|
log.ErrorFormat("{0}: FIO error 2", cameraIdx);
|
|
break;
|
|
case FioState.Open2:
|
|
if ((char)b == 'e') { state = FioState.Open3; return; }
|
|
log.ErrorFormat("{0}: FIO error 3", cameraIdx);
|
|
break;
|
|
case FioState.Open3:
|
|
if ((char)b == 'n') { state = FioState.Open4; return; }
|
|
log.ErrorFormat("{0}: FIO error 4", cameraIdx);
|
|
break;
|
|
case FioState.Open4:
|
|
if (b == 1)
|
|
{
|
|
fioFilenameBuilder.Clear();
|
|
state = FioState.Filename;
|
|
return;
|
|
}
|
|
log.ErrorFormat("{0}: FIO error 5", cameraIdx);
|
|
break;
|
|
case FioState.Filename:
|
|
if (b == 0)
|
|
{
|
|
if (fioWriter != null)
|
|
{
|
|
fioWriter.Close();
|
|
fioWriter.Dispose();
|
|
}
|
|
|
|
try
|
|
{
|
|
fioFullPathname = imagesPath + fioFilenameBuilder.ToString();
|
|
fioWriter = new BinaryWriter(File.Open(fioFullPathname, FileMode.Create));
|
|
log.DebugFormat("{0}: Open(\"{1}\")", cameraIdx, fioFullPathname);
|
|
}
|
|
catch
|
|
{
|
|
fioWriter = null;
|
|
log.WarnFormat("{0}: Failed to open(\"{1}\")", cameraIdx, fioFullPathname);
|
|
}
|
|
state = FioState.Command;
|
|
return;
|
|
}
|
|
else if (fioFilenameBuilder.Length < MAXFNAMESIZE)
|
|
{
|
|
fioFilenameBuilder.Append((char)b);
|
|
return;
|
|
}
|
|
log.ErrorFormat("{0}: FIO error 6", cameraIdx);
|
|
break;
|
|
case FioState.Close:
|
|
if ((char)b == 'l') { state = FioState.Close2; return; }
|
|
log.ErrorFormat("{0}: FIO error 7", cameraIdx);
|
|
break;
|
|
case FioState.Close2:
|
|
if ((char)b == 'o') { state = FioState.Close3; return; }
|
|
log.ErrorFormat("{0}: FIO error 8", cameraIdx);
|
|
break;
|
|
case FioState.Close3:
|
|
if ((char)b == 's') { state = FioState.Close4; return; }
|
|
log.ErrorFormat("{0}: FIO error 9", cameraIdx);
|
|
break;
|
|
case FioState.Close4:
|
|
if ((char)b == 'e') { state = FioState.Close5; return; }
|
|
log.ErrorFormat("{0}: FIO error 10", cameraIdx);
|
|
break;
|
|
case FioState.Close5:
|
|
if ((char)b == '1' && fioWriter != null)
|
|
{
|
|
log.DebugFormat("{0}: Close()", cameraIdx);
|
|
fioWriter.Close();
|
|
fioWriter.Dispose();
|
|
fioWriter = null;
|
|
Bridge.OnImageUpdated(this, new ImageUpdatedEventArgs(cameraIdx, fioFullPathname));
|
|
fioFullPathname = String.Empty;
|
|
state = FioState.Command;
|
|
return;
|
|
}
|
|
log.ErrorFormat("{0}: FIO error 11", cameraIdx);
|
|
break;
|
|
case FioState.Write:
|
|
if ((char)b == 'r') { state = FioState.Write2; return; }
|
|
log.ErrorFormat("{0}: FIO error 12", cameraIdx);
|
|
break;
|
|
case FioState.Write2:
|
|
if ((char)b == 'i') { state = FioState.Write3; return; }
|
|
log.ErrorFormat("{0}: FIO error 13", cameraIdx);
|
|
break;
|
|
case FioState.Write3:
|
|
if ((char)b == 't') { state = FioState.Write4; return; }
|
|
log.ErrorFormat("{0}: FIO error 14", cameraIdx);
|
|
break;
|
|
case FioState.Write4:
|
|
if ((char)b == 'e') { state = FioState.Write5; return; }
|
|
log.ErrorFormat("{0}: FIO error 15", cameraIdx);
|
|
break;
|
|
case FioState.Write5:
|
|
if ((char)b == '1') { state = FioState.PktSize1; return; }
|
|
log.ErrorFormat("{0}: FIO error 16", cameraIdx);
|
|
break;
|
|
case FioState.PktSize1:
|
|
packetSize = (int)b;
|
|
state = FioState.PktSize2;
|
|
return;
|
|
case FioState.PktSize2:
|
|
packetSize = (256 * packetSize) + (int)b;
|
|
state = FioState.PktSize3;
|
|
return;
|
|
case FioState.PktSize3:
|
|
packetSize = (256 * packetSize) + (int)b;
|
|
log.DebugFormat("{0}: write(len={1})", cameraIdx, packetSize);
|
|
state = FioState.Data;
|
|
return;
|
|
default:
|
|
log.ErrorFormat("{0}: FIO error 18", cameraIdx);
|
|
break;
|
|
}
|
|
}
|
|
|
|
state = FioState.Command;
|
|
}
|
|
}
|
|
}
|