Send/receive data to Serial Port in binary mode

Aug 19 2010 6:49 AM

I have successfully sent and received data in text mode in vb.net 2005. How do I send/receive data in binary mode ?
Given below is the code I have written for text mode :
**********************************************
Public
Class Form1
Dim WithEvents serialPort As New IO.Ports.SerialPort
 
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1
cbbComPorts.Items.Add(
My.Computer.Ports.SerialPortNames(i))
Next
btnDisconnect.Enabled =
False
End Sub
Private Sub btnConnect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConnect.Click
If serialPort.IsOpen() Then
serialPort.Close()
End If
Try
With serialPort
.PortName = cbbComPorts.Text
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
serialPort.Open()
lblMessage.Text = cbbComPorts.Text &
" connected."
btnConnect.Enabled =
False
btnDisconnect.Enabled =
True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub btnDisconnect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
Try
serialPort.Close()
lblMessage.Text = cbbComPorts.Text &
" disconnected"
btnConnect.Enabled =
True
btnDisconnect.Enabled =
False
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
Try
serialPort.Write(txtDataToSend.Text & vbCrLf)
With txtDataReceived
.SelectionColor = Color.Black
.AppendText(txtDataToSend.Text & vbCrLf)
.ScrollToCaret()
End With
txtDataToSend.Text =
String.Empty
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Delegate Sub MyDelegate()
Public Sub UpdateTextBox()
With txtDataReceived
'.Font
.AppendText(serialPort.ReadExisting)
.ScrollToCaret()
End With
End Sub
Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serialPort.DataReceived
txtDataReceived.Invoke(
New MyDelegate(AddressOf UpdateTextBox), New Object() {})
End Sub
End
Class