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
USB cable detection
Moderator: Benj
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: USB cable detection
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: USB cable detection
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.
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.
- Attachments
-
- USB cable.fcf
- (8 KiB) Downloaded 276 times
Re: USB cable detection
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
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
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: USB cable detection
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: USB cable detection
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.
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.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: USB cable detection
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: USB cable detection
Hi Benj,
Thanks for the easy sollution,the whole system is working pretty well .
Quid
Thanks for the easy sollution,the whole system is working pretty well .
Quid