SKEAZ128xxx4 控制器:C 语言 UART2 串口通信 (波特率 115200) 示例
#include <stdio.h>\n#include <stdlib.h>\n#include <stdint.h>\n#include <unistd.h>\n#include <fcntl.h>\n#include <termios.h>\n\nint main()\n{\n int uart_fd;\n struct termios uart_options;\n\n // \u5f00\u59cb\u4e00\u5ea7\u4e32\u53e3\u8bbe\u5907\n uart_fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);\n if (uart_fd == -1) {\n perror("Unable to open UART. Ensure it is not in use by another application.");\n return -1;\n }\n\n // \u914d\u7f6e\u4e32\u53e3\u9009\u9879\n tcgetattr(uart_fd, &uart_options);\n uart_options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;\n uart_options.c_iflag = IGNPAR;\n uart_options.c_oflag = 0;\n uart_options.c_lflag = 0;\n tcflush(uart_fd, TCIFLUSH);\n tcsetattr(uart_fd, TCSANOW, &uart_options);\n\n // \u51c6\u5907\u53d1\u9001\u7684\u6570\u7ec4\u6570\u636e\n uint8_t send_data[] = {0x01, 0x02, 0x03, 0x04, 0x05};\n int send_size = sizeof(send_data);\n\n // \u53d1\u9001\u6570\u7ec4\u6570\u636e\n int bytes_written = write(uart_fd, send_data, send_size);\n if (bytes_written < 0) {\n perror("UART write error");\n return -1;\n }\n\n // \u63a5\u6536\u6570\u636e\u7f13\u51b2\u533a\n uint8_t receive_data[256];\n\n // \u63a5\u6536\u6570\u636e\n int bytes_read = read(uart_fd, &receive_data, sizeof(receive_data));\n if (bytes_read < 0) {\n perror("UART read error");\n return -1;\n }\n\n // \u6253\u5370\u63a5\u6536\u5230\u7684\u6570\u636e\n printf("Received data: ");\n for (int i = 0; i < bytes_read; i++) {\n printf("%02X ", receive_data[i]);\n }\n printf("\n");\n\n // \u5173\u95ed\u4e32\u53e3\u8bbe\u5907\n close(uart_fd);\n\n return 0;\n}\n\n\u8bf7\u6ce8\u610f\uff0c\u4e0a\u8ff0\u793a\u4f8b\u4e2d\u7684\u4e32\u53e3\u8bbe\u5907\u8def\u5f84\u201c/dev/ttyS1\u201d\u662f\u6839\u636e\u60a8\u7684\u7cfb\u7edf\u548c\u786c\u4ef6\u73af\u5883\u800c\u5b9a\u7684\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u6839\u636e\u81ea\u5df1\u7684\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u4fee\u6539\u3002\u52a0\u4e0a\uff0c\u8be5\u793a\u4f8b\u53ea\u53d1\u9001\u4e86\u4e00\u4e2a\u56fa\u5b9a\u7684\u6570\u7ec4\u6570\u636e\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u4fee\u6539\u53d1\u9001\u7684\u6570\u636e\u3002
原文地址: https://www.cveoy.top/t/topic/qd3u 著作权归作者所有。请勿转载和采集!