Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
'--close the serail port if it is open --
If serialPort1.IsOpen Then
serialPort1.Close()
End If
Try
'-- configure the various parameters of the serial port --
With serialPort1
.PortName = "COM22"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.DataBits = 8
End With
' -- now open the port
serialPort1.Open()
' -- update the status if you like and
' -- enable/disable the buttons --
btnConnect.Enabled = False
btnDisconnect.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Code (C#)
private void btnConnect_Click(System.Object sender, System.EventArgs e)
{
//--close the serail port if it is open --
if (serialPort1.IsOpen) {
serialPort1.Close();
}
try {
//-- configure the various parameters of the serial port --
var _with1 = serialPort1;
_with1.PortName = "COM22";
_with1.BaudRate = 9600;
_with1.Parity = System.IO.Ports.Parity.None;
_with1.StopBits = System.IO.Ports.StopBits.One;
_with1.DataBits = 8;
// -- now open the port
serialPort1.Open();
// -- update the status if you like and
// -- enable/disable the buttons --
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
} catch (Exception ex) {
Interaction.MsgBox(ex.ToString());
}
}