SerialStream simulation : Reads characters from a file instead of a serial port.

This commit is contained in:
Milan Hanajik
2021-03-08 18:06:54 +01:00
parent e4c6bf5aa6
commit 6f701adb9e
@@ -4,6 +4,7 @@
using System;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
using log4net;
using Config.Entities;
using TBF.Rig.Generic;
@@ -171,7 +172,8 @@ namespace TBF.Rig.RegisterReaders.SerialStream
///
/// Opto serial port and worker thread related private variables
///
private SerialPort serialPort;
private SerialPort serialPort; /// Used in DebugMode.Normal
private TextReader textReader; /// Used instead of serialPort in DebugMode.Simulate
public SerialStream()
@@ -201,27 +203,27 @@ namespace TBF.Rig.RegisterReaders.SerialStream
{
ClearData();
if (DebugLevel == DebugMode.Normal)
datastreamParsingEnabled = false;
currentFrameFormat = new FrameFormat(FrameLength, FrameFrequency,
VolumeUnits, VolumeScaleFactor,
VolumeFieldStart, VolumeFieldEnd, VolumeFieldFormat,
TimeUnits, TimeScaleFactor,
TimeFieldStart, TimeFieldEnd, TimeFieldFormat);
log.WarnFormat("Initialize() ... FrameFormat set to {0}", currentFrameFormat);
/// Allocate memory for opto-data from iPerl
datastreamFrames = new DatastreamFrame[MaxDatastreamFramesCount];
for (int i = 0; i < MaxDatastreamFramesCount; i++) datastreamFrames[i] = new DatastreamFrame();
toBeFlushed = new DatastreamFrame();
flushedFramesCount = 0;
synchronized = false;
synchronized2 = false;
partOfTelegram = string.Empty;
if (DebugLevel == DebugMode.Normal)
{
datastreamParsingEnabled = false;
currentFrameFormat = new FrameFormat(FrameLength, FrameFrequency,
VolumeUnits, VolumeScaleFactor,
VolumeFieldStart, VolumeFieldEnd, VolumeFieldFormat,
TimeUnits, TimeScaleFactor,
TimeFieldStart, TimeFieldEnd, TimeFieldFormat);
log.WarnFormat("Initialize() ... FrameFormat set to {0}", currentFrameFormat);
/// Allocate memory for opto-data from iPerl
datastreamFrames = new DatastreamFrame[MaxDatastreamFramesCount];
for (int i = 0; i < MaxDatastreamFramesCount; i++) datastreamFrames[i] = new DatastreamFrame();
toBeFlushed = new DatastreamFrame();
flushedFramesCount = 0;
synchronized = false;
synchronized2 = false;
partOfTelegram = string.Empty;
/// Prepare serial port
serialPort = new SerialPort(string.Format("COM{0}", myCfg.ComPortNr),
myCfg.BaudRate,
@@ -232,11 +234,22 @@ namespace TBF.Rig.RegisterReaders.SerialStream
serialPort.Open();
}
else if (DebugLevel == DebugMode.Simulate)
{
try
{
textReader = new StreamReader("C:\\TBF\\Simulate\\serialstream.txt");
}
catch (Exception)
{
MessageBox.Show("Missing file C:\\TBF\\Simulate\\seriastream.txt");
}
}
}
public void RunDeviceBefore()
{
if (DebugLevel == DebugMode.Normal)
if (DebugLevel == DebugMode.Normal || DebugLevel == DebugMode.Simulate)
{
try
{
@@ -462,13 +475,23 @@ namespace TBF.Rig.RegisterReaders.SerialStream
/// <param name="streamState">OptoState.Read or OptoState.Flush</param>
void ReadDatastream(DatastreamState streamState, FrameFormat ff)
{
int nrBytes = serialPort.BytesToRead;
if (nrBytes > 0)
{
char[] buffer = new char[nrBytes];
serialPort.Read(buffer, 0, nrBytes);
string received = new string(buffer);
string received = null;
if (DebugLevel == DebugMode.Normal)
{
int nrBytes = serialPort.BytesToRead;
char[] buffer = new char[nrBytes];
serialPort.Read(buffer, 0, nrBytes);
received = new string(buffer);
}
else if (DebugLevel == DebugMode.Simulate)
{
string line = textReader.ReadLine();
received = line + '\r' + '\n';
}
if (received != null)
{
string allRcvd = partOfTelegram + received;
while (true)