I made a change to the original. The complete routine should look like this
Code:
Private Sub MSComm1_OnComm()
'
' Assumes RTHreshold Property is set to a value > 0
' (Recommended value is 1)
'
Static strBuffer As String
Dim strData As String
'Dim strRX As String
Dim boComplete As Boolean
Dim intPos As Integer
Select Case MSComm1.CommEvent
Case comEvReceive
strData = MSComm1.Input
strBuffer = strBuffer & strData
'
' Remove any / all ASC 231 characters from the buffer
'
Do
intPos = InStr(strBuffer, Chr(231))
If intPos > 0 Then
'
' Move the data up to but not including the ASC 231
'
If intPos <> 1 Then
strBuffer = Mid$(strBuffer, 1, intPos - 1)
'
' If there's data after it then move that as well
'
If intPos + 1 <= Len(strBuffer) Then
strBuffer = strBuffer & Mid$(strBuffer, intPos + 1)
End If
Else
strBuffer = Mid$(strBuffer, 2)
End If
End If
'
' Repeat until there's no ASC 231 in the Buffer
'
Loop Until intPos <= 0
Do
If Len(strBuffer) >= 13 Then
strRX = Mid$(strBuffer, 1, 13)
Text1.Text = strRX
MSComm1.InputLen = 0
'
' rest of your code goes here - strRX contains the 13 characters sent
' After you've done all your processing add the following code
'
If Len(strBuffer) = 13 Then
strBuffer = ""
boComplete = True
Else
strBuffer = Mid$(strBuffer, 14)
End If
Else
boComplete = True
End If
Loop Until boComplete = True
End Select
End Sub
If it's always an ASC 231 then it's most likely a 'problem' with the PLC code (if it was 'electrical noise' I'd expect it to be a random value). If it's any help, ASC 231 (Hex E7) is the character '7' (Hex 37) with the top bit set. There may be some 'untidy' PLC code which is not always assembling the characters correctly. (No offence to the PLC Programmer intended !)
EDIT:
I'm guessing that the PLC RS232 code is not running within an Interrupt routine so it could be possible that an Interrupt is being serviced by the PLC whilst it's assembling the characters to send. This could cause an issue if that Interrupt routine modifies something that the RS232 code doesn't expect to change. It could be a simple matter of disabling Interrupts within the PLC whilst the RS232 code is executing and then turning them back on when it's finished. (But if they're going to have to modify the PLC code, perhaps you could persuade them to stick a Carriage Return at the end of each message whilst they're at it - it would make your life so much simpler !)
Another possible problem could be the timing loop within the PLC. If it's 'creeping' it's possible that a bit could be misrepresented (ie it's sending a one instead of a zero. This could be caused if the start bit is too short)
You may like to add something at the end of the OnComm Event before the End Select - this will notify you if there's been a Framing Error on Receive.
Code:
Case comEventFrame
MsgBox "Framing Error"
End Select