I have a problem with my code and i don't know how to process that.
I create an app for my society, i have to read serial data of an half-duplex object.
This object sends me a message every 1s which starts with HEX02 and ends with HEX03 (so in this form [02]DATA[03]), I must therefore recover this data to use it, but I also have to need to be able to send a command and receive a response, I manage to send but the interpretation of the response is complex with the message above.
Basically my command once sent returns me a result, generally it starts with "!", exemple :
-> 99AA
<- !RESPONSE
But sometimes it returns several lines, for example:
-> 99SS
<- !RESPONSE
<- RESPONSE2
..
<- RESPONSEX
And i have to get all response to recover data i use a function triggered by an event handler when SerialPort receive data :
public void SerialRead(object sender, SerialDataReceivedEventArgs s)
{
try
{
if (Serial.IsOpen)
{
byte[] buffer = new byte[Serial.BytesToRead];
Serial.Read(buffer, 0, buffer.Length);
string receivedData = Encoding.ASCII.GetString(buffer);
BeginInvoke(new Action(() =>
{
// Process DATA
}));
}
}
catch (Exception e)
{
MessageBox.Show(String.Format("Erreur : SRE01\n\nUne erreur est survenue lors de la lecture des données séries : {0}", e), "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
I gave u an hex dump of the response if this can help u
02 30 30 30 2e 31 20 31 37 30 20 2b 32 31 2e 35 .000.1 170 +21.5
20 46 46 46 2e 46 46 46 46 46 46 20 46 46 46 46 FFF.FFFFFF FFFF
2e 46 46 46 46 46 46 20 46 46 46 46 20 46 46 46 .FFFFFF FFFF FFF
2e 46 20 46 46 46 2e 46 20 31 34 2e 31 32 2e 32 .F FFF.F 14.12.2
33 20 31 36 3a 31 32 3a 30 30 20 2a 36 46 0d 03 3 16:12:00 *6F..
39 39 53 53 0d 99SS.
21 30 31 53 53 30 30 30 30 30 0d 0a 30 31 41 42 !01SS00000..01AB
38 30 31 30 36 0d 0a 30 31 41 44 30 30 30 30 35 80106..01AD00005
0d 0a 30 31 41 48 30 30 31 30 30 0d 0a 30 31 41 ..01AH00100..01A
4c 30 30 31 32 30 0d 0a 30 31 41 4d 30 30 30 30 L00120..01AM0000
31 0d 0a 30 31 41 4f 30 30 30 30 31 0d 0a 30 31 1..01AO00001..01
41 50 30 30 31 30 30 0d 0a 30 31 41 51 30 30 31 AP00100..01AQ001
30 30 0d 0a 30 31 41 52 30 30 30 36 30 0d 0a 30 00..01AR00060..0
31 41 54 31 34 30 36 30 0d 0a 30 31 41 56 30 30 1AT14060..01AV00
30 31 30 0d 0a 30 31 42 4f 30 30 30 30 30 0d 0a 010..01BO00000..
30 31 42 50 30 30 30 30 38 0d 0a 30 31 42 52 30 01BP00008..01BR0
30 30 39 36 0d 0a 30 31 42 54 30 30 30 30 30 0d 0096..01BT00000.
0a 30 31 43 49 30 30 30 30 30 0d 0a 30 31 44 4d .01CI00000..01DM
30 30 30 30 30 0d 0a 30 31 44 4f 30 30 30 30 31 00000..01DO00001
0d 0a 30 31 44 54 30 30 30 30 36 0d 0a 30 31 45 ..01DT00006..01E
49 30 30 30 30 30 0d 0a 30 31 45 54 30 30 30 33 I00000..01ET0003
30 0d 0a 30 31 48 43 30 30 30 31 30 0d 0a 30 31 0..01HC00010..01
48 48 30 30 30 30 30 0d 0a 30 31 48 50 30 30 30 HH00000..01HP000
31 30 0d 0a 30 31 48 53 30 30 30 30 31 0d 0a 30 10..01HS00001..0
31 48 54 30 30 30 30 31 0d 0a 30 31 49 44 30 30 1HT00001..01ID00
30 30 31 0d 0a 30 31 49 49 30 30 30 30 30 0d 0a 001..01II00000..
30 31 4b 59 30 34 37 31 31 0d 0a 30 31 4d 43 30 01KY04711..01MC0
30 30 30 30 0d 0a 30 31 4d 44 30 30 30 30 32 0d 0000..01MD00002.
0a 30 31 4e 43 30 30 30 30 30 0d 0a 30 31 4f 4c .01NC00000..01OL
31 32 37 38 39 0d 0a 30 31 4f 50 30 30 30 30 30 12789..01OP00000
0d 0a 30 31 4f 52 30 31 30 30 30 0d 0a 30 31 4f ..01OR01000..01O
53 30 30 30 30 30 0d 0a 30 31 50 45 30 30 30 31 S00000..01PE0001
36 0d 0a 30 31 50 48 30 30 32 36 30 0d 0a 30 31 6..01PH00260..01
50 4e 30 30 30 30 32 0d 0a 30 31 50 54 30 30 30 PN00002..01PT000
31 30 0d 0a 30 31 52 44 30 30 30 32 30 0d 0a 30 10..01RD00020..0
31 52 54 30 30 30 30 33 0d 0a 30 31 53 48 30 30 1RT00003..01SH00
30 30 30 0d 0a 30 31 53 4d 30 30 30 30 30 0d 0a 000..01SM00000..
30 31 53 4e 30 33 32 30 30 34 37 33 0d 0a 30 31 01SN03200473..01
53 54 30 30 30 30 31 0d 0a 30 31 53 56 30 30 34 ST00001..01SV004
31 36 0d 0a 30 31 54 41 39 32 30 30 30 30 30 30 16..01TA92000000
30 0d 0a 30 31 54 54 30 30 30 30 31 0d 0a 30 31 0..01TT00001..01
54 5a 30 30 30 30 30 0d 0a TZ00000..
I have good approaches to understanding but I'm wasting time on the subject and it has to be resolved, I know that if I look into it a lot I will succeed but I have a DeadLine. I also tried CHATGPT without any real success.