Comm32 Logo
Home Button   

Topic:   Missing data when receiving from RS232 - MSCOMM

By: crazydudePosted on: May 31 2013 at 04:33:35 AM
hi ,

I have a code as below. It receive data from PLC machine through rs232. This machine have 3 stations that will be sending out data once testing completed. all the 3 station will be tested simultaneously or 1 by 1.

It was working fine until I notice that recently i have missing data. Assume that i supposed to receive 100 files but receive only about 90-95% of it.
if i test 1 by 1 there is no problem in receiving it. Only happens when 2 or 3 stations send out together continuously.
Any idea how i can prevent from missing data when receiving ?





Private Sub MSComm1_OnComm()
Dim i As Long
Dim strInput As String
Dim strEnd As String
Dim strNow As String

''----------------------------------------------------------------
i = 0
strInput = ""
strNow = ""
Label7.Caption = ""
Label7.BackColor = vbYellow
strTerminalCharacter = vbCrLf
''----------------------------------------------------------------
Do
strInput = strInput & MSComm1.Input
DoEvents
i = i + 1
strEnd = Right(strInput, 2) 'Incase want to check Terminal Characters
Text3.Text = i
If MSComm1.PortOpen Then Shape1.BackColor = vbGreen Else Shape1.BackColor = vbRed
DoEvents
Loop Until (i > 3001) Or (strEnd = strTerminalCharacter)
''----------------------------------------------------------------

If (Len(strInput) >= nTotalLengthReceive) And (strEnd = strTerminalCharacter) Then
If (List1.ListCount >= nAutoSaveData) Then Call Command2_Click 'Auto save data after how many scan

Text1.Text = Left(strInput, nDataLength)

If Check1.Value = 1 Then
Clipboard.Clear
Clipboard.SetText (Label7.Caption)
End If
''----------------------------------------------------------------



By: Guest Posted on: May 31 2013 at 06:14:52 PM
3 PLC stations ? All 3 connected to the same serial port or do you have 3 different serial ports ?

You can not assume that the OnComm event means you have received some data. Other events can occur. You much check MSComm1.CommEvent to see if it is ev Receive and only call Input if it is.

Another thing I would suggest you never call DoEvents inside the OnComm event. This is allowing other OnComm events to occur and you have re-entrant code that is stealing data out of the port. I think that is the cause of your data loss.

Another thing I would suggest you don't loop 3000 times.

Another thing - why are you doing if MSComm1.PortOpen ? The port must be open otherwise you wouldn't be having an OnComm event.

Personally I think it's a can of worms that's impossible to Debug

Another thing - this is not a support forum for MSComm32.

By: Matt another guest.Posted on: May 31 2013 at 06:27:51 PM
Your problem is almost certainly caused by the looping doevents. As the previous 'guest' said, its allowing more ONComm events to trigger and you have more than one runni g at the same time and each is getting only partial data.

As the previous guy also said, this isnt a support forum for MSComm32 but if you were using this website's sComm32 component you would not have the same problems. Using sComm32 you can call doevents as often as you wish without causing other oncomm events to fire.