Page 1 of 1
USB cable detection
Posted: Wed Jul 29, 2009 12:48 pm
by quid
Hello,
I'am trying to write a simple program to detect if the USB cable is connected /or disconnected to a pc during a running program.
I tried the USB initialise macro witch returns a "0" if connected and a "255" disconnected.
This doesn't work,the program seems to hang in a loop somehow.
Is there another simple way to check the usb cable state?
Thx
Re: USB cable detection
Posted: Wed Jul 29, 2009 1:38 pm
by Benj
Hello
To allow the USB detect to work you must go into the USB component properties and tick the box saying Enumeration wait. The length specified is the amount of time the component will wait in the initialise macro before returning.
Re: USB cable detection
Posted: Wed Jul 29, 2009 11:55 pm
by quid
Hi,
I know about the Enumeration wait.
The program detects when the cable is connected,but when i disconnect the cable the retval value stays "0".
So the program only detect the cable connection one time.
Regards.
Re: USB cable detection
Posted: Thu Jul 30, 2009 12:18 am
by quid
Hi,
When I start the program without the usb cabel plugged in the Lcd screen shows: "cable failure".Which is ok
After plugging in the Lcd screen shows:"cable connected" .Which is also ok
When I now unplug the cable the Lcd shows:"cable connected" .What I is wrong here?
Is it possible to reduce the Enumeration wait time somehow.1Sec is a long time time.Can it be done by interrupt?
Thx
Re: USB cable detection
Posted: Thu Jul 30, 2009 9:18 am
by Benj
Hello
If you reduce the enumeration wait anymore then Windows will not have enough time to recognise the device and think about assigning drivers before the device dissapears again. If you are trying to install the drivers for the first time then this can be very annoying.
What you could do is when you think you are connected you could send a ping message eg a byte with the value 0x55 if the computer is connected then the software running on it (VB / Labview / Delphi) could return the 0x55 or 0xAA etc and this could be your way to detect if you are still connected to the PC. Note once you have had a successful connection established you do not have to call the connect macro anymore that is unless you want to confirm connection status without using the PC side software.
Re: USB cable detection
Posted: Thu Jul 30, 2009 10:39 am
by quid
Hi,
I have build a windmill,wich is 200M from the house.
I send all the data from the windmill wireless to a portable receiver ,which act also as a data logger.
The dectection of a USB cable plugged in ,or not,should allow me to download the data directly in Exel for futher proces.
The program itself is working but the USB cable detection part works only once.
Is it possible to receive an example how to send a ping message?
So far all the programming is done with Flowcode.
All ideas most welcome.
Re: USB cable detection
Posted: Thu Jul 30, 2009 4:20 pm
by Benj
Hello Quid
I have found an easy workaround for your problem. When sending bytes or strings using the USB component macro, there is a return value.
If this return value is 0 then the data pipeline to the USB is ok meaning data is either making it to the PC or the buffer is not full yet. If you continue sending data and there is no PC connected then eventually the USB buffer will become full and the send functions will return a 1 instead of a 0.
This is your indication that the PC has become disconnected or the PC application (eg Hyperterminal) is not receiving the USB data.
At the moment a data transfer timeout of 32000 cycles applies to the send byte and send string macros. If you wanted to make this timeout shorter then this should not be a problem just dont make it too small. You can edit the timeout value by using the Flowcode V4 Custom Code feature and editing the timeout values in the send byte and send string functions.
Let me know how you get on.
Re: USB cable detection
Posted: Tue Aug 04, 2009 11:39 pm
by quid
Hi Benj,
Thanks for the easy sollution,the whole system is working pretty well .
Quid