
Here you can find code to use the serial port.
It has been tested with GCC on Linux and MinGW on Windows XP/2000.
Handshaking or flowcontrol is not supported.
It uses polling to receive characters from the serial port.
Interrupt/event-based is not supported.
Without modifications, this code uses 8N1 (8 databits, no parity, 1 stopbit).
It is licensed under the GPL version 2.
This is free software, it is experimental and available under the GPL License version 2.
Despite this software is intend to be usefull, there is no warranty, use this software at your own risk.
int OpenComport(int comport_number, int baudrate)
Opens the comport, comportnumber starts with 0 (see the list of numbers).
Baudrate is expressed in baud per second i.e 115200 (see the list of possible baudrates).
Returns 1 in case of an error.
int PollComport(int comport_number, unsigned char *buf, int size)
Gets characters from the serial port (if any). Buf is a pointer to a buffer
and size the size of the buffer in bytes.
Returns the amount of received characters into the buffer.
After succesfully opening the COM-port, connect this function to a timer.
The timer should have an interval of approx. 100 milliSeconds.
Do not forget to stop the timer before closing the COM-port.
int SendByte(int comport_number, unsigned char byte)
Sends a byte via the serial port. Returns 1 in case of an error.
int SendBuf(int comport_number, unsigned char *buf, int size)
Sends multiple bytes via the serial port. Buf is a pointer to a buffer
and size the size of the buffer in bytes.
Returns -1 in case of an error, otherwise it returns the amount of bytes sent.
This function blocks (it returns after all the bytes have been processed).
void CloseComport(int comport_number)
Closes the serial port.
void cprintf(int comport_number, const char *text)
Sends a string via the serial port. String must be null-terminated.
int IsCTSEnabled(int comport_number)
This function is not needed but can be used to check the status of the CTS-pin
at the serial port.
tip: To get access to the serial port on Linux, you need to be a member of the group "dialout".
With older versions of openSuse (11.0 and older) you need to be a member of the group "uucp".
| Linux | windows | |
| 0 | ttyS0 | COM1 |
| 1 | ttyS1 | COM2 |
| 2 | ttyS2 | COM3 |
| 3 | ttyS3 | COM4 |
| 4 | ttyS4 | COM5 |
| 5 | ttyS5 | COM6 |
| 6 | ttyS6 | COM7 |
| 7 | ttyS7 | COM8 |
| 8 | ttyS8 | COM9 |
| 9 | ttyS9 | COM10 |
| 10 | ttyS10 | COM11 |
| 11 | ttyS11 | COM12 |
| 12 | ttyS12 | COM13 |
| 13 | ttyS13 | COM14 |
| 14 | ttyS14 | COM15 |
| 15 | ttyS15 | COM16 |
| 16 | ttyUSB0 | n.a. |
| 17 | ttyUSB1 | n.a. |
| 18 | ttyUSB2 | n.a. |
| 19 | ttyUSB3 | n.a. |
| 20 | ttyUSB4 | n.a. |
| 21 | ttyUSB5 | n.a. |
| Linux | windows |
| 50 | n.a. |
| 75 | n.a. |
| 110 | 110 |
| 134 | n.a. |
| 150 | n.a. |
| 200 | n.a. |
| 300 | 300 |
| 600 | 600 |
| 1200 | 1200 |
| 1800 | n.a. |
| 2400 | 2400 |
| 4800 | 4800 |
| 9600 | 9600 |
| 19200 | 19200 |
| 38400 | 38400 |
| 57600 | 57600 |
| 115200 | 115200 |
| 230400 | 128000 |
| 460800 | 256000 |
| 500000 | n.a. |
| 576000 | n.a. |
| 921600 | n.a. |
| 1000000 | n.a. |