#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <stdint.h>
#include <wiringPi.h>
#include <string.h>

int main()
{
    int fileuart;
    if ((fileuart = serialOpen("/dev/ttyAMA0", 9600)) < 0)
    {
        printf("/dev/ttyAMA0 Open Faild\r\n");
        // return 0;
    }
    printf("/dev/ttyAMA0 Open success\r\n");

    char message[9] = {0x40, 0x38, 0xb, 0x6, 0x0, 0x2, 0x1, 0x2b, 0x58};
    write(fileuart, message, 9);
    
    char buf[100] = {0};
    int recbytenum = serialDataAvail(filedevid);
    for (int i = 0; i < recbytenum; i++)
    {
        buf[i] = serialGetchar(filedevid);
    }
    for (int i = 0; i < recbytenum; i++)
    {
        printf("%x ", buf[i]);
    }
    printf("\n");
    int angle = (buf[7] << 24) | (buf[8] << 16) | (buf[9] << 8) | buf[10];
    printf("angle = %d\n", angle);
    
    return 0;
}