Hi,
Since I discovered the free download of Visual Studio .Net Express 2008 over at
the microsoft site, I have had a small play bringing some of my VB 6 code over to the new standards.
Attached is a small sample I just wrote to communicate with the Serial Port.
The s/w enumerates all available ports automatically and data entred into the upper text box is transmitted to the port on clicking the Tx button. The data sent is terminated by vbCr character (chr(13)) but this can be modified in code.
Pauses are used to wait for any recieved data to be filled in the buffer, and the Rx data is then added to the RichTextBox. Again these can be modified or replaced by Do..While loops depending on what device you communicate with.
This is not the best way of working with VB.NET, I am reading a lot about threading at the moment. However this very simple code works in my application 100%, and hopefully it can give others a small head start.
Good luck!
Code:
Public Class Form1
Dim comPorts As Array 'Com ports enumerated into here
Dim rxBuff As String 'Buffer for receievd data
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'When the form loads
'Enumerate available Com ports and add to ComboBox1
comPorts = IO.Ports.SerialPort.GetPortNames()
For i = 0 To UBound(comPorts)
ComboBox1.Items.Add(comPorts(i))
Next
'Set ComboBox1 text to first available port
ComboBox1.Text = ComboBox1.Items.Item(0)
'Set SerialPort1 portname to first available port
SerialPort1.PortName = ComboBox1.Text
'Set remaining port attributes
SerialPort1.BaudRate = 19200
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'When Tx button clicked
'Clear buffer
rxBuff = ""
'If port is closed, then open it
If SerialPort1.IsOpen = False Then SerialPort1.Open()
'Write this data to port
SerialPort1.Write(TextBox1.Text & vbCr)
'Pause for 800ms
System.Threading.Thread.Sleep(800)
'If the port is open, then close it
If SerialPort1.IsOpen = True Then SerialPort1.Close()
'If the buffer is still empty then no data. End sub
If rxBuff = "" Then GoTo ends
'Else display the recieved data in the RichTextBox
RichTextBox1.Text = rxBuff
ends:
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'When ComboBox1 selection is changed, we need to update SerialPort1 with the new choise
'But only if the port is closed!
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = ComboBox1.Text
Else : MsgBox("Operation only valid when port is closed.", MsgBoxStyle.Exclamation, "Error")
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'This sub gets called automatically when the com port recieves some data
'Pause while all data is read
System.Threading.Thread.Sleep(300)
'Move recieved data into the buffer
rxBuff = (SerialPort1.ReadExisting)
End Sub
End Class