lark1s_demo/linux/main.c

45 lines
1.0 KiB
C
Raw Normal View History

2024-05-26 14:54:40 +08:00
#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;
}