Serial port is opening even though nothing is connected
In my code to open a serial port, when I search ports two ports are being shown in the combobox. when I select COM4, then it is going straight to the Catch and showing error as nothing is connected. But when I select COM5 in the combobox, then the port is opening even though nothing is connected here either. I have only two cable points in my laptop. Why is this happening?
try
{
serialPort1.PortName = comboBox1.Text;
serialPort1.Open();
button1.Enabled = false;
button2.Enabled = true;
DateTime d = new DateTime();
d = DateTime.Now;
textBox21.Text = d.ToString("dd.MM.yyyy");
}
catch
{
MessageBox.Show("/*...*/"}c#
If the port is present in the system and there is no other application connected to it you will be able to connect to it even if there is no device on the other end. Essentially connecting to the Serial port is sort of like reserving the right of using it, preventing other apps from accessing it. There is no reliable way to determine if there is a device connected on the other end except for trying to communicate with the device.