tag:blogger.com,1999:blog-9977539741480046.post23463613886501141..comments2024-02-09T23:27:37.645+11:00Comments on Zach Saw's Blog: SerialPort IOException Workaround in C#Zach Sawhttp://www.blogger.com/profile/02019604928253273845noreply@blogger.comBlogger58125tag:blogger.com,1999:blog-9977539741480046.post-35414796060601345482020-06-08T03:46:28.506+10:002020-06-08T03:46:28.506+10:00It seems that the code above is not compatible whe...It seems that the code above is not compatible when running on .NET Core (Windows 10, 64bit version 2004)Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-25822141743799802032018-05-12T15:00:12.814+10:002018-05-12T15:00:12.814+10:00Hm... why the unchecked integer in the constructor...Hm... why the unchecked integer in the constructor? It's a constant and the assigned value falls well within the range of System.Int32.<br /><br />I'm converting to VB.NET, which doesn't have the unsigned keyword. It's true that there are some workarounds, but the ones I've seen feel klunky to me.<br /><br />It appears we can leave it out here, and just use a checked System.Int32. If you feel I'm mistaken with this observation, I'd be pleased to hear more about it.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-15168930577271236902015-02-24T17:57:06.883+11:002015-02-24T17:57:06.883+11:00Stack Trace: SerialPortFixer.WinIoError()
at Seria...<b>Stack Trace: SerialPortFixer.WinIoError()<br />at SerialPortFixer..ctor(String portName)<br />at SerialPortFixer.Execute(String portName)</b>Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-2833223170383956762014-09-19T23:42:56.309+10:002014-09-19T23:42:56.309+10:00In my case, the call to m_Handle.Close() in Dispos...In my case, the call to m_Handle.Close() in Dispose() freezes (never returns) rendering the solution useless... Anybody else had that problem?Raloshttps://www.blogger.com/profile/03478774757827400853noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-7733089059587357542014-09-19T17:32:36.347+10:002014-09-19T17:32:36.347+10:00Hi,
thanks for the code. I have a similar problem...Hi,<br /><br />thanks for the code. I have a similar problem, but in my case it is not IOException, but ObjectDisposedException in serial port (after the port was disconnected by the device - virtual serial port).<br />It seems that this problem do not work in my case. Unfortunately after applying this code the port was blocked after first error (port is there even when the device was disconnected and "File not found" exception is thrown when the port is connected).<br /><br />But at least I know, that the serial port in .NET framework has many bugs.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-49035750799149895052014-09-03T04:51:35.076+10:002014-09-03T04:51:35.076+10:00System.IO.IOException was unhandled
Message=A de...System.IO.IOException was unhandled<br /> Message=A device attached to the system is not functioning.<br /><br /> Source=System<br /> StackTrace:<br /> at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)<br /> at System.IO.Ports.InternalResources.WinIOError()<br /> at System.IO.Ports.SerialStream.set_DtrEnable(Boolean value)<br /> at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)<br /> at System.IO.Ports.SerialPort.Open()<br /> at SerialPortTester.Program.Main(String[] args) in C:\Users\user\Desktop\Temp\SimpleSerialCS\SimpleSerial\Program.cs:line 216<br /> at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)<br /> at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)<br /> at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()<br /> at System.Threading.ThreadHelper.ThreadStart_Context(Object state)<br /> at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)<br /> at System.Threading.ThreadHelper.ThreadStart()<br /> InnerException: <br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-39412488134081273572014-02-18T22:57:19.232+11:002014-02-18T22:57:19.232+11:00Thanks to Zach for the program. I also had the pro...Thanks to Zach for the program. I also had the problem that Dan du Preez had, in my case I was not calling Dispose(). If anyone has a similar problem they should ensure that Dispose() is being called. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-37411197212221912292013-11-07T03:27:09.746+11:002013-11-07T03:27:09.746+11:00Thank you!
You have saved me huge trouble.
Where i...Thank you!<br />You have saved me huge trouble.<br />Where is the tip jar?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-20594106014470962102013-07-11T07:46:55.005+10:002013-07-11T07:46:55.005+10:00I've pasted the snippet, believing that this w...I've pasted the snippet, believing that this would finally work... It seemed so obvious it will. But after running the code, it fails on SetCommState call. As I see, this function is called for 10 times, and if the result is still not good, it throws an exception.<br />But even trying to omit this (not throwing the exception), still opening of the port is impossible (IOException: Device is not connected....).<br />Could you help with explanation of such situation? That would be very nice. Kind regards, Tom.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-43724854720686608422013-05-23T15:57:12.066+10:002013-05-23T15:57:12.066+10:00Zach ... thanks so much for your well put article ...Zach ... thanks so much for your well put article and code snip....<br /><br />I just suddenly started having problems with a service not been able to connect to Serial ports..<br /><br />I implemented this code (granted in VB.NET) and it now works ...<br /><br />BTW, I put up the VB code here -> http://forums.codeguru.com/showthread.php?537211-Com-Ports-in-a-Service<br /><br />thanks...GremlinSAhttp://forums.codeguru.com/showthread.php?537211-Com-Ports-in-a-Service...noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-10376053605179324812013-05-11T16:03:59.363+10:002013-05-11T16:03:59.363+10:00Thanks a bunch! Searched for an hour before I foun...Thanks a bunch! Searched for an hour before I found this workaround.<br /><br />My code had been working for a long time, but suddenly one day I started getting this problem on the .Open() method.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-48469736317635358442013-05-10T06:50:14.749+10:002013-05-10T06:50:14.749+10:00The gift that keeps on giving. Just found your bl...The gift that keeps on giving. Just found your blog today and it works great. Thanks very much for the post.Anonymoushttps://www.blogger.com/profile/04607818779240815337noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-40631802867875272262013-04-10T01:02:00.535+10:002013-04-10T01:02:00.535+10:00Thanks so much for posting this fix - I have been ...Thanks so much for posting this fix - I have been struggling with this problem for years.<br /><br />I'm VERY grateful!<br /><br />JeffJeff Frandsenhttps://www.blogger.com/profile/08334620179318665541noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-78254828592695192792013-02-19T22:20:30.433+11:002013-02-19T22:20:30.433+11:00I still have same problem i.e "A device attac...I still have same problem i.e "A device attached to the System is not functioning"<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-55813374503336649522013-02-17T08:46:22.396+11:002013-02-17T08:46:22.396+11:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/08884105356084363610noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-15749084277149646342013-01-12T03:18:31.759+11:002013-01-12T03:18:31.759+11:00What's the reasoning behind CommStateRetries=1...What's the reasoning behind CommStateRetries=10 ?<br /><br />Is it really necessary to try it 10 times ?<br /><br />Also, it it necessary to call ClearCommErrors before calling Get/SetComState ?<br /><br />Thanks<br />IanIanSnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-66605994752719437052012-10-23T00:36:04.234+11:002012-10-23T00:36:04.234+11:00Great code. I have been using it successfully as 3...Great code. I have been using it successfully as 32 bit for some time. Just switched the application to 64 bit and on 64 bit machine it breaks. <b>GetFileType</b> throws an <b>Access is denied</b> error? Any ideas anyone?Richnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-41638302505901334252012-09-30T08:21:53.367+10:002012-09-30T08:21:53.367+10:00Got rid of all my headaches by just writing a simp...Got rid of all my headaches by just writing a simple managed C++ dll and having my C# project reference that instead of the .NET SerialPort class. <br /><br />Thanks SO MUCH M$!OurTiehttps://www.blogger.com/profile/14619480076477175174noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-28175356537242444922012-09-29T23:08:05.360+10:002012-09-29T23:08:05.360+10:00I pasted code into new module, but have no idea ho...I pasted code into new module, but have no idea how to referance it?<br /><br />I get this i/o thread issue when my usb voice modem is tet as AT-STR=3 trying to detect when the extention goes off hook...<br /><br />i get 4-5 messages coming in from the serial port, but none of them can be read because of the error...<br /><br />any advice please email me: ctf_dai_lafing@msn.comAnonymoushttps://www.blogger.com/profile/04201779306492259639noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-91635461403359061982012-08-30T18:02:50.486+10:002012-08-30T18:02:50.486+10:00@Ken Wilson: I have the same situation with a GPS ...@Ken Wilson: I have the same situation with a GPS USB where the users will unplug the device Ken. Have you (or anyone else) been able to find a work around to this situation? It is really a critical issue for us and IS DRIVING ME CRAZY!!! Thank you Microsoft >:(OurTiehttps://www.blogger.com/profile/14619480076477175174noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-71352737891967965042012-08-25T04:47:20.095+10:002012-08-25T04:47:20.095+10:00I put in a debug statement after GetCommState. Thi...I put in a debug statement after GetCommState. This helped me to determine who the culprit was (HyperTerm). e.g.<br /><br /> GetCommStateNative(ref dcb);<br /> uint fAbort = dcb.Flags & (1u << DcbFlagAbortOnError);<br /> Debug.WriteLine("fAbort as found = " + fAbort.ToString());<br />eejake52https://www.blogger.com/profile/03749870902536039144noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-26568983824024571532012-04-18T15:34:24.814+10:002012-04-18T15:34:24.814+10:00@ Sean Riddle: Can u plz share your c++ code@ Sean Riddle: Can u plz share your c++ codeAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-57456795505091690872012-04-12T23:05:51.622+10:002012-04-12T23:05:51.622+10:00Absolutely perfect, converted to VB and work perfe...Absolutely perfect, converted to VB and work perfect !!<br /><br />Thank You!Francoishttps://www.blogger.com/profile/01655310807878618723noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-82143512456058853222012-02-25T09:23:39.106+11:002012-02-25T09:23:39.106+11:00Zach. Nice job on this. The GC.SuppressFinalize su...Zach. Nice job on this. The GC.SuppressFinalize suggestion works in preventing the IO error but causes a worse problem. I have an application running on tablets (Windows 7, .NET Framework 4.) that is GPS enabled. Problem is that users have a tendency to unplug the GPS dongle from the USB port. As soon as they do that, we get the IO error. But using your code and the GC.SuppressFinalize idea, I avoid the IO error, but then basic windows control functions, like form closing, don't work properly (events get fired but form doesn't close). If I remove the GC.SuppressFinalize from my code, the form works just fine (except for the IO error when the user unplugs the GPS device). I'm in a bit of a vicious loop here. Any suggestions?Ken Wilsonhttps://www.blogger.com/profile/13520662824384308787noreply@blogger.comtag:blogger.com,1999:blog-9977539741480046.post-60962463594243233052012-01-04T17:44:51.976+11:002012-01-04T17:44:51.976+11:00Thanks to everyone fighting against this problem. ...Thanks to everyone fighting against this problem. I tried the serialportfixer and the GC.SuppressFinalize suggestion, but unfortunately none of them fixed my problem. I still get the exception: "System.IO.IOException: Ein an das System angeschlossenes Gerät funktioniert nicht.<br /><br /> at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)<br /> at System.IO.Ports.InternalResources.WinIOError()<br /> at System.IO.Ports.SerialStream.set_DtrEnable(Boolean value)<br /> at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)<br /> at System.IO.Ports.SerialPort.Open()<br /> at Helice.Helice._RequestHeliceInfo(String port) in D:\devicemanager\DeviceManager\DeviceManager\Helice\Helice.cs:line 400" almost every time I try to open the port :-( Does anyone has anymore idea how to solve this?HorstJnoreply@blogger.com