473,837 Members | 1,450 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4785
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_grierNOSP AM@.msn.comwrot e:
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.hardands oftware.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_grierNOSP AM@.msn.comwrot e:
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.hardands oftware.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.T imer) 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_CO MPLETE_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
13207
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. However, I'm unsure of how exactly I should be designing the program, I thought I could use threading to start class: class scanner(Thread): def __init__(self,port):
5
34375
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 and I could solve that problem if I could read a couple entries from the registry. Thanks in advance, ~Logan
7
2773
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 while, the serial port seems corrupted because when my data is sent, it doesn't go through the serial port, so as same to receive process. In order to fix this, I have to close the port and reopen the port again. Why?!! Is there any way to fix...
6
2882
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 the first seconds the communication is ok, later i receive read/write error. I?ve been in MSDN site and there i discover that the read/write error is a INVALID_HANDLE problem. But why??? I just create the serial communication file and use it....
3
1715
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 on the device, the operation fails. I would like to have a setup where the user presses a button on my Palm application to initiate a HotSync and then continue with the Python program. It would go like this: user presses special button on Palm,...
1
4367
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 device is attached to the COM (Serial) port of my system using VC++/C++ ? prblm: Regardless of any device is connected to serial/COM port, the port can be opened using CFile, if no other programme has locked the port. But i need to find Is any...
6
2399
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
1973
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 at time. If yes ,how it is possible? Thanks in advance.
0
1808
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 fine! But now i want to put a DeviceDriver-Module in front of the RFCOMM-Device, so that PPP uses my DeviceDriver-Module. The Module just forwards the data from PPP to the RFCOMM device - no data is changed (later the module shall filter some...
0
9842
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9682
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10566
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10623
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10271
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7000
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5848
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3124
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.