By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,445 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

Serial Port Send or Receive data not available for display

P: n/a
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
Jun 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <lo********@comcast.netwrote in message
news:e7**************@TK2MSFTNGP02.phx.gbl...
>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


Jun 9 '07 #2

P: n/a
Lou
Thanks, I have been at this for days!
It looks like it should just work!

"PlatinumBay" <st*******@community.nospamwrote in message
news:O3****************@TK2MSFTNGP04.phx.gbl...
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" <lo********@comcast.netwrote in message
news:e7**************@TK2MSFTNGP02.phx.gbl...
>>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



Jun 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.