Flushed opto-data indication in iPerlCommunicationsForm, version 1.5.119.

This commit is contained in:
Milan Hanajik 2015-09-21 16:29:12 +02:00
parent 8c796eaeee
commit b3dc49e8b7
5 changed files with 1885 additions and 1266 deletions

View File

@ -146,17 +146,19 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
buffer = new byte[lenght];
for (int i = 0; i < lenght; i++) buffer[i] = buf[i];
string name = string.Format("{0}({1})", wm.Name, wm.SerialNr);
///
if ((messageID == MessageID.Configuration) && (offset == 0) && (lenght == WaterMeters.iPerl.ConfigStruct.Length))
{
rfidDataLogger.InfoFormat("{0}, ReadRequestPort({1},...) returned {2} {3}", wm.Name, messageID, retv, (retv != 0) ? "!" : WaterMeters.iPerl.ConfigStruct.FromByteArray(buffer).ToString());
rfidDataLogger.InfoFormat("{0}: ReadRequestPort({1},...) returned {2} {3}", name, messageID, retv, (retv != 0) ? "!" : WaterMeters.iPerl.ConfigStruct.FromByteArray(buffer).ToString());
}
else if ((messageID == MessageID.Calibration) && (offset == 0) && (lenght == WaterMeters.iPerl.CalibrationStruct.Length))
{
rfidDataLogger.InfoFormat("{0}, ReadRequestPort({1},...) returned {2} {3}", wm.Name, messageID, retv, (retv != 0) ? "!" : WaterMeters.iPerl.CalibrationStruct.FromByteArray(buffer).ToString());
rfidDataLogger.InfoFormat("{0}: ReadRequestPort({1},...) returned {2} {3}", name, messageID, retv, (retv != 0) ? "!" : WaterMeters.iPerl.CalibrationStruct.FromByteArray(buffer).ToString());
}
else
{
rfidDataLogger.InfoFormat("{0}, ReadRequestPort({1}, {2}, {3}, ...) returned {4} {5}", wm.Name, messageID, offset, lenght, retv, (retv != 0) ? "!" : "");
rfidDataLogger.InfoFormat("{0}: ReadRequestPort({1}, {2}, {3}, ...) returned {4} {5}", name, messageID, offset, lenght, retv, (retv != 0) ? "!" : "");
}
}
@ -199,25 +201,27 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
retv = 0;
}
if (lenght == 1)
string name = string.Format("{0}({1})", wm.Name, wm.SerialNr);
///
if (lenght == 1)
{
rfidDataLogger.InfoFormat("{0}, WriteRequestPort({2}, {3}, {4}, {5}) returned {1} {6}", wm.Name, retv, messageID, offset, lenght, pBuf[0].ToString("X2"), (retv != 0) ? "!" : "");
rfidDataLogger.InfoFormat("{0}: WriteRequestPort({2}, {3}, {4}, {5}) returned {1} {6}", name, retv, messageID, offset, lenght, pBuf[0].ToString("X2"), (retv != 0) ? "!" : "");
}
else if (lenght == 2)
{
if (messageID == MessageID.Calibration && offset == 2)
{
UInt16 calibFactor = (UInt16)(pBuf[0] + 256 * pBuf[1]);
rfidDataLogger.InfoFormat("{0}, WriteRequestPort(calibFactor={1}) returned {2} {3}", wm.Name, calibFactor, retv, (retv != 0) ? "!" : "");
rfidDataLogger.InfoFormat("{0}: WriteRequestPort(calibFactor={1}) returned {2} {3}", name, calibFactor, retv, (retv != 0) ? "!" : "");
}
else
{
rfidDataLogger.InfoFormat("{0}, WriteRequestPort({2}, {3}, {4}, {5} {6}) returned {1} {7}", wm.Name, retv, messageID, offset, lenght, pBuf[0].ToString("X2"), pBuf[1].ToString("X2"), (retv != 0) ? "!" : "");
rfidDataLogger.InfoFormat("{0}: WriteRequestPort({2}, {3}, {4}, {5} {6}) returned {1} {7}", name, retv, messageID, offset, lenght, pBuf[0].ToString("X2"), pBuf[1].ToString("X2"), (retv != 0) ? "!" : "");
}
}
else
{
rfidDataLogger.InfoFormat("{0}, WriteRequestPort({2}, {3}, {4}, {5} {6} ...) returned {1} {7}", wm.Name, retv, messageID, offset, lenght, pBuf[0].ToString("X2"), pBuf[1].ToString("X2"), (retv != 0) ? "!" : "");
rfidDataLogger.InfoFormat("{0}: WriteRequestPort({2}, {3}, {4}, {5} {6} ...) returned {1} {7}", name, retv, messageID, offset, lenght, pBuf[0].ToString("X2"), pBuf[1].ToString("X2"), (retv != 0) ? "!" : "");
}
}
return retv;
@ -245,6 +249,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
int textBoxesCount;
Label[] labels;
TextBox[] counters;
TextBox[] messages;
static IList<WaterMeters.iPerl.WaterMeter> waterMeters;
@ -289,6 +294,19 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
wmLabel41, wmLabel42, wmLabel43, wmLabel44, wmLabel45,
wmLabel46, wmLabel47, wmLabel48,
};
counters = new TextBox[48]
{
textBox1, textBox2, textBox3, textBox4, textBox5,
textBox6, textBox7, textBox8, textBox9, textBox10,
textBox11, textBox12, textBox13, textBox14, textBox15,
textBox16, textBox17, textBox18, textBox19, textBox20,
textBox21, textBox22, textBox23, textBox24, textBox25,
textBox26, textBox27, textBox28, textBox29, textBox30,
textBox31, textBox32, textBox33, textBox34, textBox35,
textBox36, textBox37, textBox38, textBox39, textBox40,
textBox41, textBox42, textBox43, textBox44, textBox45,
textBox46, textBox47, textBox48,
};
messages = new TextBox[48]
{
wmTextBox1, wmTextBox2, wmTextBox3, wmTextBox4, wmTextBox5,
@ -438,6 +456,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
for (int i = 0; i < lineSize; i++)
{
labels[dest] = labels[l * lineSize + l * gap + i];
counters[dest] = counters[l * lineSize + l * gap + i];
messages[dest] = messages[l * lineSize + l * gap + i];
dest++;
}
@ -471,7 +490,7 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
//}
//else
{
labels[i].Visible = messages[i].Visible = true;
labels[i].Visible = counters[i].Visible = messages[i].Visible = true;
messages[i].Text = "---";
}
}
@ -480,6 +499,13 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
Left = (ls.iPerlCommunicationsFormLeft != 0) ? ls.iPerlCommunicationsFormLeft : 150;
Top = (ls.iPerlCommunicationsFormTop != 0) ? ls.iPerlCommunicationsFormTop : 150;
/// Reset opto-data indication
for (int i = 0; i < waterMeters.Count; i++)
{
waterMeters[i].FlushedDataCount = 0;
counters[i].Text = "0";
}
///
/// Start worker threads, etc.
///
@ -1051,6 +1077,11 @@ namespace TBF.BenchControl.TestMethods.iPerlCommunication
{
if (data.WMNr >= 0) messages[data.WMNr].Text = data.CommMessage;
for (int i = 0; i < waterMeters.Count; i++)
{
counters[i].Text = (waterMeters[i].FlushedDataCount % 1000).ToString();
}
lock (this)
{
if (++completedCommCount < rfidPortNrs.Count) return;

View File

@ -100,4 +100,10 @@ namespace TBF.BenchControl.WaterMeters.iPerl
InvalidTelegram, /// Wrong telegram format of checksum error
SyncError,
}
public enum OptoState
{
Read,
Flush,
}
}

View File

@ -228,6 +228,16 @@ namespace TBF.BenchControl.WaterMeters.iPerl
int optoDataCount;
string optoDataLogFileName;
OptoTelegramRaw toBeFlushed;
int flushedDataCount;
public int FlushedDataCount
{
get { return flushedDataCount; }
set { flushedDataCount = value; }
}
///
/// Opto serial port and worker thread related private variables
///
@ -238,12 +248,6 @@ namespace TBF.BenchControl.WaterMeters.iPerl
{
disabled = false;
commFailed = false;
synchronized = false;
synchronized2 = false;
partOfTelegram = string.Empty;
flushBuffer = new char[8192];
}
public WaterMeter(WaterMeterCfg cfg)
@ -251,7 +255,7 @@ namespace TBF.BenchControl.WaterMeters.iPerl
{
disabled = false;
commFailed = false;
iPerlCfg = cfg;
log.Debug(this.ToString());
}
@ -272,21 +276,25 @@ namespace TBF.BenchControl.WaterMeters.iPerl
{
if (DebugLevel == Entities.DebugMode.Normal)
{
//int nr = NrFormName(Name);
//optoDataLogger = LogManager.GetLogger("OptoData_" + nr.ToString());
//optoDataLogger.Fatal("------------------------------------------------------------------------");
//optoDataLogger.Fatal("Program restarted");
optoSerialPortParsingEnabled = false;
/// Allocate memory for opto-data from iPerl
optoData = new OptoTelegramRaw[MaxOptoDataCount];
for (int i = 0; i < MaxOptoDataCount; i++) optoData[i] = new OptoTelegramRaw();
toBeFlushed = new OptoTelegramRaw();
flushedDataCount = 0;
synchronized = false;
synchronized2 = false;
partOfTelegram = string.Empty;
/// Prepare serial port
optoSerialPort = new SerialPort(string.Format("COM{0}", iPerlCfg.OptoComPortNr),
9600, Parity.None, 8, StopBits.One);
optoSerialPort.Handshake = Handshake.None;
OpenSerialPort();
optoSerialPort.Open();
}
}
@ -297,9 +305,9 @@ namespace TBF.BenchControl.WaterMeters.iPerl
try
{
if (optoSerialPortParsingEnabled)
ReadOptoSerialPort();
ReadOptoSerialPort(OptoState.Read);
else
FlushOptoSerialPort();
ReadOptoSerialPort(OptoState.Flush);
}
catch (Exception e)
{
@ -324,9 +332,9 @@ namespace TBF.BenchControl.WaterMeters.iPerl
public void StopDevice()
{
if (DebugLevel == Entities.DebugMode.Normal)
if (DebugLevel == Entities.DebugMode.Normal && optoSerialPort != null)
{
CloseSerialPort();
optoSerialPort.Close();
}
}
@ -417,9 +425,8 @@ namespace TBF.BenchControl.WaterMeters.iPerl
timestampSec = 0;
timestampSec0 = 0;
CloseSerialPort();
OpenSerialPort();
ReadPulses();
ReadPulses();
StartParsingOptoSerialPort();
}
/// <summary>Run this operation</summary>
@ -460,6 +467,7 @@ namespace TBF.BenchControl.WaterMeters.iPerl
/// <summary>Stop this operation</summary>
public void Stop()
{
StopParsingOptoSerialPort();
SaveOptoData();
}
@ -482,36 +490,10 @@ namespace TBF.BenchControl.WaterMeters.iPerl
bool optoSerialPortParsingEnabled;
bool synchronized;
bool synchronized2;
string partOfTelegram;
/// <summary> Open RFID serial port and start parsing serial data </summary>
private void OpenSerialPort()
{
if (optoSerialPort != null)
{
optoSerialPort.Open();
StartParsingOptoSerialPort();
}
}
/// <summary> Stop parsing serial data and close RFID serial port </summary>
private void CloseSerialPort()
{
if (optoSerialPort != null)
{
StopParsingOptoSerialPort();
optoSerialPort.Close();
}
}
/// <summary> Flush internal buffers and start parsing the opto serial port data </summary>
void StartParsingOptoSerialPort()
{
synchronized = false;
synchronized2 = false;
partOfTelegram = string.Empty;
optoSerialPortParsingEnabled = true;
}
@ -521,15 +503,12 @@ namespace TBF.BenchControl.WaterMeters.iPerl
optoSerialPortParsingEnabled = false;
}
char[] flushBuffer;
void FlushOptoSerialPort()
{
int nrBytes = optoSerialPort.BytesToRead;
if (nrBytes > 0) optoSerialPort.Read(flushBuffer, 0, nrBytes);
}
///
/// Variables storing the context of serial port data parsing (ReadOptoSerialPort(...))
///
bool synchronized;
bool synchronized2;
string partOfTelegram;
/// <summary>
/// 9600 Bd, 8 data bits, 1 stop bit, no parity
@ -543,7 +522,8 @@ namespace TBF.BenchControl.WaterMeters.iPerl
/// FFDD3A 51F9 0000 65324E 0088 F631A60B 45
/// ...
/// </summary>
void ReadOptoSerialPort()
/// <param name="optoState">OptoState.Read or OptoState.Flush</param>
void ReadOptoSerialPort(OptoState optoState)
{
int nrBytes = optoSerialPort.BytesToRead;
if (nrBytes > 0)
@ -564,28 +544,54 @@ namespace TBF.BenchControl.WaterMeters.iPerl
partOfTelegram = allRcvd;
return;
}
// CR+LF found
else if (pos < OptoTelegram.Length - 2)
{
/// CR+LF found too early, truncate the beginning incl CR+LF and keep scanning in this loop
allRcvd = allRcvd.Substring(pos + 2);
if (synchronized)
{
optoData[optoDataCount++].SetFlags(OptoTelegramFlags.SyncError);
}
synchronized = true;
}
// CR+LF found and (pos >= OptoTelegram.Length - 2)
else if (optoData[optoDataCount].UpdateFromString(allRcvd.Substring(pos - OptoTelegramRaw.Length + 2)))
{
OptoTelegramRreceived(optoData[optoDataCount++], synchronized2);
synchronized2 = synchronized;
allRcvd = allRcvd.Substring(pos + 2);
}
else
{
optoData[optoDataCount++].SetFlags(OptoTelegramFlags.InvalidTelegram);
allRcvd = allRcvd.Substring(pos + 2);
// CR+LF found
if (optoState == OptoState.Read)
{
if (pos < OptoTelegram.Length - 2)
{
/// CR+LF found too early, truncate the beginning incl CR+LF and keep scanning in this loop
allRcvd = allRcvd.Substring(pos + 2);
if (synchronized)
{
optoData[optoDataCount++].SetFlags(OptoTelegramFlags.SyncError);
}
synchronized = true;
}
// CR+LF found and (pos >= OptoTelegram.Length - 2)
else if (optoData[optoDataCount].UpdateFromString(allRcvd.Substring(pos - OptoTelegramRaw.Length + 2)))
{
OptoTelegramRreceived(optoData[optoDataCount++], synchronized2);
synchronized2 = synchronized;
allRcvd = allRcvd.Substring(pos + 2);
}
else
{
optoData[optoDataCount++].SetFlags(OptoTelegramFlags.InvalidTelegram);
allRcvd = allRcvd.Substring(pos + 2);
}
}
else /// optoState == OptoState.Flush
{
if (pos < OptoTelegram.Length - 2)
{
/// CR+LF found too early, truncate the beginning incl CR+LF and keep scanning in this loop
allRcvd = allRcvd.Substring(pos + 2);
synchronized = true;
}
// CR+LF found and (pos >= OptoTelegram.Length - 2)
else if (toBeFlushed.UpdateFromString(allRcvd.Substring(pos - OptoTelegramRaw.Length + 2)))
{
flushedDataCount++;
synchronized2 = synchronized;
allRcvd = allRcvd.Substring(pos + 2);
}
else
{
allRcvd = allRcvd.Substring(pos + 2);
}
}
}
}

View File

@ -29,5 +29,5 @@ using System.Runtime.InteropServices;
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.5.116.1")]
[assembly: AssemblyFileVersion("1.5.116.1")]
[assembly: AssemblyVersion("1.5.119.1")]
[assembly: AssemblyFileVersion("1.5.119.1")]