1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <log/log.h> #include <stdlib.h> #define UART_DEVICE "/dev/ttySLB1" struct temp { float temp_max1; float temp_max2; float temp_max3; float temp_min; float temp_mean; float temp_enviromem; char temp_col[1536]; };
int main(void) { int count, i, fd; struct termios oldtio, newtio; struct temp *temp; temp = (struct temp *)malloc(sizeof(struct temp)); if (!temp) { printf("malloc failed\n"); return -1; }
char cmd_buf1[] = { 0xAA, 0x01, 0x04, 0x00, 0x06, 0x10, 0x05, 0x00, 0xBB}; char cmd_buf2[] = { 0xAA, 0x01, 0x04, 0x00, 0x00, 0xA0, 0x00, 0x03, 0xBB}; char cmd_buf3[] = { 0xAA, 0x01, 0x04, 0x00, 0x03, 0x10, 0x01, 0x00, 0xBB}; char read_buf[2000];
fd = open(UART_DEVICE, O_RDWR | O_NOCTTY); if (fd < 0) { printf("Open %s failed\n", UART_DEVICE); return -1; } tcgetattr(fd, &oldtio); memset(&newtio, 0, sizeof(newtio)); newtio.c_cflag = B230400 | CS8 | CLOCAL | CREAD | CSTOPB; newtio.c_iflag = IGNPAR; tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &newtio);
for (i = 0; i < 9; i++) printf("%#X ", cmd_buf1[i]);
count = write(fd, cmd_buf1, 9); if (count != 9) { printf("send failed\n"); return -1; } usleep(500000); memset(read_buf, 0, sizeof(read_buf)); count = read(fd, read_buf, sizeof(read_buf)); if (count > 0) { for (i = 0; i < count; i++); temp->temp_max1 = read_buf[7] << 8 | read_buf[6]; temp->temp_max2 = read_buf[9] << 8 | read_buf[8]; temp->temp_max3 = read_buf[11] << 8 | read_buf[10]; temp->temp_min = read_buf[13] << 8 | read_buf[12]; temp->temp_mean = read_buf[15] << 8 | read_buf[14];
printf("temp->temp_max1 = %f\n", temp->temp_max1 * 0.01); printf("temp->temp_max2 = %f\n", temp->temp_max2 * 0.01); printf("temp->temp_max3 = %f\n", temp->temp_max3 * 0.01); printf("temp->temp_min = %f\n", temp->temp_min * 0.01); printf("temp->temp_mean = %f\n", temp->temp_mean * 0.01); } else { printf("read temp failed\n"); return -1; }
count = write(fd, cmd_buf3, 9); if (count != 9) { printf("send failed\n"); return -1; }
usleep(365); memset(read_buf, 0, sizeof(read_buf)); count = read(fd, read_buf, sizeof(read_buf)); if (count > 0) { for (i = 0; i < count; i++); temp->temp_enviromem = read_buf[7] << 8 | read_buf[6]; printf("temp->temp_enviromem = %f\n", temp->temp_enviromem * 0.01); } else { printf("read enviromem failed\n"); return -1; }
count = write(fd, cmd_buf2, 9); if (count != 9) { printf("send failed\n"); return -1; }
usleep(70000); memset(read_buf, 0, sizeof(read_buf)); memset(temp->temp_col, 0, sizeof(temp->temp_col)); count = read(fd, read_buf, sizeof(read_buf)); printf("count = %d\n", count); if (count > 0) { for (i = 0; i < count - 7; i++) temp->temp_col[i] = read_buf[i+6]; for (i = 0; i < 1536; i++) { if (!(i%10)) printf("\n"); printf("%#X ", temp->temp_col[i]); } } else { printf("read temp colour failed\n"); return -1; } free(temp); close(fd); tcsetattr(fd, TCSANOW, &oldtio); return 0; }
|