This shows you the differences between two versions of the page.
| — | en:examples:communication:rs232:linux [2026/02/19 11:30] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | ====== RS-232 on Linux ====== | ||
| + | |||
| + | Example based on Ubuntu | ||
| + | |||
| + | Prerequisites: | ||
| + | If the program is missing, install it with: //sudo apt-get install minicom// | ||
| + | |||
| + | Connect the microcontroller to a USB-COM converter and check if it was detected by the system: //dmesg | grep tty// | ||
| + | |||
| + | the result could look like this: | ||
| + | |||
| + | [ 0.000000] console [tty0] enabled | ||
| + | [ 3823.820524] usb 5-1: pl2303 converter now attached to ttyUSB0 | ||
| + | [ 3937.382589] usb 5-2: FTDI USB Serial Device converter now attached to ttyUSB1 | ||
| + | [ 4152.680434] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0 | ||
| + | [ 4159.873377] ftdi_sio ttyUSB1: FTDI USB Serial Device converter now disconnected from ttyUSB1 | ||
| + | [ 4164.285887] usb 5-2: FTDI USB Serial Device converter now attached to ttyUSB0 | ||
| + | [ 4215.343098] usb 5-1: pl2303 converter now attached to ttyUSB1 | ||
| + | [ 4633.056395] pl2303 ttyUSB1: pl2303 converter now disconnected from ttyUSB1 | ||
| + | [ 4659.016240] usb 5-1: pl2303 converter now attached to ttyUSB1 | ||
| + | |||
| + | From this output you need to determine which port is assigned to the USB converter. | ||
| + | |||
| + | When the example code is loaded into the microcontroller, | ||
| + | //sudo minicom -s// | ||
| + | |||
| + | {{: | ||
| + | |||
| + | First you need to configure the connection: | ||
| + | |||
| + | To set the port, press key A and set the connection, for example: /// | ||
| + | and the port parameters Bps/ | ||
| + | Press ESC to exit. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | After selecting //Exit//, minicom initializes the port connection. | ||
| + | |||
| + | The microcontroller must be connected to the computer' | ||
| + | |||
| + | The program below responds to every symbol received on the UART port with a greeting and toggles the LED on the board. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | If you want to see your own keypresses, enable local echo: //CTRL+A E// | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | |||
| + | usart port = USART(0); | ||
| + | pin led = PIN(B, 7); //red LED on controller board | ||
| + | |||
| + | int main (void) | ||
| + | { | ||
| + | char c; | ||
| + | | ||
| + | pin_setup_output(led); | ||
| + | | ||
| + | usart_init_async(port, | ||
| + | USART_DATABITS_8, | ||
| + | USART_STOPBITS_ONE, | ||
| + | USART_PARITY_NONE, | ||
| + | USART_BAUDRATE_ASYNC(115200)); | ||
| + | |||
| + | while (1) // | ||
| + | { | ||
| + | //if char is sent to controller, read it into c | ||
| + | if (usart_try_read_char(port, | ||
| + | { | ||
| + | //invert LED | ||
| + | pin_toggle(led); | ||
| + | // Write some greetings | ||
| + | usart_send_string(port, | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||