L
Lou
I have a class that creates an instance of the seril Port.
Every thing works fine except whenever I receive data I cannot display the
recieved data. I get no errors but the recived data seems to just go no
where.
I can see the recived data in my serial receive function but when I either
raise an event
with it or try to display it in a text box nothing happens. I do use
beginInvoke on the text box.
If I trace the code through it all appears as if should work but the receive
data doesn't get displayed
and the callback never gets executed. Been wrestling with this one for days
now. It's very Wierd!
I have socket stuff in the same class and it's recieve callbacks work just
fine and displays just fine.
Public Function SendData(ByVal sData As String) As Boolean
Try
If SerialPort.IsOpen = False Then OpenSerialPort(mComPort)
' Write a line to the serial port
SerialPort.Write(Bytes, 0, (Bytes.Length - 1))
SendOutForDisplay(sData, True, mName)
RaiseEvent Rs232DataSent(sData)
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
End Function
Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As
SerialDataReceivedEventArgs)
Dim serial As SerialPort
Dim BytesAvailable As Integer
serial = CType(sender, SerialPort)
BytesAvailable = serial.BytesToRead
If BytesAvailable < 1 Then
Debug.Print("Serial Port read zero bytes...")
Exit Sub
End If
Dim bytes(256) As [Byte]
Dim retval As Integer = SerialPort.Read(bytes, 0, BytesAvailable)
Dim received As String = Encoding.Default.GetString(bytes)
RaiseEvent Rs232DataReceived(received)
SendOutForDisplay(received, False, mName)
End Sub
Private Sub SendOutForDisplay(ByVal sData As String, ByVal bSendOut As
Boolean, ByVal DeviceName As String)
frmMain.colDisplayData.Add(sData)
If frmMain.txtData.InvokeRequired Then
MessageBox.Show("Invoke required")
End If
frmMain.DisplayData(sData, bSendOut, DeviceName)
End Sub
Private Sub Device_Rs232DataReceived(ByVal sData As String) Handles
Device.Rs232DataReceived
If txtData.InvokeRequired Then
txtData.BeginInvoke(New txtDataControlDelegate(AddressOf DisplayData), New
Object() {sData, False, Device.Name})
Else
DisplayData(sData, False, Device.Name)
End If
End Sub
Public Sub DisplayData(ByVal Buffer As String, ByVal bSent As Boolean, ByVal
DeviceName As String)
Dim Whichway As String
If bSent = True Then
Whichway = "Sent: "
Else
Whichway = "Received: "
End If
Buffer = Buffer.Replace(vbCr, "")
Buffer = Buffer.Replace(vbLf, "")
If Len(txtData.Text) > 32000 Then txtData.Clear()
txtData.AppendText(vbCrLf & Now.ToLongTimeString & "->" & Whichway & "[" &
DeviceName & "]" & Buffer)
End Sub
Every thing works fine except whenever I receive data I cannot display the
recieved data. I get no errors but the recived data seems to just go no
where.
I can see the recived data in my serial receive function but when I either
raise an event
with it or try to display it in a text box nothing happens. I do use
beginInvoke on the text box.
If I trace the code through it all appears as if should work but the receive
data doesn't get displayed
and the callback never gets executed. Been wrestling with this one for days
now. It's very Wierd!
I have socket stuff in the same class and it's recieve callbacks work just
fine and displays just fine.
Public Function SendData(ByVal sData As String) As Boolean
Try
If SerialPort.IsOpen = False Then OpenSerialPort(mComPort)
' Write a line to the serial port
SerialPort.Write(Bytes, 0, (Bytes.Length - 1))
SendOutForDisplay(sData, True, mName)
RaiseEvent Rs232DataSent(sData)
Catch ex As System.Exception
MessageBox.Show(ex.Message)
End Try
End Function
Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As
SerialDataReceivedEventArgs)
Dim serial As SerialPort
Dim BytesAvailable As Integer
serial = CType(sender, SerialPort)
BytesAvailable = serial.BytesToRead
If BytesAvailable < 1 Then
Debug.Print("Serial Port read zero bytes...")
Exit Sub
End If
Dim bytes(256) As [Byte]
Dim retval As Integer = SerialPort.Read(bytes, 0, BytesAvailable)
Dim received As String = Encoding.Default.GetString(bytes)
RaiseEvent Rs232DataReceived(received)
SendOutForDisplay(received, False, mName)
End Sub
Private Sub SendOutForDisplay(ByVal sData As String, ByVal bSendOut As
Boolean, ByVal DeviceName As String)
frmMain.colDisplayData.Add(sData)
If frmMain.txtData.InvokeRequired Then
MessageBox.Show("Invoke required")
End If
frmMain.DisplayData(sData, bSendOut, DeviceName)
End Sub
Private Sub Device_Rs232DataReceived(ByVal sData As String) Handles
Device.Rs232DataReceived
If txtData.InvokeRequired Then
txtData.BeginInvoke(New txtDataControlDelegate(AddressOf DisplayData), New
Object() {sData, False, Device.Name})
Else
DisplayData(sData, False, Device.Name)
End If
End Sub
Public Sub DisplayData(ByVal Buffer As String, ByVal bSent As Boolean, ByVal
DeviceName As String)
Dim Whichway As String
If bSent = True Then
Whichway = "Sent: "
Else
Whichway = "Received: "
End If
Buffer = Buffer.Replace(vbCr, "")
Buffer = Buffer.Replace(vbLf, "")
If Len(txtData.Text) > 32000 Then txtData.Clear()
txtData.AppendText(vbCrLf & Now.ToLongTimeString & "->" & Whichway & "[" &
DeviceName & "]" & Buffer)
End Sub