diff --git a/linux/main.c b/linux/main.c new file mode 100644 index 0000000..99b19a3 --- /dev/null +++ b/linux/main.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include +#include +#include + +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; +} \ No newline at end of file