D
davewilliamson55555
I have an existing VB.NET 2003 app that uses the vb6 mscomm32.ocx
control to interface with the serial port on the desktop. I can't seem
to figure out how to code the compactframework 2 VB.NET 2005 app
running on Windows Mobile 2005 to talk reliably to the desktop.
What I am finding is that when the message "NEXT:{255}" (where {255} is
actually the character 255) is sent to the Windows Mobile device the
Mobile Device is not always seeing the message. I have confirmed using
SysInternals PortMon that the data is being sent by the PC app to the
port. And the app will get the message a random number of times
successfully. The cycle is the Windows Mobile app sends a data record
to the desktop and the desktop replies with the NEXT when it is ready
for another data record. However, after 15 or 20 cycles the Windows
Mobile app acts as if nothing came across the wire. Sometimes it runs
for 400 to 500 cycles before getting the condition. Sometimes it never
reads the 1st NEXT coming back.
My guess is that depending on what character(s) happen to be in the
inbuffer on the Windows Mobile port at the time of reading is the
problem ... I suspect that reading just a nonprintable character
somehow the serialport control throws it out before passing it along to
it's stream. By adding some junk text to the NEXT message
"NEXT:{1}this is junk to fill space{255}" the cycles ran without fail
up to 3000 cycles. My guess is the longer data meant less of a chance
that only the character 255 would be in the inbuffer for a read.
Can anyone help me get this new serialport control talking to the old
one?
The desktop app sets up the port this way:
With MSComm1
.CommPort = CType(portNum, Short)
.Handshaking = MSCommLib.HandshakeConstants.comRTS
.InBufferSize = 4096
'.InputLen = 0
.InputMode = MSCommLib.InputModeConstants.comInputModeText
.OutBufferSize = 4096
.RThreshold = 1
.Settings = "57600,N,8,1"
.PortOpen
End With
The desktop app reads data off the port this way:
Private Sub MSComm1_OnComm1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MSComm1.OnComm
Try
Dim dataBuffer As System.String = CType(MSComm1.Input,
System.String)
comData &= dataBuffer
Catch ex As Exception
Call LogException(ex)
MsgBox(ex.Message)
End Try
End Sub
The desktop app writes data to the port this way (where term is the
character 255):
MSComm1.Output = HELLO & term
The Windows Mobile app sets up the port this way:
With SerialPort1
.PortName = "COM1" 'windows mobile 5
.Handshake = IO.Ports.Handshake.RequestToSend
.Encoding = System.Text.Encoding.ASCII
.ReadTimeout = 500
.WriteTimeout = 500
.ReadBufferSize = 4096
.WriteBufferSize = 4096
.ReceivedBytesThreshold = 1
.BaudRate = 57600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.DiscardNull = True
.Open()
End With
The Windows Mobile app reads data from the port this way (note comdata
is a stringbuilder):
Private Sub ReadFromPort()
Try
Dim b(SerialPort1.ReadBufferSize) As System.Byte
Dim numOfBytesRead As System.Int32 = SerialPort1.Read(b, 0,
b.Length)
If numOfBytesRead > 0 Then
comData.Append(BytesToString(b))
End If
Catch
End Try
End Sub
Private Function BytesToString(ByVal inBytes() As System.Byte) As
System.String
Dim s As System.String = ""
Try
Dim sa As System.Text.StringBuilder = New
System.Text.StringBuilder(inBytes.Length)
Dim i As System.Int32 = 0
For Each b As System.Byte In inBytes
If inBytes(i) > 0 Then 'skip null chars
sa.Append(Chr(inBytes(i)))
End If
i += 1
Next
s = sa.ToString
Catch ex As Exception
MsgBox("BytesToString - " & ex.Message)
End Try
Return s
End Function
The Windows Mobile app writes data to the port this way:
Private Sub WriteToPort(ByVal dataString As System.String)
Try
Dim b() As System.Byte = ToBytes(dataString)
SerialPort1.Write(b, 0, b.Length)
Catch
End Try
End Sub
Private Function ToBytes(ByVal inString As System.String) As Byte()
Dim b() As System.Byte = Nothing
Try
ReDim b(inString.Length - 1)
Dim ca() As System.Char = inString.ToCharArray
Dim i As System.Int32 = 0
For Each c As System.Char In ca
b(i) = Asc(c)
i += 1
Next
Catch ex As Exception
MsgBox("ToBytes - " & ex.Message)
End Try
Return b
End Function
control to interface with the serial port on the desktop. I can't seem
to figure out how to code the compactframework 2 VB.NET 2005 app
running on Windows Mobile 2005 to talk reliably to the desktop.
What I am finding is that when the message "NEXT:{255}" (where {255} is
actually the character 255) is sent to the Windows Mobile device the
Mobile Device is not always seeing the message. I have confirmed using
SysInternals PortMon that the data is being sent by the PC app to the
port. And the app will get the message a random number of times
successfully. The cycle is the Windows Mobile app sends a data record
to the desktop and the desktop replies with the NEXT when it is ready
for another data record. However, after 15 or 20 cycles the Windows
Mobile app acts as if nothing came across the wire. Sometimes it runs
for 400 to 500 cycles before getting the condition. Sometimes it never
reads the 1st NEXT coming back.
My guess is that depending on what character(s) happen to be in the
inbuffer on the Windows Mobile port at the time of reading is the
problem ... I suspect that reading just a nonprintable character
somehow the serialport control throws it out before passing it along to
it's stream. By adding some junk text to the NEXT message
"NEXT:{1}this is junk to fill space{255}" the cycles ran without fail
up to 3000 cycles. My guess is the longer data meant less of a chance
that only the character 255 would be in the inbuffer for a read.
Can anyone help me get this new serialport control talking to the old
one?
The desktop app sets up the port this way:
With MSComm1
.CommPort = CType(portNum, Short)
.Handshaking = MSCommLib.HandshakeConstants.comRTS
.InBufferSize = 4096
'.InputLen = 0
.InputMode = MSCommLib.InputModeConstants.comInputModeText
.OutBufferSize = 4096
.RThreshold = 1
.Settings = "57600,N,8,1"
.PortOpen
End With
The desktop app reads data off the port this way:
Private Sub MSComm1_OnComm1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MSComm1.OnComm
Try
Dim dataBuffer As System.String = CType(MSComm1.Input,
System.String)
comData &= dataBuffer
Catch ex As Exception
Call LogException(ex)
MsgBox(ex.Message)
End Try
End Sub
The desktop app writes data to the port this way (where term is the
character 255):
MSComm1.Output = HELLO & term
The Windows Mobile app sets up the port this way:
With SerialPort1
.PortName = "COM1" 'windows mobile 5
.Handshake = IO.Ports.Handshake.RequestToSend
.Encoding = System.Text.Encoding.ASCII
.ReadTimeout = 500
.WriteTimeout = 500
.ReadBufferSize = 4096
.WriteBufferSize = 4096
.ReceivedBytesThreshold = 1
.BaudRate = 57600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.DiscardNull = True
.Open()
End With
The Windows Mobile app reads data from the port this way (note comdata
is a stringbuilder):
Private Sub ReadFromPort()
Try
Dim b(SerialPort1.ReadBufferSize) As System.Byte
Dim numOfBytesRead As System.Int32 = SerialPort1.Read(b, 0,
b.Length)
If numOfBytesRead > 0 Then
comData.Append(BytesToString(b))
End If
Catch
End Try
End Sub
Private Function BytesToString(ByVal inBytes() As System.Byte) As
System.String
Dim s As System.String = ""
Try
Dim sa As System.Text.StringBuilder = New
System.Text.StringBuilder(inBytes.Length)
Dim i As System.Int32 = 0
For Each b As System.Byte In inBytes
If inBytes(i) > 0 Then 'skip null chars
sa.Append(Chr(inBytes(i)))
End If
i += 1
Next
s = sa.ToString
Catch ex As Exception
MsgBox("BytesToString - " & ex.Message)
End Try
Return s
End Function
The Windows Mobile app writes data to the port this way:
Private Sub WriteToPort(ByVal dataString As System.String)
Try
Dim b() As System.Byte = ToBytes(dataString)
SerialPort1.Write(b, 0, b.Length)
Catch
End Try
End Sub
Private Function ToBytes(ByVal inString As System.String) As Byte()
Dim b() As System.Byte = Nothing
Try
ReDim b(inString.Length - 1)
Dim ca() As System.Char = inString.ToCharArray
Dim i As System.Int32 = 0
For Each c As System.Char In ca
b(i) = Asc(c)
i += 1
Next
Catch ex As Exception
MsgBox("ToBytes - " & ex.Message)
End Try
Return b
End Function