Tech »

Serial Port

This is a simple tutorial on how to send and receive data through the serial port (including the USB-Serial converters).


Use this serialport.c file which includes basic functions to initialize the serial port and send/receive data.

The simplest way to start is to just put your main function in the same file. A sample main function is shown below:

int main()
    HANDLE serialPort;
    if(!serialportInit(&serialPort, "\\\\.\\COM10", 38400))
        printf("Error in serialportInit");
        return -1;

    // Send whatever you want
    SerialPutC(serialPort, 'U');

    return 0;

(Yes, that weird pattern made up of backslashes and dot in front of the port name is required)

Just replace COM10 with the port you are using and 38400 with the baud rate you want.

Some errors you might get

Error 2
This is a file not found error, the problem most likely being that you have not given the proper port name to serialPortInit.
Error 5
An "Access Denied" error, the most common problem being that the port has been opened by some other application, so close any applications using the serial port and try again.


For Linux, there is already a very good library available:

Page last modified on Tue May 3 11:57:25 2011 - Powered by PmWiki