I could be wrong, but I believe you need to have the mBuino loaded with USB Serial code or you will not get the mbed Virtual port loaded. SO many things happened to me trying to get the driver loaded I am not sure now
Perhaps someone from GHI will/can enlighten us on this one?
Here is the code I used.
#include "mbed.h"
#include "USBSerial.h"
#include <sstream>
//Declare the 7 LEDs as outputs
DigitalOut LED[] = {(LED1), (LED2), (LED3), (LED4), (LED5), (LED6), (LED7)};
//Virtual serial port over USB
USBSerial serial;
Ticker toggle;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
DigitalOut led5(LED5);
DigitalOut led6(LED6);
DigitalOut led7(LED7);
bool begin = false;
int count = 1;
void toggler() {
if(begin)
{
led1 = !led1;
led3 = !led3;
led5 = !led5;
led7 = !led7;
char str[80];
sprintf(str, "Count: = %d", count);
serial.puts("USB write on led change state: ");
serial.puts(str);
serial.puts("\r\n");
count++;
}
}
//
int main(void) {
//led changes to show activity
led1 = 1;
led2 = 0;
led3 = 1;
led4 = 0;
led5 = 0;
led6 = 0;
led7 = 0;
toggle.attach(&toggler, 1.5); //Delay between writes
while(1)
{
//Wait until we can attach the Terminal to the USB Virtual Port
if(!begin)
{
char c = serial.getc(); //wait for any keypress from the terminal
serial.puts("\r\nBegin writing to USB port\r\n");
begin = true;
}
wait(1);
}
}
//
If you have GHI drivers (perhaps from NETMF SDK), hereās a fragment of some very old sandboxing I was doing. I didnāt try to compile this, it may not; but, at one point it worked
Iām sorry in advance if this is clarified elsewhere, but I am trying to determine if the USBSerial connection requires that you have a separate USB connector to the PC (i.e. instead of the USB cable that I can use to connect directly to my mBuino)
Could anyone confirm if itās possible to use the USBSerial to communicate with the PC over the USB socket on the mBuino? (to the USB port on the PC)
(BTW, My application is to use the mBuino to echo input from the PC in morse code) - I have it working when I hard-code the text I want to āblinkā but I would actually prefer to be able to supply the mBuino with an ongoing stream of text and have it continuously blink out the text I send it.)
I have been trying to get this to work for a few days with very limited success. I see the COM port appear in the device manager, but I cannot get a puts(āanythingā) command to be seen in teraterm. I have both sides set to 9600 bps. The code is minimal.
IIRC, that was consistent with my experience. I spent a bit of time pulling my hair out trying to figure out why it wasnāt working, and then suddenly it was. Go figure.
However, teraterm only sees the port AFTER the code starts running. So, any initial prompt messages are never seen.
And, teraterm only sees the com port every OTHER time the mBuino is reset.
But still fine for debugging as long as nothing comes to the serial port until after the terminal program is running which can only happen after the mBuino is running. Just a bit of a Catch 22.
The āproblemā is that USB-serial ports are only enumerated on the PC when a device connects. That unfortunately is when the code initialises it on the microprocessor side.
My approach would be use a hardware USB-serial not the built in usb port. Means the PC will always see a serial port it can open, just no data arrives until the app opens the UART and sends. I like CP2102 based modules, cheap-as on ebay (buy several) and have an OK windows driver unlike some of the other cheapies.
Another approach can be sending a sync byte from the mbuino to the serial port until it gets a response and then it progresses to send real data
all depends on what type of problem you want to solveā¦
@ Blue Hair Bob - IIRC, I ended up having the mBuino wait a few seconds at startup to give the serial connection time to initialize. Might be worth a tryā¦
@ Blue Hair Bob - I have used the second solution @ Brett gave, at startup I wait for a character from the serial port before allowing the code to proceed, the once treaters is connect I just press a key on the keyboard and everything synchronizes.