VB 2010: Eventhandler for serial port data receiving work only in debug mode
It's quite strange. The eventhandlers for my serial port seem to be
working only in debug mode. Problem is, at the hardware side, the RX and
TX pins are connected, which means they share the same data line. In this
case, there will be always 2 DataReceived events triggered after every
Send. The first one is the command itself, and the second is the answer
that I need. Here is the code part:
Private Sub mspSerialPort_DataReceived(ByVal sender As Object, _
ByVal e As
SerialDataReceivedEventArgs) _
Handles mspSerialPort.DataReceived
'handles serial data received events
Dim iNum As Integer = mspSerialPort.BytesToRead
UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)
If iNum > 0 Then
ReDim mbReceiveBuffer(iNum - 1)
mspSerialPort.Read(mbReceiveBuffer, 0, iNum)
If iNum > 1 Then
Me.Invoke(UpdateFormDelegate1)
End If
End If
End Sub
The If-condition "iNum > 1" is just intended to neglect the first received
byte, which is the command itself. Everything works fine in debug mode,
but once the program is run in normal mode, the received bytes in second
event won't be displayed. The UpdateDisplay function is shown below:
Private Sub UpdateDisplay()
'display received data
msReceiveBufferString = ""
For i = 0 To UBound(mbReceiveBuffer)
msReceiveBufferString &= (mbReceiveBuffer(i).ToString("X2") & " ")
Next
Select Case mbTestIDFlag
Case gbTEST_ID_02 'SW-ID
txtSWID.Text = msReceiveBufferString
Case gbTEST_ID_2F 'Trim value
txtTrim.Text = msReceiveBufferString
Case Else
'do nothing
End Select
End Sub
Does anyone have any ideas why that doesn't work? Is it because of the
delegate function, or the eventhandler, or sth else? Many thanks in
advance!
No comments:
Post a Comment