Serial connection data not received completely

Good Day
Inside dataReceived event I write below code
While me.BytesAvailable > 0
s = me.ReadAll
DataBuffer = DataBuffer + s
me.Poll

Wend

But I cannot read all the data completely. It will fire 3 times for that single data which is send by my device
Attached my sample data for reference. can any one help me out?

2022-07-23 16:26:56 RECEIVE_DATA : {“GLGU”:“1|0|1658448000”}{“GLGU”:“1|1|0,0,0,DD”}{“GLGU”:“1|2|0,0,0,DD”}{“GLGU”:“1|3|0,0,0,DD”}{“GLGU”:“1|4|0,0,0,DD”}{“GLGU”:“1|5|0,0,0,DD”}{“GLGU”:“1|6|0,0,0,DD”}{“GLGU”:“1|7|0,0,0,DD”}{“GLGU”:“1|8|0,0,0,DD”}{“GLGU”:“1|9|0,0,0,DD”}{“GLGU”:“1|10|0,0,0,DD”}{“GLGU”:“1|11|0,0,0,DD”}{“GLGU”:“1|12|0,0,0,DD”}{“GLGU”:“1|13|0,0,0,DD”}{“GLGU”:“1|14|0,0,0,DD”}{“GLGU”:“1|15|0,0,0,DD”}{“GLGU”:“1|16|0,0,0,DD”}{“GLGU”:“1|17|0,0,0,DD”}{“GLGU”:“1|18|0,0,0,DD”}{“GLGU”:“1|19|0,0,0,DD”}{“GLGU”:“1|20|0,0,0,DD”}{“GL
2022-07-23 16:26:56 RECEIVE_DATA : GU”:“1|21|0,0,0,DD”}{“GLGU”:“1|22|0,0,0,DD”}{“GLGU”:“1|23|0,0,0,DD”}{“GLGU”:“1|24|0,0,0,DD”}{“GLGU”:“1|25|0,0,0,DD”}{“GLGU”:“1|26|0,0,0,DD”}{“GLGU”:“1|27|0,0,0,DD”}{“GLGU”:“1|28|0,0,0,DD”}{“GLGU”:“1|29|0,0,0,DD”}{“GLGU”:“1|30|0,0,0,DD”}{“GLGU”:“1|31|0,0,0,DD”}{“GLGU”:“1|32|0,0,0,DD”}{“GLGU”:“1|33|0,0,0,DD”}{“GLGU”:“1|34|0,0,0,DD”}{“GLGU”:“1|35|0,0,0,DD”}{“GLGU”:“1|36|0,0,0,DD”}{“GLGU”:“1|37|0,0,0,DD”}{“GLGU”:“1|38|0,0,0,DD”}{“GLGU”:“1|39|0,0,0,DD”}{“GLGU”:“1|40|0,0,0,DD”}{“GLGU”:“1|41|0,0,0,DD”}{“GLGU”:“1|42|0,0,0,DD”}{“GLGU”:“1|43|0,0,0,DD”}{“GLGU”:“1|44|0,0,0,DD”}{“GLGU”:“1|45|0,0,0,DD”}{“GLGU”:“1|46|0,0,0,DD”}{“GLGU”:“1|47|0,0,0,DD”}{“GLGU”:“1|48|0,0,0,DD”}{“GLGU”:“1|49|0,0,0,DD”}{“GLGU”:“1|50|0,0,0,DD”}{“GLGU”:“1|51|0,0,0,DD”}{“GLGU”:“1|52|0,0,0,DD”}{“GLGU”:“1|53|0,0,0,DD”}{“GLGU”:“1|54|0,0,0,DD”}{“GLGU”:“1|55|0,0,0,DD”}{“GLGU”:“1|56|0,0,0,DD”}{“GLGU”:“1|57|0,0,0,DD”}{“GLGU”:“1|58|0,0,0,DD”}{“GLGU”:“1|59|0,0,0,DD”}{“GLGU”:“1|60|0,0,0,DD”}{“GLGU”:“1|61|0,0,0,DD”}{“GLGU”:“1|62|0,0,0,DD”}{“GLGU”:“1|63|0,0,0,DD”}{“GLGU”:“1|64|0,0,0,DD”}
2022-07-23 16:26:57 RECEIVE_DATA : {“GLGU”:“1|65|0,0,0,DD”}{“GLGU”:“1|66|0,0,0,DD”}{“GLGU”:“1|67|0,0,0,DD”}{“GLGU”:“1|68|0,0,0,DD”}{“GLGU”:“1|69|0,0,0,DD”}{“GLGU”:“1|70|0,0,0,DD”}{“GLGU”:“1|71|0,0,0,DD”}{“GLGU”:“1|72|0,0,0,DD”}{“GLGU”:“1|73|0,0,0,DD”}{“GLGU”:“1|74|0,0,0,DD”}{“GLGU”:“1|75|0,0,0,DD”}{“GLGU”:“1|76|0,0,0,DD”}{“GLGU”:“1|77|0,0,0,DD”}{“GLGU”:“1|78|0,0,0,DD”}{“GLGU”:“1|79|0,0,0,DD”}{“GLGU”:“1|80|0,0,0,DD”}{“GLGU”:“1|81|0,0,0,DD”}{“GLGU”:“1|82|0,0,0,DD”}{“GLGU”:“1|83|0,0,0,DD”}{“GLGU”:“1|84|0,0,0,DD”}{“GLGU”:“1|85|0,0,0,DD”}{“GLGU”:“1|86|0,0,0,DD”}{“GLGU”:“1|87|0,0,0,DD”}{“GLGU”:“1|88|0,0,0,DD”}{“GLGU”:“1|89|0,0,0,DD”}{“GLGU”:“1|90|0,0,0,DD”}{“GLGU”:“1|91|0,0,0,DD”}{“GLGU”:“1|92|0,0,0,DD”}{“GLGU”:“1|93|0,0,0,DD”}{“GLGU”:“1|94|0,0,0,DD”}{“GLGU”:“1|95|0,0,0,DD”}{“GLGU”:“1|96|0,0,0,DD”}

Your While-Wend loop is monopolizing the processor, so the SerialConnection cannot function. Inside the DataReceived event you should only add received bytes to your buffer. You can set a timer (in the event or somewhere else) and parse the buffer data in the timer’s Action event.

1 Like

Never Poll in a DataReceived event. Bad things happen, as you have experienced.

2 Likes

You’re a better Reader of Original Posts than I, but I still stand by my advice :slight_smile:

It would appear that you have 97 chunks of data ({…}) that make up a packet. My take would be to read each chunk into an array and then process the array when the count reaches 97. So assuming DataBuffer is now a string array I would have this code in the DataReceived Event Handler (Coded Here)

Var Length As Integer = Me.LookAhead.IndexOf("}")

While Length > -1
  DataBuffer.Add(me.Read(Length))
  Length = Me.LookAhead.IndexOf("}")
Wend

You may have to adjust length to include the “}” character. What this does is read each chunk of your packet into the DataBuffer Array leaving any partial chunks in the buffer. To process the data I would add a timer that looked at the DataBuffer.Count to see if it is =>97 and then process the received data as required.

As mentioned earlier this is coded here & untested.

HTH
Wayne