Serial port communication
-
Hello everybody My project is a C# project on VS2005. My application communicate with external device through Serial Port.I'm using USB to RS convertor whitch add another COM Port in my computer.When I connect with device through this COM Port(transmit and receive data) everythink is OK.When unplug the USB cable from PC and I try to transmit data the program is generate exeption.How can I detect when the COM Port is disappear? Thanks in advance
-
Hello everybody My project is a C# project on VS2005. My application communicate with external device through Serial Port.I'm using USB to RS convertor whitch add another COM Port in my computer.When I connect with device through this COM Port(transmit and receive data) everythink is OK.When unplug the USB cable from PC and I try to transmit data the program is generate exeption.How can I detect when the COM Port is disappear? Thanks in advance
Hi, I beleive you are looking for
.IsOpen
I've just tried this with a USB-Serial converter and it works. (Goes false on a disconnect). It doesn't cause an event though.Matthew Butler
-
Hi, I beleive you are looking for
.IsOpen
I've just tried this with a USB-Serial converter and it works. (Goes false on a disconnect). It doesn't cause an event though.Matthew Butler
Sorry but it doesn't works. My code is if (sp.IsOpen) { sp.Write(TxBuf, 0, (k + 2 + ofs)); } else { FL_Connected = false; MessageBox.Show("Message"); } When I unplug USB cable (Converter) from my computer the COM Port is still yet open but when the program try to write data to serial port it's generate exception "Access to the Port is denied"
-
Sorry but it doesn't works. My code is if (sp.IsOpen) { sp.Write(TxBuf, 0, (k + 2 + ofs)); } else { FL_Connected = false; MessageBox.Show("Message"); } When I unplug USB cable (Converter) from my computer the COM Port is still yet open but when the program try to write data to serial port it's generate exception "Access to the Port is denied"
You should always put communication like this in a try/catch block so you can catch exceptions like this. It may be worth starting a timer when you open your port, pausing it when you're about to read/write and stopping it when you close it. That way you can test the port on each Tick event so you always know when it's connected/disconnected. You can also monitor USB devices, get notified when one is inserted/removed and check if it's the one you're intersted in so you can act accordingly. There are some articles on this site if you search.
Dave
-
You should always put communication like this in a try/catch block so you can catch exceptions like this. It may be worth starting a timer when you open your port, pausing it when you're about to read/write and stopping it when you close it. That way you can test the port on each Tick event so you always know when it's connected/disconnected. You can also monitor USB devices, get notified when one is inserted/removed and check if it's the one you're intersted in so you can act accordingly. There are some articles on this site if you search.
Dave
thanks for an answer I try to do this try { sp.Write(TxBuf, 0, (k + 2 + ofs)); } catch { FL_Connected = false; MessageBox.Show("Message"); } And it works.But when I close the program it generate the same exception(Access to the port is denied)
-
thanks for an answer I try to do this try { sp.Write(TxBuf, 0, (k + 2 + ofs)); } catch { FL_Connected = false; MessageBox.Show("Message"); } And it works.But when I close the program it generate the same exception(Access to the port is denied)
Are you closing the port when you're finished with it? It sounds like you're keeping it open and the program is trying to close the port when it ends - which will generate an exception if it's not available. If you need to keep the port open for the duration, you should close the port manually in the formclosing event - and again, put that inside a try/catch block.
Dave
-
Are you closing the port when you're finished with it? It sounds like you're keeping it open and the program is trying to close the port when it ends - which will generate an exception if it's not available. If you need to keep the port open for the duration, you should close the port manually in the formclosing event - and again, put that inside a try/catch block.
Dave
Thanks DaveyM69 I try to do this protected override void OnClosing(CancelEventArgs e) { try { sp.Close(); } catch { int probe = 33; } } In this case there is no exception when close the program but the program is stoped on the breakpoint in the catch. There is something interesting.When I start the program again the all window is white for a 2 or 3 seconds and then starts normaly.
-
Thanks DaveyM69 I try to do this protected override void OnClosing(CancelEventArgs e) { try { sp.Close(); } catch { int probe = 33; } } In this case there is no exception when close the program but the program is stoped on the breakpoint in the catch. There is something interesting.When I start the program again the all window is white for a 2 or 3 seconds and then starts normaly.
Hmm... Looks like it's taking a while for it to Dispose. You could try calling Dispose() or Dispose(true) and maybe even do a GC.Collect() to force the clean up. First thing I'd do is see what exceptions if any are occurring
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.InnerException.Message);
}Dave
-
Hmm... Looks like it's taking a while for it to Dispose. You could try calling Dispose() or Dispose(true) and maybe even do a GC.Collect() to force the clean up. First thing I'd do is see what exceptions if any are occurring
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.InnerException.Message);
}Dave
The problem is when I unplug the USB converter from PC and COM Port is disapear any tring of using a method of instance of serial port generate exception i.e. I can't use Sp.Dispose() or SP.Close();
-
The problem is when I unplug the USB converter from PC and COM Port is disapear any tring of using a method of instance of serial port generate exception i.e. I can't use Sp.Dispose() or SP.Close();
-
Maybe check if it's null before doing any operations?
public static bool IsPortAvailable(System.IO.Ports.SerialPort portToTest)
{
return !(null == portToTest);
}Dave
Thanks DaveyM69 and sorry to loosing your time.In this moment nothing help.Now I will relax and try again later to decide the problem Thanks for all