Home | Products | Teensy | Blog | Forum |
Oct 25, 2013 Hi, I have a USB CDC (communications device class) device (Atmel microcontroller) that I want to communicate with via USB. This device implements the CDC services that are required to make a simple serial connection and under XP the driver was simply an INF which instructed the OS to create a virtual serial port using the standard USB drivers. HHD Software Virtual Serial Ports is a software package that allows you to create virtual COM ports. Created virtual COM port may be used in one of the following ways: Connected with another virtual COM port in a local bridge with flow control and baud rate emulation.
You are here:TeensyCode LibraryUSB Serial |
| This code implements a virtual serial port, which is compatible withterminal emulators and programs that perform serial communication. On Windows, Microsoft includes a driver, but an INF must be specifiedduring 'detect new hardware' to make Windows load it. Linux and Macintosh OS Xprovide drivers that load automatically. Download Files
Example ApplicationA interactive shell that allows port pins to be accessed isincluded as an example. It can be used via a terminal emulator, likeHyperterminal on Windows.
Operating System SetupOn Windows, this Windows Driver Installer is needed. On Vista or Windows 7, right click and choose 'Run as administrator'.After installation, Windows 7 will should work automatically. Windows XP and Vistawill still show the New Hardware Wizard, but it will be able to find the driverwith the default choices.On Linux, this UDEV Rule File is needed to givenon-root users permission to use the device. On Mac OS-X, the drivers just work. Snow Leopard may show an unnecessarynetwork/modem setup window the first time you connect. Just close it. Receive Data Functions#include <usb_serial.h>usb_serial_available()How many characters are waiting in the receive buffer?This function returns the number of buffered bytes, or 0 if nothingis in the receive buffer. Double buffering is used, and this numberonly represents the first buffer, so in the case of non-zero return,additional bytes may be waiting in the second buffer but will notbecome visible with this function until the first buffer is fullyconsumed. usb_serial_getchar()Receive a character.The next byte is returned (0 to 255), or -1 if an error or timeout occurs.If you wish to avoid waiting, usb_serial_available() should be calledto verify at least 1 character is buffered so this function will returnimmediately. usb_serial_flush_input()Discard any buffered bytes that have not been read.Transmit Data Functions#include <usb_serial.h>usb_serial_putchar(character)Transmit a single character.0 is returned if your characterwas transmitted successfully, or -1 if on timeout or error. A timeout is implemented, so this function will always return.Subsequent calls after a timeout will NOT wait for a second timeout,but will immediately return with an error if transmission is notpossible. This feature protects against lengthy delays when longstrings of characters are transmitted without monitoring the returnvalue. Only the first will wait forthe timeout, all subsequent calls will not wait. Of course, whendata transfer is possible again, your character is sent and timeoutchecking is reset to normal. usb_serial_write(buffer, size)Transmit a block of data.0 returned on success, -1 on error. This function is optimized for speed. Writing 64 byte blockscan achieve nearly the maximum possible USB speed. usb_serial_putchar_nowait(character)Transmit a single character.0 is returned if your characterwas transmitted successfully, or -1 if on error. This function always returns immediately. There is no timeout,so an error is returned if there is no buffer space available. usb_serial_flush_output()Transmit any buffered data as soon as possible.Buffering in the USB controller is used to maximize throughput andminimize impact on your program's execution speed. Buffered data isautomatically transmitted to the PC when your program does not perform morewrites after a brief timeout, so normally this function is not necessary. If you want to transmit all buffered data as soon as possible, this function causes any data buffered in the USB controller to besent. Actual data USB transfer is always managedby the USB host (on your PC or Macintosh), so this function typicallyreturns while data is still buffered, but will be transfered as soonas possible. Serial Paramater Functions#include <usb_serial.h>usb_serial_get_baud()Get the baud rate. Returned as a 32 bit unsigned long.Data is always tranferred at full USB speed. This is merely thesetting selected by the PC or Macintosh software, which is notused by USB. You do NOT needto constrain your transmission to this rate. However, manyserial communication programs are coded very inefficientlybecause the programmer assumes 'slow' data. You caneasily overwhelm these programs, even when running on very fast machines,if you sustain full USB speed transfers! Likewise, the host does not enforce this baud rate upon the softwarethat is sending data to you. However, unlike real serial communicationwhere you could lose data if you do not read fast enough, USB willalways buffer data until you have read it. If the software does notimplement timeouts, you may read at any speed and never lose a byte. usb_serial_get_stopbits()Get the number of stop bits.Stop bits are never included in the USB data stream. This ismerely the setting selected by the PC software. usb_serial_get_paritytype()Get the parity typeParity bits are never included in the USB data stream. This ismerely the setting selected by the PC software. usb_serial_get_numbits()Get the number of data bits.Possible values are 5, 6, 7, 8, 16. Data isalways padded to full bytes when 5, 6 or 7 are selected. usb_serial_get_control()Get the RTS and DTR signal state.The byte returned contains the following bits. usb_serial_set_control(byte);Set various control lines and status flags.The following bits can be OR'd together to form the byte totransmit. This function should only be called if the byte isdifferent from the previously transmitted one. There is no CTS signal. If software on the host has transmitteddata to you but you haven't been calling the getchar function,it remains buffered (either in local buffers or buffers on the host).It can not belost because you weren't listening at the right time, like itwould in real serial communication. TODO: this function is untested. Does it work? Please [email protected] if you have tried it.... USB Connection Management Functions#include <usb_serial.h>usb_init()Initialize the USB controller. This must be called before anyothers, typically as your program initializes everything. Thisfunction always returns immediately and never waits for any USBcommunication.usb_configured()Is the USB controller configured?Returns 0 (false) if the host has not enumerated (auto-detected) andconfigured the USB controller. Returns non-zero (true) if configurationis complete. Many PC and Macintosh drivers are not immediately ready to transfer data,even after configuration is complete. An additional delay of 1 secondis generally a good idea to allow drivers to load on the PC beforeinitiating data transfers. Transmit Bandwidth BenchmarkA simple transmit bandwidth benchmark program is included in thefiletx_benchmark.c . To use this, edit the Makefile,or copy it over example.c, and compile with 'make'.The benchmark waits for you to run a program (which assertsthe DTR signal). It then wait 5 seconds to begin the data, followedby a short message is sent as rapidly as possible for 10 seconds.To run the benchmark, a simple program which reads from the deviceas rapidly as possible should be used. Here are examples.Serial listen source for Linux and Mac OS X.Serial read source for Windows.Ready to use EXE for Windows. On Windows,for COM10 and higher, you must use the syntax .COM## These benchmark results tested on a Macbook with Intel dual core 2.4 Ghz,4 gigs RAM, external USB mouse, and built-in USB peripherals. Non-macintoshsystems were installed via Boot Camp.
|