|
| 1 | + |
| 2 | +Library: RS-232 |
| 3 | +Author: Teunis van Beelen |
| 4 | +Url: http://www.teuniz.net/RS-232/ |
| 5 | +E-mail: teuniz@protonmail.com |
| 6 | +License: GPLv3 |
| 7 | + |
| 8 | +Last revision: August, 2020 |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +int RS232_OpenComport(int comport_number, int baudrate, const char * mode, int flowctrl) |
| 13 | + |
| 14 | + Opens the comport, comportnumber starts with 0 (see the list of numbers). |
| 15 | + Baudrate is expressed in baud per second i.e 115200 (see the list of possible baudrates). |
| 16 | + Mode is a string in the form of "8N1", "7E2", etc. |
| 17 | + 8N1 means eight databits, no parity, one stopbit. If in doubt, use 8N1 (see the list of possible modes). |
| 18 | + If flowctrl is set to 0, no flow control is used. |
| 19 | + If flowctrl is set to 1, hardware flow control is enabled using the RTS/CTS lines. |
| 20 | + Returns 1 in case of an error. |
| 21 | + In case the comport is already opened (by another process), |
| 22 | + it will not open the port but raise an error instead. |
| 23 | + |
| 24 | +int RS232_PollComport(int comport_number, unsigned char *buf, int size) |
| 25 | + |
| 26 | + Gets characters from the serial port (if any). Buf is a pointer to a buffer |
| 27 | + and size the size of the buffer in bytes. |
| 28 | + Returns the amount of received characters into the buffer. This can be less than size or zero! |
| 29 | + It does not block or wait, it returns immediately, no matter if any characters have been received or not. |
| 30 | + After succesfully opening the COM-port, connect this function to a timer. |
| 31 | + The timer should have an interval of approx. 20 to 100 milliSeconds. |
| 32 | + Do not forget to stop the timer before closing the COM-port. |
| 33 | + Allways check the return value! The return value tells you how many bytes |
| 34 | + are actually received and present in your buffer! |
| 35 | + |
| 36 | +int RS232_SendByte(int comport_number, unsigned char byte) |
| 37 | + |
| 38 | + Sends a byte via the serial port. Returns 1 in case of an error. |
| 39 | + |
| 40 | +int RS232_SendBuf(int comport_number, unsigned char *buf, int size) |
| 41 | + |
| 42 | + Sends multiple bytes via the serial port. Buf is a pointer to a buffer |
| 43 | + and size the size of the buffer in bytes. |
| 44 | + Returns -1 in case of an error, otherwise it returns the amount of bytes sent. |
| 45 | + This function blocks (it returns after all the bytes have been processed). |
| 46 | + |
| 47 | +void RS232_CloseComport(int comport_number) |
| 48 | + |
| 49 | + Closes the serial port. |
| 50 | + |
| 51 | +void RS232_cputs(int comport_number, const char *text) |
| 52 | + |
| 53 | + Sends a string via the serial port. String must be null-terminated. |
| 54 | + |
| 55 | +int RS232_GetPortnr(const char *devname) |
| 56 | + |
| 57 | + Returns the comport number based on the device name e.g. "ttyS0" or "COM1". |
| 58 | + (Doesn't mean the device actually exists!) |
| 59 | + Returns -1 when not found. |
| 60 | + |
| 61 | + |
| 62 | +The following functions are normally not needed but can be used to set or check the status of the control-lines: |
| 63 | +================================================================================================================ |
| 64 | + |
| 65 | +void RS232_enableDTR(int comport_number) |
| 66 | + |
| 67 | + Sets the DTR line high (active state). |
| 68 | + |
| 69 | +void RS232_disableDTR(int comport_number) |
| 70 | + |
| 71 | + Sets the DTR line low (non active state). |
| 72 | + |
| 73 | +void RS232_enableRTS(int comport_number) |
| 74 | + |
| 75 | + Sets the RTS line high (active state). Do not use this function if hardware flow control is enabled! |
| 76 | + |
| 77 | +void RS232_disableRTS(int comport_number) Do not use this function if hardware flow control is enabled! |
| 78 | + |
| 79 | + Sets the RTS line low (non active state). |
| 80 | + |
| 81 | +int RS232_IsRINGEnabled(int comport_number) |
| 82 | + |
| 83 | + Checks the status of the RING-pin. Returns 1 when the the RING line is high (active state), otherwise 0. |
| 84 | + |
| 85 | +int RS232_IsDSREnabled(int comport_number) |
| 86 | + |
| 87 | + Checks the status of the DSR-pin. Returns 1 when the the DSR line is high (active state), otherwise 0. |
| 88 | + |
| 89 | +int RS232_IsCTSEnabled(int comport_number) |
| 90 | + |
| 91 | + Checks the status of the CTS-pin. Returns 1 when the the CTS line is high (active state), otherwise 0. |
| 92 | + |
| 93 | +int RS232_IsDCDEnabled(int comport_number) |
| 94 | + |
| 95 | + Checks the status of the DCD-pin. Returns 1 when the the DCD line is high (active state), otherwise 0. |
| 96 | + |
| 97 | + |
| 98 | +The following functions are normally not needed but can be used to empty the rx/tx buffers: |
| 99 | +=========================================================================================== |
| 100 | + |
| 101 | +("discards data written to the serial port but not transmitted, or data received but not read") |
| 102 | + |
| 103 | +void RS232_flushRX(int comport_number) |
| 104 | + |
| 105 | + Flushes data received but not read. |
| 106 | + |
| 107 | +void RS232_flushTX(int comport_number) |
| 108 | + |
| 109 | + Flushes data written but not transmitted. |
| 110 | + |
| 111 | +void RS232_flushRXTX(int comport_number) |
| 112 | + |
| 113 | + Flushes both data received but not read, and data written but not transmitted. |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +Notes: |
| 118 | + |
| 119 | +You don't need to call RS232_PollComport() when you only want to send characters. |
| 120 | +Sending and receiving do not influence eachother. |
| 121 | + |
| 122 | +The os (kernel) has an internal buffer of 4096 bytes (for traditional onboard serial ports). |
| 123 | +USB/Serial-converter drivers use much bigger buffers (multiples of 4096). |
| 124 | +If this buffer is full and a new character arrives on the serial port, |
| 125 | +the oldest character in the buffer will be overwritten and thus will be lost. |
| 126 | + |
| 127 | +After a successfull call to RS232_OpenComport(), the os will start to buffer incoming characters. |
| 128 | + |
| 129 | +tip: To get access to the serial port on Linux, you need to be a member of the group "dialout". |
| 130 | + |
| 131 | +Note: Traditional (on-board) UART's usually have a speed limit of max. 115200 baud. |
| 132 | + Special cards and USB to Serial converters can usually be set to higher baudrates. |
| 133 | + |
| 134 | + |
| 135 | +List of comport numbers, possible baudrates and modes: |
| 136 | + |
| 137 | + Linux windows |
| 138 | +0 ttyS0 COM1 |
| 139 | +1 ttyS1 COM2 |
| 140 | +2 ttyS2 COM3 |
| 141 | +3 ttyS3 COM4 |
| 142 | +4 ttyS4 COM5 |
| 143 | +5 ttyS5 COM6 |
| 144 | +6 ttyS6 COM7 |
| 145 | +7 ttyS7 COM8 |
| 146 | +8 ttyS8 COM9 |
| 147 | +9 ttyS9 COM10 |
| 148 | +10 ttyS10 COM11 |
| 149 | +11 ttyS11 COM12 |
| 150 | +12 ttyS12 COM13 |
| 151 | +13 ttyS13 COM14 |
| 152 | +14 ttyS14 COM15 |
| 153 | +15 ttyS15 COM16 |
| 154 | +16 ttyUSB0 COM17 |
| 155 | +17 ttyUSB1 COM18 |
| 156 | +18 ttyUSB2 COM19 |
| 157 | +19 ttyUSB3 COM20 |
| 158 | +20 ttyUSB4 COM21 |
| 159 | +21 ttyUSB5 COM22 |
| 160 | +22 ttyAMA0 COM23 |
| 161 | +23 ttyAMA1 COM24 |
| 162 | +24 ttyACM0 COM25 |
| 163 | +25 ttyACM1 COM26 |
| 164 | +26 rfcomm0 COM27 |
| 165 | +27 rfcomm1 COM28 |
| 166 | +28 ircomm0 COM29 |
| 167 | +29 ircomm1 COM30 |
| 168 | +30 cuau0 COM31 |
| 169 | +31 cuau1 COM32 |
| 170 | +32 cuau2 n.a. |
| 171 | +33 cuau3 n.a. |
| 172 | +34 cuaU0 n.a. |
| 173 | +35 cuaU1 n.a. |
| 174 | +36 cuaU2 n.a. |
| 175 | +37 cuaU3 n.a. |
| 176 | + |
| 177 | +Linux windows |
| 178 | +50 n.a. |
| 179 | +75 n.a. |
| 180 | +110 110 |
| 181 | +134 n.a. |
| 182 | +150 n.a. |
| 183 | +200 n.a. |
| 184 | +300 300 |
| 185 | +600 600 |
| 186 | +1200 1200 |
| 187 | +1800 n.a. |
| 188 | +2400 2400 |
| 189 | +4800 4800 |
| 190 | +9600 9600 |
| 191 | +19200 19200 |
| 192 | +38400 38400 |
| 193 | +57600 57600 |
| 194 | +115200 115200 |
| 195 | +230400 128000 |
| 196 | +460800 256000 |
| 197 | +500000 500000 |
| 198 | +576000 n.a. |
| 199 | +921600 921600 |
| 200 | +1000000 1000000 |
| 201 | +1152000 n.a. |
| 202 | +1500000 1500000 |
| 203 | +2000000 2000000 |
| 204 | +2500000 n.a. |
| 205 | +3000000 3000000 |
| 206 | +3500000 n.a. |
| 207 | +4000000 n.a. |
| 208 | + |
| 209 | +Mode |
| 210 | +8N1 |
| 211 | +8O1 |
| 212 | +8E1 |
| 213 | +8N2 |
| 214 | +8O2 |
| 215 | +8E2 |
| 216 | +7N1 |
| 217 | +7O1 |
| 218 | +7E1 |
| 219 | +7N2 |
| 220 | +7O2 |
| 221 | +7E2 |
| 222 | +6N1 |
| 223 | +6O1 |
| 224 | +6E1 |
| 225 | +6N2 |
| 226 | +6O2 |
| 227 | +6E2 |
| 228 | +5N1 |
| 229 | +5O1 |
| 230 | +5E1 |
| 231 | +5N2 |
| 232 | +5O2 |
| 233 | +5E2 |
| 234 | + |
| 235 | + |
0 commit comments