Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 3 months ago.
Could the mbed know if he is connected to the PC?
Hi there,
I have an externally powered mbed which is not powered by connecting the USB cable. And I would like to have a safe solution to know if somebody is reading the printf messages from the mbed. I tried to use the return value of printf, but it doesn't work at all. (look at the code below) If nobody is reading the message, I thought the return value of printf should be below 0, but it isn't - at least LED1 is permanently off!
#include "mbed.h" DigitalOut errorLED(LED1); DigitalOut executionLED(LED2); int main() { while(1) { if (0<printf("test\n")) errorLED = 0; // successfull else errorLED = 1; // not successfull executionLED = ! executionLED; // LED2 toggling wait(1); } }
Anybody has an idea? Besides: No I don't want to send a message from PC to the mbed first. Sure I could, but I won't.
Regards David
Question relating to:
1 Answer
11 years, 3 months ago.
Printf returns the number of characters written (at least that is what it should do). An UART won't know if there is anyone listening at the other side, there is no ACK or something similar, so it can just return it sent those characters.
So options are either software on the PC which sends something back (can just be something random), or if it is enough to know for you if the USB cable is plugged in (but then you don't know if someone is actually listening, just that it is plugged in), I guess you can connect Vu to a random other pin, possible enable pull-down, and get its voltage. If USB is plugged in it should be 5V, otherwise 0V.
Oh yes you are right. I forget that is basically an UART communication. Anyway aren't those UART messages packed into USB packages? Because some of the USB package types get a confirm answer. But I guess USB Virtual Serial Serial Port use one of those types, which get no answers.
An other idea: When an USB device will be connected to the PC, there is running a kind of negotiation and configuration first. Is there a chance to get a configuration ready signal?
posted by 17 May 2013Problem is that the USB<>Serial conversion is handled by the mbed IC on the bottom of the PCB. So the LPC1768 doesn't have any information regarding that, it just communicates via UART0. If you use the LPC1768's USB port as virtual com port you could get some more info probably. At least if the USB port is plugged in, but thats the same as just shorting your Vu pin to a random other pin and reading that voltage back.
With some googling I found there also used to be an mbed_interface_vbus() command which could tell if there was USB power, but apparently that never made it out of beta.
posted by 17 May 2013Okay thank you for your help Erik!
It looks like there is no solution. I found the mbed_interface_bus() at the Beta page. Even this one would only tell me if USB is connected, not if someone is listening. So the same like Vu connected to a random pin.
Besides: Vu is not a perfect solution to detect if a USB cable is connected. When I power my mbed externally over Vin and leave USB disconnected, the voltage at Vu is raising very slow but steady. It's stopping somewhere between 3V and 4V depending on the input voltage and temperature of the mbed. I guess when it get hot enough, Vu would get close to Vin, but I didn't test this.
posted by 17 May 2013