#include #include #include #include #include #include #include #include #include #include #include #include #include #define TOTAL_NUM (1000) #define UART_BUFFER (128) #define SCPI_BUFFER (512) #define PORT (5025) #define HOST ("192.168.10.11") #define MSG_BEEP ("SYST:BEEP\n") #define MSG_SAVE ("EXP:WAV:SAVE\n") #define MSG_NAME ("EXP:WAV:NAME \"/USB_FRONT/TRC%04i.CSV\"\n") uint8_t cLUT(char c) { return c>= 'A' ? c - 'A' + 10 : c - '0'; } char vLUT(uint8_t n) { return n >= 10 ? n + 'A' - 10 : n + '0'; } void get_rand(char* buffer, int n) { int rd = open("/dev/urandom", O_RDONLY); read(rd, buffer, n); close(rd); for(int i = 0; i < n; ++i) { buffer[i] &= 0x0F; buffer[i] = vLUT(buffer[i]); } } int uart_open(char* path) { int fd = open(path, O_RDWR | O_ASYNC); struct termios attr; tcgetattr(fd, &attr); cfsetospeed(&attr, B115200); cfsetispeed(&attr, B115200); attr.c_cflag &= ~(PARENB | CSTOPB | CSIZE); attr.c_cflag |= CS8 | CLOCAL; attr.c_lflag = ICANON; attr.c_oflag &= ~OPOST; tcsetattr(fd, TCSANOW, &attr); tcflush(fd, TCIOFLUSH); return fd; } int uart_recv(int fd, char *b, int len) { int i = 0; for (char c = 0; i < len && c != '\n'; ++i) { read(fd, &c, 1); b[i] = c; } return i; } int uart_send(int fd, char *b, int len) { return write(fd, b, len);; } int uart_close(int fd) { return close(fd); } int scpi_cmd_write(int sd, char* cmd) { return write(sd, cmd, strlen(cmd)); } int scpi_cmd_read(int sd, char* cmd, char* b) { if (scpi_cmd_write(sd, cmd) != strlen(cmd)) { printf("Команда %s не прошла\n", cmd); return -1; } return read(sd, b, SCPI_BUFFER); } int scpi_init(char* ip, int port) { int sd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in server = {.sin_family = AF_INET, .sin_port = htons(port)}; inet_pton(AF_INET, ip, &server.sin_addr); connect(sd, (struct sockaddr *) &server, sizeof(server)); return sd; } void run_process(int ud, FILE *fd) { static char buffer[UART_BUFFER] = {0}; memcpy(buffer, "ENC 0x", 6); get_rand(&buffer[6], 32); printf("%s -> ", &buffer[4]); uart_send(ud, buffer, strlen(buffer)); for (int i = 6; i < 6 + 32; i += 2) { char c = (cLUT(buffer[i]) << 4) | (cLUT(buffer[i + 1])); fwrite(&c, 1, 1, fd); } memset(buffer, 0, UART_BUFFER); uart_recv(ud, buffer, UART_BUFFER); printf("%s", buffer); for (int i = 2; i < 2 + 32; i += 2) { char c = (cLUT(buffer[i]) << 4) | (cLUT(buffer[i + 1])); fwrite(&c, 1, 1, fd); } fflush(fd); return; } void save_trace(int sd) { static int num = 0; static char buffer[SCPI_BUFFER] = {0}; sprintf(buffer, MSG_NAME, num++); scpi_cmd_write(sd, buffer); scpi_cmd_write(sd, MSG_SAVE); scpi_cmd_write(sd, MSG_BEEP); usleep(5000000); return; } int main(int argc, char** argv) { int sd = scpi_init(HOST, PORT); int ud = uart_open(argv[1]); FILE *fd = fopen("data.bin", "w"); printf("%s\n", "Начинаем!"); for (int i = 0; i < TOTAL_NUM; ++i) { printf("%s %04i: ", "Трасса", i); run_process(ud, fd); save_trace(sd); } printf("%s\n", "Готово!"); close(sd); close(ud); fclose(fd); return 0; }