|By: Guest||Posted on: Nov 8 2016 at 12:01:15 AM|
Im receiving data from a caller id unit via serial connection. I never had issues with mscomm control so presumed code would work same way
retrieving data as below however it appears
Do While SComm1.InBufferCount > 0
stEvent = stEvent & SComm1.Input
this should essentially retrieve data and then store in the stevent. Issue what is happening is it thinks buffer has receved all data and then exits the loop.
data then is coming as separate strings, anyway to resolve this
|By: Guest||Posted on: Nov 10 2016 at 07:12:32 PM|
|When handling asynchronous data you have to assume that there will be gaps in the data stream (That's exactly what asynchronous means - the data is not synchronous) and that at any time your loop could exit.|
The fact it worked with MSComm32 is probably more by luck than design and, even with MSComm32, the behavior you've used could change based on many factors such as fast computer getting round the loop before the next full byte arrives.
I suspect SComm32 is just returning quicker than MSComm32 and your code is getting round the loop quicker. ie before the next full byte arrives. (Although in my experience Scomm32 is not quicker than MScomm32 so there's something else going on)
You need to rethink that loop. You really should be using the OnComm event to collect data as it arrives. But if you're determined to loop like that that then maybe just add a small delay in there before looping again.
|By: Guest||Posted on: Nov 17 2016 at 08:23:28 AM|
|I have taken out the loop and set inputlen as 0. Im using a caller id device with it. On the comm event on first call data returns with correct value. On second call the on com event fires twice instead of once like the first call. Is somehing being stored in the buffer. |
|By: Guest||Posted on: Nov 17 2016 at 09:48:56 PM|
|You said the oncomm event is firing twice. Do you get data both times? For example if you're expecting a string of data do you get part of the string in one OnComm event and then a moment later another oncomm event gets the rest of the string?|
|By: Guest||Posted on: Nov 17 2016 at 10:07:08 PM|
|yes, i get part of the string in one and part in the other, wheras on the first time the oncomm runs for example on call 1 all the data is returned on one comm event. its the calls after which then retrieves data in 2 parts|
|By: Guest||Posted on: Nov 18 2016 at 07:04:56 PM|
|Asynchronous means there is no guarantee of the space between bytes. It could be 0ms or it could be two days. You code has to accept that.|
Here's some psudo code.
Outside of your oncomm event, at the very top of your program define a string buffer to hold the received data.
Dim sBuffer As String
If scomm1.commevent = comevreceive then
While scomm1.inbuffercount > 0
sBuffer = sBuffer & scomm1.Input
''Now test sBuffer to see if it contains all of the data you expect.
' If sBuffer is not the correct length then Exit Sub. We'll come back on the next oncomm event.
' If you get here then assume sBuffer contains all of your data.
' handle sBuffer
' then clear sBuffer ready for the next packet.
sBuffer = ""
Doing it like that means your code will work if the data comes in one oncomm event or one hundred. Which mean it'll work no matter how slow or fast your serial port is.
Reply - add a comment to this topic.
You may enter letters, numbers and standard punctuation only. HTML and other scripts/tags will be rejected.