#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <errno.h>

int main() {
    int fd = open("/dev/ttyS7", O_RDWR | O_NOCTTY | O_NONBLOCK);
    if(fd < 0) { perror("open"); return 1; }

    struct termios tty;
    tcgetattr(fd, &tty);
    cfsetospeed(&tty, B460800);
    cfsetispeed(&tty, B460800);
    tty.c_cflag |= (CLOCAL | CREAD);
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;
    tty.c_cflag &= ~PARENB;
    tty.c_cflag &= ~CSTOPB;
    tcsetattr(fd, TCSANOW, &tty);

    unsigned char cmd[] = {0xFF,0xAA,0x00,0x07,0x01,0x01,0xF6};
    write(fd, cmd, sizeof(cmd));
    usleep(1000*100); // 100ms 延迟

    unsigned char buf[128];
    int n = read(fd, buf, sizeof(buf));
    if(n > 0) {
        for(int i=0;i<n;i++) printf("%02X ", buf[i]);
        printf("\n");
    } else {
        if(errno == EAGAIN || errno == EWOULDBLOCK)
            printf("No data yet\n");
        else
            perror("read");
    }

    close(fd);
    return 0;
}