#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> char * scope_dev = "/dev/ttyS0"; int scope; #define BUFSIZE 1024 char answer[BUFSIZE]; void gds_ask(char * cmd) { char c; int k; int ret; printf("Rx: %s", cmd); ret = write(scope, cmd, strlen(cmd)); if (ret == -1) { perror("write()"); exit(1); }; memset(answer, 0, BUFSIZE); k = 0; do { ret = read(scope, &c, 1); if (ret == -1) { perror("read()"); break; }; answer[k++] = c; if (BUFSIZE == k) { printf("Oops, not enough buffer space\n"); break; } } while ( c != '\n' ); printf("Tx: %s\n", answer); } #include <termios.h> void set_tty(void) { struct termios options; int r; r = tcgetattr(scope, &options); if (-1 == r) { perror("tcgetattr()"); exit(1); } /* set 9600 8N1 */ /* no parity bit */ options.c_cflag &= ~PARENB; /* one stop bit */ options.c_cflag &= ~CSTOPB; /* 8 bit symbols */ options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; /* no RTS/CTS handshake */ options.c_cflag &= ~CRTSCTS; /* baud rate == 9600 */ cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); /* disable modem control signals and enable rx */ options.c_cflag |= (CLOCAL | CREAD); /* raw mode */ options.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); options.c_oflag &= ~(OPOST); options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN); r = tcsetattr(scope, TCSANOW, &options); if (-1 == r) { perror("tcsetattr()"); exit(1); } } int main(void) { printf("Opening %s ... ", scope_dev); fflush(stdout); scope = open(scope_dev, O_RDWR); if (scope == -1) { perror("open()"); exit(1); }; printf("OK\n"); printf("Setting tty... "); fflush(stdout); set_tty(); printf("OK\n"); gds_ask("*IDN?\n"); exit(0); }