Serial Port Send or Receive data not available for display

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
 
P

PlatinumBay

Lou,

It may be possible that the bytestream contains zero value bytes, which are
not displayable in a string. You may want to check for zero bytes in the
byte stream and not try to display those.

I'll also take a look through some old serial port code I have (which works)
to check it against yours.

Hope this helps,


Steve

Lou said:
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
 
L

Lou

Thanks, I have been at this for days!
It looks like it should just work!

PlatinumBay said:
Lou,

It may be possible that the bytestream contains zero value bytes, which
are not displayable in a string. You may want to check for zero bytes in
the byte stream and not try to display those.

I'll also take a look through some old serial port code I have (which
works) to check it against yours.

Hope this helps,


Steve

Lou said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top