473,394 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

USB device on serial port locks program when disconnected

JDS
I have an application that interfaces with a USB device using the .Net
serial port. The code works fine, displaying live data on the screen;
that is until the USB lead is pulled out from the PC when the program
hangs.

The obvious answer is not to pull the lead out, but unfortunately the
application is one in which there is a high possibility the lead will
be pulled out so the code needs to be more robust.

The form has a timer (System.Windows.Forms.Timer) that sends a request
to the device for data. The returning data is handled by the
DataReceived event of the serial port and uses a delegate to process
the data, ultimately displaying it on the form.

The timer code first checks to ensure the port exists (Not IsNothing)
and that it is open (IsOpen) before sending the request data command.

I am struggling to see what I can do to prevent the application from
hanging when the lead is disconnected (and the port is open).

Any help greatly appreciated.

Jeremy.
Aug 29 '08 #1
4 4734
Hi,

Comments inline>>>
I was assuming that the .Net SerialPort would be the best option but
it seems not. Are you saying that there is an inherent problem with
it?
<<<

For devices that use USB, AND that might be disconnected during operation,
the (IMO) the best option is DesktopSerialIO. The problem with the built-in
serial port object is that it uses SetCommMask/WaitCommState APIs. The
WaitCommState callback is not properly processed when the USB device is
disconnected. (I think this may be a problem in the API, not in the actual
serial port object).

If you take a look at the DesktopSerialIO code on the CD ROM that
accompanies my book, you will see that I do not use the WaitCommState API.
The result is that my code has approximately 5% poorer raw serial receive
throughput (though this is never an issue in a practical application), but
it doen't suffer a hang when the USB serial device is disconnected.
>>>
I am programming in VS2008 but targeting .Net version 2.0 as I have
seen various reports of problems of the SerialPort object in .Net 3.5
in a few forums. Would the dll in the fourth edition or your website
be suitable for this?
<<<

Yes. I recommend that you actually built the dll as part of your project.
Thus, the targeted framework is handled as you build or rebuild you project.

If your copy of the book is even a year or so old, you may want to purchase
an updated CD ROM. I keep it up to date, with new examples and utility
programs, even though I have not changed the actual printed text. The
current CD content exceeds 500 MB. See my website, under books, for
purchase information.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
Aug 30 '08 #2
JDS
On Aug 30, 5:47*pm, "Dick Grier" <dick_grierNOSPAM@.msn.comwrote:
Hi,

Comments inline>>>
I was assuming that the .Net SerialPort would be the best option but
it seems not. Are you saying that there is an inherent problem with
it?
<<<

For devices that use USB, AND that might be disconnected during operation,
the (IMO) the best option is DesktopSerialIO. *The problem with the built-in
serial port object is that it uses SetCommMask/WaitCommState APIs. *The
WaitCommState callback is not properly processed when the USB device is
disconnected. *(I think this may be a problem in the API, not in the actual
serial port object).

If you take a look at the DesktopSerialIO code on the CD ROM that
accompanies my book, you will see that I do not use the WaitCommState API..
The result is that my code has approximately 5% poorer raw serial receive
throughput (though this is never an issue in a practical application), but
it doen't suffer a hang when the USB serial device is disconnected.

I am programming in VS2008 but targeting .Net version 2.0 as I have
seen various reports of problems of the SerialPort object in .Net 3.5
in a few forums. Would the dll in the fourth edition or your website
be suitable for this?
<<<

Yes. *I recommend that you actually built the dll as part of your project.
Thus, the targeted framework is handled as you build or rebuild you project.

If your copy of the book is even a year or so old, you may want to purchase
an updated CD ROM. *I keep it up to date, with new examples and utility
programs, even though I have not changed the actual printed text. *The
current CD content exceeds 500 MB. *See my website, under books, for
purchase information.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
Seewww.hardandsoftware.netfor details and contact information.
Fantastic. Thanks for all your help.
Aug 30 '08 #3
JDS
On Aug 30, 5:47*pm, "Dick Grier" <dick_grierNOSPAM@.msn.comwrote:
Hi,

Comments inline>>>
I was assuming that the .Net SerialPort would be the best option but
it seems not. Are you saying that there is an inherent problem with
it?
<<<

For devices that use USB, AND that might be disconnected during operation,
the (IMO) the best option is DesktopSerialIO. *The problem with the built-in
serial port object is that it uses SetCommMask/WaitCommState APIs. *The
WaitCommState callback is not properly processed when the USB device is
disconnected. *(I think this may be a problem in the API, not in the actual
serial port object).

If you take a look at the DesktopSerialIO code on the CD ROM that
accompanies my book, you will see that I do not use the WaitCommState API..
The result is that my code has approximately 5% poorer raw serial receive
throughput (though this is never an issue in a practical application), but
it doen't suffer a hang when the USB serial device is disconnected.

I am programming in VS2008 but targeting .Net version 2.0 as I have
seen various reports of problems of the SerialPort object in .Net 3.5
in a few forums. Would the dll in the fourth edition or your website
be suitable for this?
<<<

Yes. *I recommend that you actually built the dll as part of your project.
Thus, the targeted framework is handled as you build or rebuild you project.

If your copy of the book is even a year or so old, you may want to purchase
an updated CD ROM. *I keep it up to date, with new examples and utility
programs, even though I have not changed the actual printed text. *The
current CD content exceeds 500 MB. *See my website, under books, for
purchase information.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
Seewww.hardandsoftware.netfor details and contact information.
UPDATE:

For the benefit of anyone else coming across this thread with similar
problems.

I have written a test application using Dick's DesktopSerialIO and
restructered the rest of the code. This has had mixed results. If I
stream data to the port I seem to be able to unplug the USB connection
OK. However, if I use a timer (on this occasion using
System.Timers.Timer) to send a request to the device and wait for the
data to return, this works fine until I unplug the USB and then I get
the blue screen of death with a reference to
MULTIPLE_IRP_COMPLETE_REQUESTS. I have tried to keep the timer
implementation as simple as possible with auto_reset set to false to
re-enabling it when data is received to try and prevent timing issues.

However, a quick Google on the error message and there seems to be an
underlying problem with Vista and hardware issues. For some other
similar problems there is a recommendation to install SP1. I have now
installed SP1 and all seems to be OK; I can poll for data, unplug the
USB and no crashes. (However, I am not sure why the difference with
Vista pre-SP1 between polling and not).

Hope this is of help to anyone else encountering similar difficulties.

Jeremy.
Sep 2 '08 #4
Hi,
>>
However, I am not sure why the difference with
Vista pre-SP1 between polling and not).
<<

I have no idea, "why." I haven't had a previous report. Naturally, my
systems have SP1 installed (but I don't use the Vista machines for my
day-to-day work, anyway).

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
Sep 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: willie | last post by:
Hi, I'm writing a program which requires the use of three serial ports and one parallel port. My application has a scanning devices on each port, which I can access fine with pyserial. ...
5
by: Logan McKinley | last post by:
I was wondering if there is any way for me to read the device manager in my C# program? I have a program that crashes when some third party hardware is not installed or is set to the wrong port...
7
by: Michael Chong | last post by:
I wrote a program that communicate with SerialComm. In every 300 milliseconds, my program continuously send & receive data via the serial port once the program starts. My program is once in a...
6
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in...
3
by: dylpkls91 | last post by:
I am writing a program that uses the Palm handheld as a sort of input device. However, the code that communicates with it thru the serial port locks the port up, so if the user initiates a HotSync...
1
by: sujith[arsse] | last post by:
Hi all, I'm new to this community,so i think i shall introduce myself first, I'm a developer in VC++ 6.0 for past few years, Now my mission to post this thread is how can i find "Is any...
6
by: dev | last post by:
hi i want to know about interfacing or accessing a serial port using c-program please help me with an example thanks for whom who reply me soon
2
by: paridaG | last post by:
Hi Can any one help me ? I have connected a HID reader to my serial port. It is working fine through my c++ program. Is it possible to connect one more device to serial port and need to work...
0
by: dagobert2000 | last post by:
Hi! First of all, I'm using a the latest Version of a FUSD Module...for my device-driver. I have the following situation: I have a PPP connection over a serial (RFCOMM-)device. That works...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.