The USBSerial interface is used to emulate a serial port over USB. You can use this serial port as an extra serial port or as a debug solution. It's also a great solution to easily communicate between your mbed and a computer.
The USB connector should be attached to
p31 (D+), p32 (D-) and GND for the LPC1768 and the LPC11U24
The on-board USB connector of the FRDM-KL25Z
Driver required on Windows!
On Windows, you need a configuration file. You can download this archive containing a .inf file. Extract it.
When you plug your USBSerial serial device, Windows will try to find an existing driver for it without success. After this step, go into the device manager to find the unknown device:
Right click on the device
Update driver software
Click on "Browse my computer for driver software"
Indicate the path of serial.inf extracted previously and click next.
Accept the warning and you should have a virtual port (called Mbed Virtual Serial Port in device manager) over USB!
As product_id and vendor_id are hardcoded in the .inf file, if you don't want to use default values, you will have to change them in your program AND in the .inf file.
#include "mbed.h"#include "USBSerial.h"//Virtual serial port over USB
USBSerial serial;
int main(void) {
while(1)
{
serial.printf("I am a virtual serial port\r\n");
wait(1);
}
}
In this example, the program waits a line on the virtual serial port. When it receives a line, it sends it to the usual mbed serial port (the one used to flash a new program) and to the virtual one.
USBSerial echo
#include "mbed.h"
#include "USBSerial.h"
//Virtual serial port over USB
USBSerial serial;
Serial pc(USBTX, USBRX);
int main(void) {
uint8_t buf[128];
while(1)
{
serial.scanf("%s", buf);
serial.printf("recv: %s", buf);
pc.printf("recv: %s\r\n", buf);
}
}
I am implementing a USB device with an isochronous endpoint (an Audio Class device). I think it is nearly there, but I am not getting any callbacks on EP3OUT (EP3_OUT_Callback), which I am using for the isochronous OUT endpoint.
Looking at page 226 of the LPC 1768 User Manual, isochronous endpoints are triggered on the FRAME interrupt, but the code in USBBusInterface_LPC17_LPC23.cpp is handling EP3 callbacks in EP_SLOW which is not used for isochronous endpoints.
What is the right way to change USBBusInterface_LPC17_LPC23.cpp so that EP3_OUT_callback is triggered on a FRAME interrupt?
I am implementing a USB device with an isochronous endpoint (an Audio Class device). I think it is nearly there, but I am not getting any callbacks on EP3OUT (EP3_OUT_Callback), which I am using for the isochronous OUT endpoint.
Looking at page 226 of the [[http://ics.nxp.com/support/documents/microcontrollers/pdf/user.manual.lpc17xx.pdf|LPC 1768 User Manual]], isochronous endpoints are triggered on the FRAME interrupt, but the code in USBBusInterface_LPC17_LPC23.cpp is handling EP3 callbacks in EP_SLOW which is not used for isochronous endpoints.
What is the right way to change USBBusInterface_LPC17_LPC23.cpp so that EP3_OUT_callback is triggered on a FRAME interrupt?
Isochronous endpoints are not very well integrated in the software for the moment. I think that the first thing to do is to activate FRAME interrupts in the constructor of USBHAL:
You can then use the virtual function SOF called on a frame interrupt. In this function try to read (non blocking) the isochronous endpoint. I am not sure that it works because I didn't tested but it's my first idea. I will do some experiment tomorrow concerning isochronous endpoints.
Hope that helps.
Sam
Isochronous endpoints are not very well integrated in the software for the moment. I think that the first thing to do is to activate FRAME interrupts in the constructor of USBHAL:
<<code>>
LPC_USB->USBDevIntEn = EP_SLOW | DEV_STAT | FRAME;
<</code>>
You can then use the virtual function SOF called on a frame interrupt. In this function try to read (non blocking) the isochronous endpoint. I am not sure that it works because I didn't tested but it's my first idea. I will do some experiment tomorrow concerning isochronous endpoints.
Hope that helps.
Sam
No on linux, you should see the serial port as /dev/ttyACMx. You can use a program like minicom to handle the serial port. If you just want to see data sent by the mbed, use:
cat /dev/ttyACMx
No on linux, you should see the serial port as /dev/ttyACMx. You can use a program like minicom to handle the serial port. If you just want to see data sent by the mbed, use:
<<code>>
cat /dev/ttyACMx
<</code>>
When I load this USBSerial_HelloWorld demo onto a mbed NXP LPC1768 (without a USB cable attached to a PC) the mbed hangs, any ideas as I'd like to use this library? Is there a way that I can test to see whether the USB connection is working rather than just hang?
Regards,
Zera
Hi,
When I load this USBSerial_HelloWorld demo onto a mbed NXP LPC1768 (without a USB cable attached to a PC) the mbed hangs, any ideas as I'd like to use this library? Is there a way that I can test to see whether the USB connection is working rather than just hang?
Regards,
Zera
The USBSerial constructor waits until the device is connected. So if the cable is not plugged, the mbed will hang.
You can see if the connection is working in the device manager on windows. I think that the best way to check the usb connection is USBMouse. If the cursor is moving on your screen, it means that the usb connection is correct.
Sam
Hi Zera,
The USBSerial constructor waits until the device is connected. So if the cable is not plugged, the mbed will hang.
You can see if the connection is working in the device manager on windows. I think that the best way to check the usb connection is [[USBMouse|USBMouse]]. If the cursor is moving on your screen, it means that the usb connection is correct.
Sam
Thanks for the swift reply. Is there anyway to test to see whether the USB cable is attached to a PC from the mBed's point-of-view? The reason I ask is, I'd like to impliment this USB port on a system that uses this connection as an option, i.e. not always connected. I don't want the system to hang if the connection is removed, pulled out, not desired or broken.
Thanks for your support in this area.
Zera
Hi Sam,
Thanks for the swift reply. Is there anyway to test to see whether the USB cable is attached to a PC from the mBed's point-of-view? The reason I ask is, I'd like to impliment this USB port on a system that uses this connection as an option, i.e. not always connected. I don't want the system to hang if the connection is removed, pulled out, not desired or broken.
Thanks for your support in this area.
Zera
@Zera: You can delete the connect() call in the constructor of USBCDC and call it whenever you want. The connect function is blocking and wait until the device is connected. To know if the usb cable is plugged or not, you can use the virtual function suspendStateChanged(int suspend) called from the USBHAL irq handler. If suspend = 0, there is an activity on the USB bus, otherwise there is no activity. I did some tests this morning and I can detect with this function if the cable is plugged or not.
@igor: You can send data at the speed of USB. I did some performance tests and I am able to send 9000 packets of 64bytes /s .
Sam
@Zera: You can delete the connect() call in the constructor of USBCDC and call it whenever you want. The connect function is blocking and wait until the device is connected. To know if the usb cable is plugged or not, you can use the virtual function suspendStateChanged(int suspend) called from the USBHAL irq handler. If suspend = 0, there is an activity on the USB bus, otherwise there is no activity. I did some tests this morning and I can detect with this function if the cable is plugged or not.
@igor: You can send data at the speed of USB. I did some performance tests and I am able to send 9000 packets of 64bytes /s .
Sam
What about DTR, CTS, RTS, and so on?
The software probably gets the char, but how do we read it?
This could be a nice little alternative to FT232RL (I know, that's the easy way of doing it), but
i wan't to be able to do more than that. Like program different MCU's and being the easy use of USB to serial.
This could be nice. I'll see if I can get the time to write something.
R'gards
Lerche
What about DTR, CTS, RTS, and so on?
The software probably gets the char, but how do we read it?
This could be a nice little alternative to FT232RL (I know, that's the easy way of doing it), but
i wan't to be able to do more than that. Like program different MCU's and being the easy use of USB to serial.
This could be nice. I'll see if I can get the time to write something.
R'gards
Lerche
For me it works on windows 7 64 bits with this .inf.
Have you tested on another OS ? Is there an error message ? Are you sure about your hardware (try a USBMouse to be sure that it is not a hardware problem for instance) ?
Sam
Hi Bob,
For me it works on windows 7 64 bits with this .inf.
Have you tested on another OS ? Is there an error message ? Are you sure about your hardware (try a USBMouse to be sure that it is not a hardware problem for instance) ?
Sam
I have problem with sending data from my C# program to mbed using USBSerial. I have tested a simple "USBSerial echo" mentioned above. Using a terminal program (TeraTerm), it works fine and I can write the strings and see the echoes; however, every time I want to write the same string to mbed with my C# program, I'm getting an error message that "The port is closed". This error does not make sense to me as any other program accessing 'Mbed Virtual Serial Port' is already terminated. Does anybody have an explanation for this?
I have problem with sending data from my C# program to mbed using USBSerial. I have tested a simple "USBSerial echo" mentioned above. Using a terminal program (TeraTerm), it works fine and I can write the strings and see the echoes; however, every time I want to write the same string to mbed with my C# program, I'm getting an error message that "The port is closed". This error does not make sense to me as any other program accessing 'Mbed Virtual Serial Port' is already terminated. Does anybody have an explanation for this?
Thanks for your reply. I'm sure that I'm writing to open the right COM port. How can I try with COM0?, as the Mbed Virtual Serial Port in my pc is COM 7.
Kamyar
Hi Wim,
Thanks for your reply. I'm sure that I'm writing to open the right COM port. How can I try with COM0?, as the Mbed Virtual Serial Port in my pc is COM 7.
Kamyar
I dont mean writing to mbed on com0 but just open another com port as a test of your code and hook up another pc to com 0 to check that it receives your data.
I dont mean writing to mbed on com0 but just open another com port as a test of your code and hook up another pc to com 0 to check that it receives your data.
On usual mbed serial port (COM6 on my pc), when I'm using RPCFunctions and rpc, my C# code is ok! I can send and recieve data. However, when I'm using Mbed Virtual Serial Port (COM 7 on my pc), I cannot recieve any data with my C# program facing "port is closed". I'm exactly using "USBSerial Hello World" in my mbed.
Kamyar
Wim,
On usual mbed serial port (COM6 on my pc), when I'm using RPCFunctions and rpc, my C# code is ok! I can send and recieve data. However, when I'm using Mbed Virtual Serial Port (COM 7 on my pc), I cannot recieve any data with my C# program facing "port is closed". I'm exactly using "USBSerial Hello World" in my mbed.
Kamyar
I have just done some tests and I am able to communicate with an mbed over USBSerial in C#.
On the mbed I am using the USBSerial Hello World and the C# program is:
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ser
{
class Program
{
static private SerialPort mbed;
static void Main(string[] args)
{
Console.WriteLine("Hello World !!");
mbed = new SerialPort("COM11");
OpenMbedSerialPort();
Console.WriteLine("BaudRate {0}", mbed.BaudRate);
while (true)
{
try
{
string message = mbed.ReadLine();
Console.WriteLine(message);
}
catch (Exception ex)
{
Console.WriteLine("Error trying to read: {0}", ex.Message);
}
}
}
private static void OpenMbedSerialPort()
{
try
{
mbed.Open();
}
catch (Exception ex)
{
Console.WriteLine("Error opening my port: {0}", ex.Message);
}
}
}
}
Can you try this code (adapt the COM number) ?
Sam
Hi Kamyar,
I have just done some tests and I am able to communicate with an mbed over USBSerial in C#.
On the mbed I am using the USBSerial Hello World and the C# program is:
<<code>>
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ser
{
class Program
{
static private SerialPort mbed;
static void Main(string[] args)
{
Console.WriteLine("Hello World !!");
mbed = new SerialPort("COM11");
OpenMbedSerialPort();
Console.WriteLine("BaudRate {0}", mbed.BaudRate);
while (true)
{
try
{
string message = mbed.ReadLine();
Console.WriteLine(message);
}
catch (Exception ex)
{
Console.WriteLine("Error trying to read: {0}", ex.Message);
}
}
}
private static void OpenMbedSerialPort()
{
try
{
mbed.Open();
}
catch (Exception ex)
{
Console.WriteLine("Error opening my port: {0}", ex.Message);
}
}
}
}
<</code>>
Can you try this code (adapt the COM number) ?
Sam
I've got the new LPC11U24-based mbed, and I've been able to get it to talk via serial to Mac OS X fine over the USB port. If I try with Win7, it only enumerates as a storage device, and does not enumerate as a virtual serial port, so you never get prompted to supply the inf file that is posted here.
Is it possible that the 11U24 mbed has a different default VID/PID that I need to take into account? Is there any way to force the VCP enumeration?
Thanks in advance...
Hi,
I've got the new LPC11U24-based mbed, and I've been able to get it to talk via serial to Mac OS X fine over the USB port. If I try with Win7, it only enumerates as a storage device, and does not enumerate as a virtual serial port, so you never get prompted to supply the inf file that is posted here.
Is it possible that the 11U24 mbed has a different default VID/PID that I need to take into account? Is there any way to force the VCP enumeration?
Thanks in advance...
Is it possible that the 11U24 mbed has a different default VID/PID that I need to take into account?
VID and PID are the same.
Can you try to go into the device manager to find the new device (apparently storage device), then:
Right click on the device
Update driver software
Click on "Browse my computer for driver software"
Indicate the path of serial.inf extracted previously and click next.
Accept the warning and install the driver
Sam
Hi Tim,
<<quote>>
Is it possible that the 11U24 mbed has a different default VID/PID that I need to take into account?
<</quote>>
VID and PID are the same.
Can you try to go into the device manager to find the new device (apparently storage device), then:
* Right click on the device
* Update driver software
* Click on "Browse my computer for driver software"
* Indicate the path of serial.inf extracted previously and click next.
* Accept the warning and install the driver
Sam
Thanks very much. Your code is working fine! but I have some questions. First of all, can we change the baud rate to 921600 with:
</code>
mbed = new SerialPort("COM7", 921600);
</code>
I did that and I have not seen any change in the COM speed?!
I need to send a bulk of data to mbed with the highest speed possible. What is the maximum baud rate the mbed could take? I could not get more than 921600 baud in my experience! In the technical sheet, it is mentioned that it follows USB 2.0 standard so I assume it should go up to 12Mbps!
Can we use "USBSerial" with RPC functions? I have used RPC functions on the usual mbed serial port and there are codes available in here to realize that but the problem is if we want to send consecutive RPC commands (in order to send some data) to mbed with the highest baud rate, you have to introduce delay between each commnad which is not desirable and slowing down the communication! Can it be the same case with USBSerial?
I can see that a circular buffer (CircBuffer.h) is used for USBSerial. How can we change the size of this buffer to customize it for your application?
Kamyar
Sam,
Thanks very much. Your code is working fine! but I have some questions. First of all, can we change the baud rate to 921600 with:
</code>
mbed = new SerialPort("COM7", 921600);
</code>
I did that and I have not seen any change in the COM speed?!
I need to send a bulk of data to mbed with the highest speed possible. What is the maximum baud rate the mbed could take? I could not get more than 921600 baud in my experience! In the technical sheet, it is mentioned that it follows USB 2.0 standard so I assume it should go up to 12Mbps!
Can we use "USBSerial" with RPC functions? I have used RPC functions on the usual mbed serial port and there are codes available in here to realize that but the problem is if we want to send consecutive RPC commands (in order to send some data) to mbed with the highest baud rate, you have to introduce delay between each commnad which is not desirable and slowing down the communication! Can it be the same case with USBSerial?
I can see that a circular buffer (CircBuffer.h) is used for USBSerial. How can we change the size of this buffer to customize it for your application?
Kamyar
The baudrate does not have an influence. Data are transmitted at USB speed.
12Mbps is the theory. In practice, I never managed to reach this bandwidth. But I noticed differences in the bandwidth according the size of packets sent:
send 64 bytes packets (size of the endpoint) to have a better bandwidth
I have never tried RPC functions with USBSerial but I assume that it works as with the usual Serial.
You can modify the size of the circular buffer in USBSerial.h (adapt buf(128) to your need):
Hi Kamyar,
The baudrate does not have an influence. Data are transmitted at USB speed.
12Mbps is the theory. In practice, I never managed to reach this bandwidth. But I noticed differences in the bandwidth according the size of packets sent:
* send 64 bytes packets (size of the endpoint) to have a better bandwidth
I have never tried RPC functions with USBSerial but I assume that it works as with the usual Serial.
You can modify the size of the circular buffer in USBSerial.h (adapt buf(128) to your need):
<<code>>
USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001): USBCDC(vendor_id, product_id, product_release), buf(128){ };
<</code>>
Hope that helps
Sam
Thanks for your response. As I have to send packets with variable sizes from my PC to mbed, what is the maximum packet size you can go with "buf(128)" above. Although the transition goes with USB speed, what speed you should expect (in your experience) if you are sending 64 bytes packets? I can see 75ms delay between each read and write on my oscilloscope! If you employ USB 3.0, would it be a great difference?
Kamyar
Hi Sam,
Thanks for your response. As I have to send packets with variable sizes from my PC to mbed, what is the maximum packet size you can go with "buf(128)" above. Although the transition goes with USB speed, what speed you should expect (in your experience) if you are sending 64 bytes packets? I can see 75ms delay between each read and write on my oscilloscope! If you employ USB 3.0, would it be a great difference?
Kamyar
I need to read from a system which seems to use a FT232RL chip. Which is a chip that converts RX TX signals to USB. I am supposed to receive int/chars from it.
So I went ahead and used this library to connect the mbed to the chip using the D-, D+, Vin pins on the mbed.
I wrote a small program that would light an LED on the mbed after it got past the constructor.
It didn't work, seems like it got stuck at the constructor like Samuel pointed out earlier in this thread.
Next I figured out the VID and PID of the FT232RL, which are 0x0403 and 0x6001 respectively.
So I changed those values in the USBSerial library. and tried the process again, but it still hangs at the constructor...
Am I at the wrong library? Anyone got mbed working with FT232RL?
I added a bit of the code, but I kept it really simple as you can see.
#include "mbed.h"
#include "USBSerial.h"
//Virtual serial port over USB
DigitalOut myled(LED1);
USBSerial serial;
int main(void) {
while(1)
{
wait(0.5);
myled = 0;
wait(0.5);
myled = 1;
}
}
Gr.
D
Hi guys,
I need to read from a system which seems to use a FT232RL chip. Which is a chip that converts RX TX signals to USB. I am supposed to receive int/chars from it.
So I went ahead and used this library to connect the mbed to the chip using the D-, D+, Vin pins on the mbed.
I wrote a small program that would light an LED on the mbed after it got past the constructor.
It didn't work, seems like it got stuck at the constructor like Samuel pointed out earlier in this thread.
Next I figured out the VID and PID of the FT232RL, which are 0x0403 and 0x6001 respectively.
So I changed those values in the USBSerial library. and tried the process again, but it still hangs at the constructor...
Am I at the wrong library? Anyone got mbed working with FT232RL?
I added a bit of the code, but I kept it really simple as you can see.
<<code>>
#include "mbed.h"
#include "USBSerial.h"
//Virtual serial port over USB
DigitalOut myled(LED1);
USBSerial serial;
int main(void) {
while(1)
{
wait(0.5);
myled = 0;
wait(0.5);
myled = 1;
}
}
<</code>>
Gr.
D
@Kamyar: I did some experiments. I am able to send 9000 packets of 64 bytes/s from the mbed using the serial.writeBlock function. Be sure to delete all printf, ... because it is very slow otherwise. For the reception, I can receive about 1500 packets/s (callback attached when a message is received). Theoretically, USB 3.0 should be a big difference (5Gbit/s!!). I can't wait to test USB 3.0.
@d asao: You are not using the right library for your need. Indeed, the FTDI chip is already a USB device which has to be connected to a USB host. If you want, the USBSerial library "emulates" a FTDI chip. So it's normal that it does not work. For the moment mbed is not supporting an official USB Host library but a good beginning can be to explore: BlueUSB
Hope that helps,
Sam
Hi,
@Kamyar: I did some experiments. I am able to send 9000 packets of 64 bytes/s from the mbed using the serial.writeBlock function. Be sure to delete all printf, ... because it is very slow otherwise. For the reception, I can receive about 1500 packets/s (callback attached when a message is received). Theoretically, USB 3.0 should be a big difference (5Gbit/s!!). I can't wait to test USB 3.0.
@d asao: You are not using the right library for your need. Indeed, the FTDI chip is already a USB device which has to be connected to a USB host. If you want, the USBSerial library "emulates" a FTDI chip. So it's normal that it does not work. For the moment mbed is not supporting an official USB Host library but a good beginning can be to explore: [[http://mbed.org/users/peterbarrett1967/notebook/blueusb---bluetooth-and-usb-host-controller-for-mb/| BlueUSB]]
Hope that helps,
Sam
Thanks for your reply. But can you tell me can mbed (I'm working LXP1768) work with USB3.0 straight away or it just can work with USB2.0 standard?
Kamyar
Hi Sam,
Thanks for your reply. But can you tell me can mbed (I'm working LXP1768) work with USB3.0 straight away or it just can work with USB2.0 standard?
Kamyar
I have just tested my mbed with USB3.0 out of curiosity! The fact is sometime it works and I can read and write block of data very fast and soemtimes it just freezes. I don't know the exact reason why the communication is being freezed but as you said it seems that mbed cannot handle USB3.0 at the moment. Too sad, as I desparately need it.
Another point, can you please send me the program that you use for testing the speed of sending and recieving! You mentioned "callback attached when a message is received"! I don't know how to use that function. Is this function is actually an interrupt fucntion, letting you know when a packet is recieved by mbed? If this the case, can we time this function. I mean we just recieve data at certain period of time (for instance every 500us). My problem is that when sending block of data in a looping manner from PC to mbed, the speed of each PC is different in sending the data, and on the top of that the mbed cannot recieve each packet exactly at the same interval. Any thoughts to get around this problem?
Kamyar
Hi Sam,
I have just tested my mbed with USB3.0 out of curiosity! The fact is sometime it works and I can read and write block of data very fast and soemtimes it just freezes. I don't know the exact reason why the communication is being freezed but as you said it seems that mbed cannot handle USB3.0 at the moment. Too sad, as I desparately need it.
Another point, can you please send me the program that you use for testing the speed of sending and recieving! You mentioned "callback attached when a message is received"! I don't know how to use that function. Is this function is actually an interrupt fucntion, letting you know when a packet is recieved by mbed? If this the case, can we time this function. I mean we just recieve data at certain period of time (for instance every 500us). My problem is that when sending block of data in a looping manner from PC to mbed, the speed of each PC is different in sending the data, and on the top of that the mbed cannot recieve each packet exactly at the same interval. Any thoughts to get around this problem?
Kamyar
You can delete the connect() call in the constructor of USBCDC and call it whenever you want. The connect function is blocking and wait until the device is connected. To know if the usb cable is plugged or not, you can use the virtual function suspendStateChanged(int suspend) called from the USBHAL irq handler. If suspend = 0, there is an activity on the USB bus, otherwise there is no activity. I did some tests this morning and I can detect with this function if the cable is plugged or not.
@Samuel: Could you, please, explane it a bit. suspendStateChanged...
<<quote>>
You can delete the connect() call in the constructor of USBCDC and call it whenever you want. The connect function is blocking and wait until the device is connected. To know if the usb cable is plugged or not, you can use the virtual function suspendStateChanged(int suspend) called from the USBHAL irq handler. If suspend = 0, there is an activity on the USB bus, otherwise there is no activity. I did some tests this morning and I can detect with this function if the cable is plugged or not.
<</quote>>
@Samuel: Could you, please, explane it a bit. suspendStateChanged...
" Undefined symbol USBHAL::stallEndpoint(unsigned char) (referred from USBDevice.cpp.cpp.LPC2368.o)." in file "/"
how to fix this.
hi samuel,
i am getting this error during compile
" Undefined symbol USBHAL::stallEndpoint(unsigned char) (referred from USBDevice.cpp.cpp.LPC2368.o)." in file "/"
how to fix this.
Hello.
Is it possible to have several "virtual serial ports" on the single USB port?
I tried to declare 2 serial ports, but on windows XP, I see only one USB port.
Is there a way to do this?
Thank you for your answer!
Hello.
Is it possible to have several "virtual serial ports" on the single USB port?
I tried to declare 2 serial ports, but on windows XP, I see only one USB port.
Is there a way to do this?
Thank you for your answer!
@Goran: If there is no activity on the USB port for more than 3 ms, the suspend bit is set to 1. suspendStateChanged(int) is a virtual function called in the irq handler. You can then override this virtual function in a daughter class to detect any activity on the USB bus (for instance, you can detect if the USB cable is plugged or not). For more information, you can see p247 of http://www.nxp.com/documents/user_manual/UM10360.pdf
@shahid: the USB device library is available for the LPC11U24 and the LPC1768, not for the LPC2368.
@Leon: You cannot instantiate two virtual serial port. You need to modify the configuration descriptor and mix two serial port on it. So that there is only one USB device with two serial port.
Sam
Hi All,
@Goran: If there is no activity on the USB port for more than 3 ms, the suspend bit is set to 1. suspendStateChanged(int) is a virtual function called in the irq handler. You can then override this virtual function in a daughter class to detect any activity on the USB bus (for instance, you can detect if the USB cable is plugged or not). For more information, you can see p247 of [[http://www.nxp.com/documents/user_manual/UM10360.pdf]]
@shahid: the USB device library is available for the LPC11U24 and the LPC1768, not for the LPC2368.
@Leon: You cannot instantiate two virtual serial port. You need to modify the configuration descriptor and mix two serial port on it. So that there is only one USB device with two serial port.
Sam
Hi, I have one more question about USBSERIAL:
Is there any flow control on this "virtual serial port"? I understand that with LPC1768's USB port linked to the PC, there is no actual serial port (unlike the USB debug serial port). So if there is some kind of flow control, it is done via software.
I have to transfer data at high speed in 2 directions between PC and Mbed, so I think I need flow control.
Thanks for your anser.
Leon.
Hi, I have one more question about USBSERIAL:
Is there any flow control on this "virtual serial port"? I understand that with LPC1768's USB port linked to the PC, there is no actual serial port (unlike the USB debug serial port). So if there is some kind of flow control, it is done via software.
I have to transfer data at high speed in 2 directions between PC and Mbed, so I think I need flow control.
Thanks for your anser.
Leon.
@Christopher: This is probably a hardware problem. Check your wirings. You have an example here
@Leon: There is no flow control in USBSerial. But I think that there is an intrinsic flow control with USB. For instance, if the host wants to send a packet and the previous one has not been read by the device, the host will retry to send it.
Sam
Hi,
@Christopher: This is probably a hardware problem. Check your wirings. You have an example [[http://mbed.org/handbook/USBDevice| here]]
@Leon: There is no flow control in USBSerial. But I think that there is an intrinsic flow control with USB. For instance, if the host wants to send a packet and the previous one has not been read by the device, the host will retry to send it.
Sam
I'm using this serial USB for a bidirectional communication channel between the mbed module and my C++ application running on my PC under Visual Studio.
On the C++ application side I'm using the COM port in the Overlaped (Async) mode.
It all works, but than, after few (30) seconds the mbed hangs until I reset it.
When I'm running the same mbed code but while replacing my application with a terminal application running on the PC (RealTerm) and I even simulate transmission of messages towards the mbed while it sends a massive messages stream to the PC, it all runs perfectlly and the mbed want hang.
It is also important to note that my C++ application perfrctly works when I'm using the mbed debug serial usb interface.
My question is: what am I might be doing wrong on my C++ application that may cause the mbed side to hang forever?
For example, will closing the port while the mbed sends a message will cause it?
Any clue that will focus my search on the debug of this irritating issue will be appreciated.
Thanks,
Nahum Budin,
Hi,
I'm using this serial USB for a bidirectional communication channel between the mbed module and my C++ application running on my PC under Visual Studio.
On the C++ application side I'm using the COM port in the Overlaped (Async) mode.
It all works, but than, after few (~30) seconds the mbed hangs until I reset it.
When I'm running the same mbed code but while replacing my application with a terminal application running on the PC (RealTerm) and I even simulate transmission of messages towards the mbed while it sends a massive messages stream to the PC, it all runs perfectlly and the mbed want hang.
It is also important to note that my C++ application perfrctly works when I'm using the mbed debug serial usb interface.
My question is: what am I might be doing wrong on my C++ application that may cause the mbed side to hang forever?
For example, will closing the port while the mbed sends a message will cause it?
Any clue that will focus my search on the debug of this irritating issue will be appreciated.
Thanks,
Nahum Budin,
Make sure you're not exiting the function which is reading/sending to/from the serial communication
otherwise it would apear to do nothing.
@ Nahm Budin
Make sure you're not exiting the function which is reading/sending to/from the serial communication
otherwise it would apear to do nothing.
#include "mbed.h"
#include "USBSerial.h"
//Virtual serial port over USB
USBSerial serial;
int main(void)
{
char buf[20];
while(1)
{
serial.scanf("%s", buf);
if(buf??????????? == "Hello")
{
serial.printf("%s\r\n", buf);
}
}
}
How do i grab the value for an if statement? Look at the code
<<code>>
#include "mbed.h"
#include "USBSerial.h"
//Virtual serial port over USB
USBSerial serial;
int main(void)
{
char buf[20];
while(1)
{
serial.scanf("%s", buf);
if(buf??????????? == "Hello")
{
serial.printf("%s\r\n", buf);
}
}
}
<</code>>
How do i grab the value for an if statement? Look at the code
You are probably looking for the strcmp() function. It compares two strings and returns 0 if they are considered equal. So your code would look like:
#include "mbed.h"
#include "USBSerial.h"
//Virtual serial port over USB
USBSerial serial;
int main(void) {
char buf[20];
while (1) {
serial.scanf("%s", buf);
if (strcmp("Hello", buf) == 0) {
serial.printf("%s\r\n", buf);
}
}
}
To use the real UART on the mbed that is converted to a virtual serial port and available over the same USB connection as you use to program (as found in SerialPC, you could also do:
#include "mbed.h"
Serial serial(USBTX, USBRX);
int main(void) {
char buf[20];
while (1) {
serial.scanf("%s", buf);
if (strcmp("Hello", buf) == 0) {
serial.printf("%s\r\n", buf);
}
}
}
Hope that helps,
Simon
Hi Matthew,
You are probably looking for the [[http://www.cplusplus.com/reference/clibrary/cstring/strcmp/|{{{strcmp()}}}]] function. It compares two strings and returns 0 if they are considered equal. So your code would look like:
<<code>>
#include "mbed.h"
#include "USBSerial.h"
//Virtual serial port over USB
USBSerial serial;
int main(void) {
char buf[20];
while (1) {
serial.scanf("%s", buf);
if (strcmp("Hello", buf) == 0) {
serial.printf("%s\r\n", buf);
}
}
}
<</code>>
To use the real UART on the mbed that is converted to a virtual serial port and available over the same USB connection as you use to program (as found in [[http://mbed.org/handbook/SerialPC|SerialPC]], you could also do:
<<code>>
#include "mbed.h"
Serial serial(USBTX, USBRX);
int main(void) {
char buf[20];
while (1) {
serial.scanf("%s", buf);
if (strcmp("Hello", buf) == 0) {
serial.printf("%s\r\n", buf);
}
}
}
<</code>>
Hope that helps,
Simon
include "mbed.h" #include "USBSerial.h" Virtual serial port over USB USBSerial serial; int main(void) { char buf[20]; while (1) { serial.scanf("%s", buf); if (strcmp("Hello", buf) == 0) { serial.printf("%s\r\n", buf); } } }
FINALLY! someone with great understanding!
Thank you Simon :)
<<quote simon>>
#include "mbed.h" #include "USBSerial.h" //Virtual serial port over USB USBSerial serial; int main(void) { char buf[20]; while (1) { serial.scanf("%s", buf); if (strcmp("Hello", buf) == 0) { serial.printf("%s\r\n", buf); } } }
<</quote>>
FINALLY! someone with great understanding!
Thank you Simon :)
Using the same post you posted above, here is a fictional scinario:
Ok so we receive the variable from serial to the buffer: serial.scanf("%s", buf);
The buffer contains this variable sent from serial: X2,Y1394
NOTE: varied sizes such as X2978,Y1394 <- which will be the maximum length of 11 and a minimum length of 5
and parse it into two string buffers of their own example: buffer1 = 2 and buffer2 = 1394
So then i would put them into here like this: key_mouse.move(buffer1, buffer2);
Can you make a sample? Thanks Simon :D
Another question
Using the same post you posted above, here is a fictional scinario:
Ok so we receive the variable from serial to the buffer: serial.scanf("%s", buf);
The buffer contains this variable sent from serial: X2,Y1394
NOTE: varied sizes such as X2978,Y1394 <- which will be the maximum length of 11 and a minimum length of 5
and parse it into two string buffers of their own example: buffer1 = 2 and buffer2 = 1394
So then i would put them into here like this: key_mouse.move(buffer1, buffer2);
Can you make a sample? Thanks Simon :D
#include "mbed.h"
#include "USBMouseKeyboard.h"
USBMouseKeyboard key_mouse;
Serial pc(USBTX, USBRX);
#define NFIELDS (6)
char* pFields[NFIELDS];
void ParseCommands(char* Buffer, char** pFields, uint32_t numFields, char* delimiter) {
char* pString = Buffer;
char* pField;
for (uint32_t i=0; i<numFields; i++) {
pField = strtok(pString, delimiter);
if (pField != NULL) {
pFields[i] = pField;
} else {
pFields[i] = "";
}
pString = NULL; //parse next
}
if (strcmp("Vector", pFields[0]) == 0) {
int x;
int y;
x = atoi (pFields[1]);
y = atoi (pFields[2]);
key_mouse.move(x,y);
}
if (strcmp("Click", pFields[0]) == 0) {
if (strcmp("LPress", pFields[1]) == 0) {
key_mouse.press(MOUSE_LEFT);
}
if (strcmp("LRelease", pFields[1]) == 0) {
key_mouse.release(MOUSE_LEFT);
}
if (strcmp("LClick", pFields[1]) == 0) {
key_mouse.click(MOUSE_LEFT);
}
if (strcmp("RPress", pFields[1]) == 0) {
key_mouse.press(MOUSE_RIGHT);
}
if (strcmp("RRelease", pFields[1]) == 0) {
key_mouse.release(MOUSE_RIGHT);
}
if (strcmp("RClick", pFields[1]) == 0) {
key_mouse.click(MOUSE_RIGHT);
}
pc.printf("Click Issued\r\n");
}
if (strcmp("Keyboard", pFields[0]) == 0) {
key_mouse.printf(pFields[1]);
pc.printf("Keyboard Issued\r\n");
}
}
int main(int argc, char* argv[]) {
pc.baud(9600);
char buf[64];
while (1) {
//Vector,x,y
//Click,LClick //Example: RClick or LPress or LRelease or RPress or RRelease
//Keyboard,sTrInG //Example: Keyboard,Hello_World //Ignores Spaces and trailing words after a space
pc.scanf("%s", buf);
ParseCommands(buf, pFields, NFIELDS, ",");
}
}
It's mostly complete, the function serial commands are in the Main Event commented out
Ok i solved it, i realised the values were integer, not strings and so i converted the chars to integer and it works!
Check this out: [[http://mbed.org/compiler/?import=http://mbed.org/users/Elitism/programs/KeyboardMouseSerialTest/mbfof3]]
Here is the code:
<<code>>
#include "mbed.h"
#include "USBMouseKeyboard.h"
USBMouseKeyboard key_mouse;
Serial pc(USBTX, USBRX);
#define NFIELDS (6)
char* pFields[NFIELDS];
void ParseCommands(char* Buffer, char** pFields, uint32_t numFields, char* delimiter) {
char* pString = Buffer;
char* pField;
for (uint32_t i=0; i<numFields; i++) {
pField = strtok(pString, delimiter);
if (pField != NULL) {
pFields[i] = pField;
} else {
pFields[i] = "";
}
pString = NULL; //parse next
}
if (strcmp("Vector", pFields[0]) == 0) {
int x;
int y;
x = atoi (pFields[1]);
y = atoi (pFields[2]);
key_mouse.move(x,y);
}
if (strcmp("Click", pFields[0]) == 0) {
if (strcmp("LPress", pFields[1]) == 0) {
key_mouse.press(MOUSE_LEFT);
}
if (strcmp("LRelease", pFields[1]) == 0) {
key_mouse.release(MOUSE_LEFT);
}
if (strcmp("LClick", pFields[1]) == 0) {
key_mouse.click(MOUSE_LEFT);
}
if (strcmp("RPress", pFields[1]) == 0) {
key_mouse.press(MOUSE_RIGHT);
}
if (strcmp("RRelease", pFields[1]) == 0) {
key_mouse.release(MOUSE_RIGHT);
}
if (strcmp("RClick", pFields[1]) == 0) {
key_mouse.click(MOUSE_RIGHT);
}
pc.printf("Click Issued\r\n");
}
if (strcmp("Keyboard", pFields[0]) == 0) {
key_mouse.printf(pFields[1]);
pc.printf("Keyboard Issued\r\n");
}
}
int main(int argc, char* argv[]) {
pc.baud(9600);
char buf[64];
while (1) {
//Vector,x,y
//Click,LClick //Example: RClick or LPress or LRelease or RPress or RRelease
//Keyboard,sTrInG //Example: Keyboard,Hello_World //Ignores Spaces and trailing words after a space
pc.scanf("%s", buf);
ParseCommands(buf, pFields, NFIELDS, ",");
}
}
<</code>>
It's mostly complete, the function serial commands are in the Main Event commented out
Think of a hireachi type tree, If you're in a loop can only implicitly run one section of code unless you break out it.
Idk if mbed supports threading but if it does it will come in handy i'm sure.
@Nahum Budin
Think of a hireachi type tree, If you're in a loop can only implicitly run one section of code unless you break out it.
Idk if mbed supports threading but if it does it will come in handy i'm sure.
These two use the same USB connection which is the EXT connection by pins
But there is a problem! I Can't use BOTH, it will only show the one that is defined last which is USBSerial serial;
If i put USBMouseKeyboard Bellow USBSerial my computer will see the keyboard and mouse but not the serial port
If i put USBSerial Bellow USBMouseKeyboard my computer will see the serial port but not the keyboard and mouse
Is there a way to use both in the same connection?
Please respond promptly to this query!
Is there any way to use this with the mouse_keyboard usb connection also?
when i put both in the cpp the last defined device is used
<<code>>
#include "mbed.h"
#include "USBMouseKeyboard.h"
#include "USBSerial.h"
USBMouseKeyboard key_mouse;
USBSerial serial;
<</code>>
These two use the same USB connection which is the EXT connection by pins
But there is a problem! I Can't use BOTH, it will only show the one that is defined last which is USBSerial serial;
If i put USBMouseKeyboard Bellow USBSerial my computer will see the keyboard and mouse but not the serial port
If i put USBSerial Bellow USBMouseKeyboard my computer will see the serial port but not the keyboard and mouse
Is there a way to use both in the same connection?
Please respond promptly to this query!
I wrote an mbed program which uses USBserial.
It should send data at a high rate over USB. However, it makes even Realterm hang, and I don't know why:
The following code WORKS:
while (1)
{
serial.printf("Hallo\r\n");
wait(1.0);
}
The following code DOESN'T WORK:
while (1)
{
serial.printf("Hallo\r\n");
wait(0.001);
}
Does anybody have an idea why?
I also tried the ticker sample and modified the "flip" routine. It should print something every 2.0 seconds. Result: Realterm hangs again. I am using WIndows 8 Consumer preview, but I don't think that is the issue.
Regards & Thank you for any suggestion
Tobias
Hi,
I noticed a bug with USBserial:
I wrote an mbed program which uses USBserial.
It should send data at a high rate over USB. However, it makes even Realterm hang, and I don't know why:
The following code WORKS:
<<code>>
while (1)
{
serial.printf("Hallo\r\n");
wait(1.0);
}
<</code>>
The following code DOESN'T WORK:
<<code>>
while (1)
{
serial.printf("Hallo\r\n");
wait(0.001);
}
<</code>>
Does anybody have an idea why?
I also tried the ticker sample and modified the "flip" routine. It should print something every 2.0 seconds. Result: Realterm hangs again. I am using WIndows 8 Consumer preview, but I don't think that is the issue.
Regards & Thank you for any suggestion
Tobias
Hi. Let me see if I'm making the this correctly.
My mBed is fed by an external power supply (5V) between Vin and GND.
I made the download USBSerial_HelloWorld to send a message every second.
An external USB connector (B type) was connected as follows:
- Pin 1 on mBed pin 39;
- Pin 2 on mBed pin 32;
- Pin 3 on mBed pin 31;
- Pin 4 on mBed pin 1;
From this USB connector, a cable was connected to PC USB port, but no virtual serial port was created.
But, connecting the mBed to PC via mBed mini USB connector, the serial port appears.
Is that correct?
Thanks.
Hi. Let me see if I'm making the this correctly.
My mBed is fed by an external power supply (5V) between Vin and GND.
I made the download USBSerial_HelloWorld to send a message every second.
An external USB connector (B type) was connected as follows:
- Pin 1 on mBed pin 39;
- Pin 2 on mBed pin 32;
- Pin 3 on mBed pin 31;
- Pin 4 on mBed pin 1;
From this USB connector, a cable was connected to PC USB port, but no virtual serial port was created.
But, connecting the mBed to PC via mBed mini USB connector, the serial port appears.
Is that correct?
Thanks.
I assume that by pin 1 on USB B You mean VCC pin. This pin is used to power USB device so this one can be connected to VIN pin on mbed only.
Pin 39 (VUSB) on mbed is 5V voltage output and You can use it to power other 5V devices. Also when You use mbed as USB host, You can use this pin to power USB devices connected to mbed. Voltage on this pin and also on pin 40 (VOUT) is available only when mbed is powered viat it's USB mini B connector.
So when You want to power mbed via external source, You don't need to connect VCC pin on USB B connector to mbed. Just D+, D- and GND.
I hope this will help You.
Hello Wanderson,
I assume that by pin 1 on USB B You mean VCC pin. This pin is used to power USB device so this one can be connected to VIN pin on mbed only.
Pin 39 (VUSB) on mbed is 5V voltage output and You can use it to power other 5V devices. Also when You use mbed as USB host, You can use this pin to power USB devices connected to mbed. Voltage on this pin and also on pin 40 (VOUT) is available only when mbed is powered viat it's USB mini B connector.
So when You want to power mbed via external source, You don't need to connect VCC pin on USB B connector to mbed. Just D+, D- and GND.
I hope this will help You.
Please login to post comments.