GHI mBuino starter code for a USB Virtual Serial Port

First off, I would suggest, if you have Windows 8 or 8.1 - Be patient!

Be able to accept or tolerate delays, problems, or suffering without becoming annoyed or anxious. (Two shots of a good Irish whisky helps)

See the following:

  1. Shows all the steps required to allow install of unsigned driver on Windows 8/8.1
    http://www.howtogeek.com/167723/how-to-disable-driver-signature-verification-on-64-bit-windows-8.1-so-that-you-can-install-unsigned-drivers/

  2. For the mbed windows driver
    Windows serial configuration - Handbook | Mbed

  3. Additional info
    SerialPC - Handbook | Mbed

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);
     }
}
//


@ willgeorge - Not being able to see Virtual COM Port without using in in the application code is by design.

USBSerial is not part of the loader, so in order to see it you have to instantiate it in your code.

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 :wink:

#include "mbed.h"
 #include "USBSerial.h"

int main(void) {

    uint8_t buf[128];

    USBSerial serial((uint16_t)0x1B9F  , (uint16_t)0x0104, (uint16_t)0x0001, false); // ghi bootloader VID/DIV       
    
    while(1)
    {
        serial.printf("Press X to start xmodem transfer\r\n");
        
        buf[0] = serial._getc();
		
		// Command Processing
        if ('X' == buf[0]) {
           
            serial.printf("saw X\r");
            
        }
         while(0xd != (buf[0] = serial._getc()) ) {
                serial.putc(buf[0]);
                wait(0.01);
         }
         serial.putc(buf[0]);
         serial.putc(0xa);

         wait(.50);
        
    }
}

I was running Teraterm on the PC side.

Hello -

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.)

Thanks in advance.

Blair

@ blairh - No, you do not need a separate USB connection to use USBSerial.

Welcome to the community!

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.

#include "mbed.h"
 #include "USBSerial.h"
USBSerial serial; 
main()
{
    DigitalOut led1(LED1, false);
    serial.printf("\r\nHello Bob\r\n");
    //char c = serial.getc(); // wait for keyboard input
    while (true) {
        led1 = !led1;
        serial.putc('-');
        wait(.5);
    }
}

Any hints?

@ Blue Hair Bob -

You installed the driver?

I did. So I do see the com port (as COM5) in the device list.

WAIT - I got it! Today it works. Yesterday it did not, but right now it does! Beauty (or Crikey as other friends might say).

1 Like

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. :slight_smile:

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.

Anyone have a solution?

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ā€¦

2 Likes

@ 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.

3 Likes

Yep, +1 one here. I am using the same technique.


serial.scanf("%s", buffer);

2 Likes

I hate typing on the iPad, ā€˜treatersā€™ was teraterm when I originally typed it, I promise :slight_smile: