[feat] add linux demo of serial port
This commit is contained in:
parent
b74eceed5e
commit
aed3224ddc
45
linux/main.c
Normal file
45
linux/main.c
Normal file
@ -0,0 +1,45 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
#include <string.h>
|
||||
|
||||
int main() {
|
||||
int fd;
|
||||
struct termios options;
|
||||
|
||||
fd = open("/dev/ttyS3", O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (fd == -1) {
|
||||
perror("can not open serial port");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* config serial port */
|
||||
tcgetattr(fd, &options);
|
||||
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
|
||||
options.c_iflag = IGNPAR;
|
||||
options.c_oflag = 0;
|
||||
options.c_lflag = 0;
|
||||
tcflush(fd, TCIFLUSH);
|
||||
tcsetattr(fd, TCSANOW, &options);
|
||||
|
||||
uint8_t buffer_w[] = {0x09, 0x04, 0x00, 0x00, 0x00, 0x20, 0xF0, 0x9A};
|
||||
uint8_t buffer_r[256];
|
||||
while (1) {
|
||||
write(fd, buffer_w, sizeof(buffer_w));
|
||||
sleep(1);
|
||||
|
||||
int num_bytes = read(fd, buffer_r, sizeof(buffer_r));
|
||||
for (int i = 0; i < num_bytes; i++) {
|
||||
printf("%02X ", buffer_r[i]);
|
||||
}
|
||||
printf("\r\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user