Professional Documents
Culture Documents
April 2008 61
property to match the new selected port, and opens
Sub OpenComPort()
the selected port:
Try
CloseComPort()
‘ Get the selected COM port’s name myComPort.PortName = _
‘ from the combo box.
cmbPorts.SelectedItem.ToString
If Not myComPort.IsOpen Then OpenComPort()
myComPort.PortName = _
cmbPorts.SelectedItem.ToString
When a user selects a new bit rate in the
‘ Get the selected bit rate from the combo cmbBitRate combo box, the combo box’s
box. SelectedIndexChanged event executes. Placing this
If cmbBitRate.SelectedIndex > 0 Then code in the event’s routine sets myComPort’s
myComPort.BaudRate = _ BaudRate property to match the selected bit rate:
CInt(cmbBitRate.SelectedItem)
End If myComPort.BaudRate = _
CInt(cmbBitRate.SelectedItem)
‘ Set other port parameters.
myComPort.Parity = Parity.None Each button on the form has a click event, which
myComPort.DataBits = 8
myComPort.StopBits = StopBits.One calls the SendCommand routine to send a command
myComPort.Handshake = Handshake.None to the COM port. Place this code in the ON button’s
click event to send the command “L11”:
myComPort.ReadTimeout = 3000
myComPort.WriteTimeout = 5000
SendCommand(“L11”)
‘ Open the port.
64 April 2008