How can i Recieve\Transmit data in binary mode using the RS232.vb class ?

Udi_Hakim

New member
Joined
Dec 3, 2003
Messages
3
Hey all of ya,

Well, i use the RS232.vb for the serial communication but the problem is that it handles the data in ASCII.

I searched in the RS232.vb and i came across 3 places where the ASCII is mentioned:



code:--------------------------------------------------------------------------------
This read-only property returns a string that represents
the data coming into to the Comm Port.
Overridable ReadOnly Property InputStreamString() As String
Get
Dim oEncoder As New System.Text.ASCIIEncoding()
Return oEncoder.GetString(Me.InputStream)
End Get
End Property
--------------------------------------------------------------------------------



code:--------------------------------------------------------------------------------
This subroutine uses another thread to write to the Comm Port. It
raises TxCompleted when done. It writes a string.
Public Overloads Sub AsyncWrite(ByVal Buffer As String)
Dim oEncoder As New System.Text.ASCIIEncoding()
Dim aByte() As Byte = oEncoder.GetBytes(Buffer)
Me.AsyncWrite(aByte)
End Sub
--------------------------------------------------------------------------------



code:--------------------------------------------------------------------------------
This subroutine writes the passed string to the
Comm Port to be written.
Public Overloads Sub Write(ByVal Buffer As String)
Dim oEncoder As New System.Text.ASCIIEncoding()
Dim aByte() As Byte = oEncoder.GetBytes(Buffer)
Me.Write(aByte)
End Sub
--------------------------------------------------------------------------------


how can i modify these codes to handle the data flow to and from the serial port in binary?
 
Hi,

have the same problem. I want to communicate with an 89C51 microcontroller but have to receive values from 0 to 255. When the value is higher than 127, then i get a wrong value. It was no problem with the mscomm control in vb6.

someone experience with the commport?

Seppe
 
You need to make your own Encoder using ChrW and AscW to correctly convert Bytes to Chars...this will correctly cast bytes greater then 127.


[VB]

homebrew byte string converting, VB.NET seems to only convert the first 128 ASCII characters
Public NotInheritable Class CByteCasting
Public Shared Function Encode8(ByVal arr() As Byte) As String
Dim i As Int32
With New StringBuilder(arr.Length())
For i = 0 To (arr.Length() - 1)
.Append(Encode8(arr(i)))
Next i
Return .ToString()
End With
End Function

Public Shared Function Encode8(ByVal str As String) As Byte()
Dim ret As Byte()
Dim i As Int32

ReDim ret(str.Length() - 1)

For i = 0 To (str.Length() - 1)
ret(i) = Encode8(str.Chars(i))
Next i
Return ret
End Function

Public Shared Function Encode8(ByVal value As Char) As Byte
Return Convert.ToByte(AscW(value))
End Function

Public Shared Function Encode8(ByVal value As Byte) As Char
Return ChrW(Convert.ToInt32(value))
End Function

Public Shared Function Encode8(ByVal value As Int16) As Char
Return ChrW(Convert.ToInt32(value And &HFFS))
End Function

Public Shared Function Encode8(ByVal value As Int32) As Char
Return ChrW(value And &HFF)
End Function
End Class
[/VB]
 
Back
Top