Serial Terminal Code for VisualBas 2010 Express
This code posted here I entered (and modified) from the tutorial at this site :
'http://www.devx.com/dotnet/Article/31001 The comments are all mine except the one noted. I recommend going to the original site and following the instructions to build this code. Then test it and make sure it works. Then you can either try to cut and paste my differences into the code or Just copy this listing into a new project (Cntl A to select all, Cntl V to paste into project). This way you will understand more what is going on in the code and can make changes as needed, like I did. I added 3 Labels and a Combobox, as you can see. I tried to make the comments for a beginner to understand but it is assumed that the beginner does know some Basic but not necessarily Microsoft Visual Studio Visual Basic 2008/2010 Express type Basic. Although this program works as a terminal, I do not understand the INVOKE/DELAGATE part at the end and so no useful comments are there and it will be hard to modify that part. Also since the code can not be modified until it is understood, this is limiting the current code to terminal use only. I am working on it. 'Modified & comments by Gary Camp Dec 1, 10 'this is from DevX website article & not bad at start but... 'http://www.devx.com/dotnet/Article/31001 Public Class Form1 Dim WithEvents serialPort As New IO.Ports.SerialPort Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load For i As Integer = 0 To _ My.Computer.Ports.SerialPortNames.Count - 1 'cbbCOMPorts is a combobox on form to chose a port cbbCOMPorts.Items.Add( _ My.Computer.Ports.SerialPortNames(i)) Next 'then you can turn it on (connect) btnDisconnect.Enabled = False End Sub '------------------------------------------- ' Event handler for the Connect button '------------------------------------------- Private Sub btnConnect_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnConnect.Click If serialPort.IsOpen Then serialPort.Close() End If 'Here the port is set up, geting the name of the port from 'cbbCOMPorts above and using the serialPort.DataBits = 8 kind 'of VB command for seting things directly. Thus, you could 'put this kind of command anywhereto set the serial port up 'I modified the "with" so I could select the BaudRate with 'a combobox(1). Note that the Try/With structure is not 'actually needed to acomplish this. It was put in by the 'auther to make a safer program (I think) to catche potential 'errors in the serial port set up Try With serialPort .PortName = cbbCOMPorts.Text 'Get port from combobox .BaudRate = ComboBox1.Text 'Get baud from other one .Parity = IO.Ports.Parity.None .DataBits = 8 .StopBits = IO.Ports.StopBits.One End With serialPort.Open() 'I added this line next to display the actual BaudRate Label2.Text = serialPort.BaudRate 'Show actual baud lblMessage.Text = cbbCOMPorts.Text & " connected." 'For saftey the conn/disconn btns are set or unset 'as needed in the program but it still is not safe. btnConnect.Enabled = False btnDisconnect.Enabled = True Catch ex As Exception MsgBox(ex.ToString) End Try End Sub 'Continues at top of next columb, delete this line |
'-------------------------------------------
' Event handler for the Disconnect button '------------------------------------------- Private Sub btnDisconnect_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnDisconnect.Click 'Again using the try cmd to catch errors. I have to read 'more to see if it is really needed. Seems to still 'work if I comment the try/catch (and msg) stuff out. Try serialPort.Close() lblMessage.Text = serialPort.PortName & " disconnected." btnConnect.Enabled = True btnDisconnect.Enabled = False Catch ex As Exception MsgBox(ex.ToString) End Try End Sub '------------------------------------------- ' Event handler for the Send button '------------------------------------------- Private Sub btnSend_Click( _ ByVal sender As System.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 ' ---This is about the only comment from the auther, and I ' do not really understand it enough to use it yet,Gary--- 'One nice feature of the SerialPort class is that you need not 'constantly poll for incoming data. Instead, you just need to 'service the DataReceived event and it will automatically fire 'when incoming data is detected. However, as this event is running 'on a separate thread, any attempt to update the main Form directly 'will result in an error. Hence, you need to use a delegate to 'update controls on the main thread (Form1): '------------------------------------------- ' Event handler for the DataReceived '------------------------------------------- '************************************************** 'This makes no sense. The Sub below does something with/to the 'updateTextBox which is a Sub below and txtDataReceived is 'also used here and there. It is almost circular definition. Private Sub 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 '------------------------------------------------------ ' Delegate and subroutine to update the Textbox control '------------------------------------------------------ 'I do not understand this at all. Is this a Sub?Where is End? Public Delegate Sub myDelegate() Public Sub updateTextBox() With txtDataReceived .Font = New Font("Garamond", 12.0!, FontStyle.Bold) .SelectionColor = Color.Red .AppendText(serialPort.ReadExisting) .ScrollToCaret() End With End Sub 'I added this ComboBox1 to display the Baud Rate Private Sub ComboBox1_SelectedIndexChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles ComboBox1.SelectedIndexChanged 'ComboBox1 selects actual baudrate (takes effect on connect) Label2.Text = serialPort.BaudRate 'Show actual Baud End Sub End Class |