Reputation: 449
I'm working with the VB.NET SerialPort class and I have created a collection of objects that represent the configuration our hardware devices so that I can alter the configuration and update the devices through the a USB virtual serial port. These objects are of a polymorphic nature and each device eventually inherits form a class called CmdSender which handles the COM IO. When finally removing all known bugs and publishing an application to another PC (which has .NET run-time installed but not Visual Studio) occasionally an uncatchable exception is thrown when calling SerialPort.Write(string) from my code.
This is the error message and topmost part of the stacktrace.
System.IO.IOException: A device attached to the system is not functioning.
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str) at System.IO.Ports.SerialStream.EndWrite(IAsyncResult asyncResult) at System.IO.Ports.SerialStream.Write(Byte[] array, Int32 offset, Int32 count, Int32 timeout) at System.IO.Ports.SerialPort.Write(String text) at TDDevice.CmdSender.SendPure(String Cmd, Boolean AddCheckSum)
I can't find any solutions online to this problem. It's also worth noting that when this exception is thrown that the application cannot simply be restarted (although that's still not ideal anyway) but the USB COM device must be removed and reinserted into the machine, without doing this my application warns me that no COM port can be found (a check that I've built in), this is telling me that .NET cannot interact with the COM port after this, I am yet to test whether other API's are able to still use the port after this occurs to confirm whether the issue affects just .NET or the port itself (however device manager still lists the port).
My question is why is this happening, and how can it be prevented? (any answers in C# should be fine as I can hopefully translate but VB would be appreciated)
Upvotes: 0
Views: 1115
Reputation: 1
I worked a little with serial port communicating with a PIC microcontroller and a got errors like this. My solution at that time was to close the port after sending the data, and open again when needed to send again. I think since is a USB convertor the device may enter sleep mode or something similar.
sendData(){
open port
send data
wait for response if needed
close port
}
sorry for not posting code but you get the ideea
Upvotes: 0