Добрый день форумчане программисты. Нужна ваша помощь.
Устройство подключено к компьютеру через com порт. Данные получаю, с этим проблем нету.
Код читается этим блоком.
#region comPort_DataReceived
/// <summary>
/// method that will be called when theres data waiting in the buffer
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
//determine the mode the user selected (binary/string)
switch (CurrentTransmissionType)
{
//user chose string
case TransmissionType.Text:
//read data waiting in the buffer
string msg = comPort.ReadExisting();
//display the data to the user
DisplayData(MessageType.Incoming, msg + "\n");
break;
//user chose binary
case TransmissionType.Hex:
//retrieve number of bytes in the buffer
int bytes = comPort.BytesToRead;
//create a byte array to hold the awaiting data
byte[] comBuffer = new byte[bytes];
//read the data and store it
comPort.Read(comBuffer, 0, bytes);
//display the data to the user
DisplayData(MessageType.Incoming, ByteToHex(comBuffer) + "\n");
break;
default:
//read data waiting in the buffer
string str = comPort.ReadExisting();
//display the data to the user
DisplayData(MessageType.Incoming, str + "\n");
break;
}
}
#endregion
А именно этой строкой
//read data waiting in the buffer
string msg = comPort.ReadExisting();
//display the data to the user
DisplayData(MessageType.Incoming, msg + "\n");
Вот как выглядит одно целое сообщение отправляемое устройством
QDSNO 40
DATE 20170626
TIME 113900
DOC
OPID 0
MODE 32
WRN 0
PM1 11
PM2 131
RM1 33
WM1 19
WM2 41
WM3 65
PART 5
EM1 41
BM1 59
PARN 29
P01 5,58 0
P02 4,90 0
P03 129 0
P04 37,31 0
P05 76 0
P06 26,2 2
P07 345 0
{!!!!!}
P08 244 0
P09 0,17 0
P10 6,9 2
P11 8,0 0
P12 35,2 0
P13 44,5 1
P14 14,1 0
P15 3,11 0
P16 0,47 0
P17 1,85 2
P18 55,8 1
P19 8,
{!!!!!}
5 1
P20 33,1 2
P21 8,0 0
P22 5,0 2
P24 0,08 0
P25 0,06 0
P26 1,5 0
P27 1,1 0
P28 4900 0
P29 4900 0
AGE 0
Там где видите знак {!!!!!} означает что буфер чтения заполнился и данные считались в переменную. Это произошло в три раза, то есть одно сообщение считалось тремя блоками. А если больше одного сообщения отправит устройство, то в буфер могут попасть конец одного сообщения и начала другого сообщения.
Как мне правильно распарсить данные, найти нужный момент для этого? Ведь нужное мне сообщение разделилось в 3 три блока, а размер сообщения пока неизвестно.
Любые идеи, размышления будут полезны. У меня самого опыта работы с com портами нету. Но работу надо сделать.