#include #include #include #include /* For the termio struct */ #include #include /* For write() */ #include /* For exit(), malloc() and free() */ #include #include #include /* baudrate settings are defined in , which is included by */ #define BAUDRATE B9600 /* change this definition for the correct port */ #define SERIAL_PORT "/dev/ttyUSB0" #define _POSIX_SOURCE 1 /* POSIX compliant source */ #define FALSE 0 #define TRUE 1 /* Number of samples in the generated waveform, between 8 and 16000 */ #define N_SAMPLES 8 volatile int STOP=FALSE; /* Function prototypes */ void port_setup(int fd, struct termios *ios); void init_hp(int fd); void write_port(int fd, char *buf); void write_char(int fd, char *buf); void restore_settings(int fd, struct termios *ios); /****************************************************************************/ int main(void) { struct termios *oldtio; /* Save current serial port settings in here */ oldtio = (termios*)malloc( sizeof(struct termios) ); int fd; /* File descriptor for the serial port */ /* Open modem device for reading and writing and not as controlling tty because we don't want to get killed if linenoise sends CTRL-C. */ printf("Opening serial port...\n"); fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY); if (fd <0) {perror(SERIAL_PORT); exit(-1); } printf("Setting up serial port...\n"); port_setup(fd, oldtio); init_hp(fd); //make_waveform(fd); restore_settings(fd, oldtio); free(oldtio); return 0; } /****************************************************************************/ void port_setup(int fd, struct termios *oldtio) { /* Most of this function was copied directly from the Linux Serial Programming HOWTO. */ struct termios newtio; tcgetattr(fd, oldtio); /* save current serial port settings */ bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */ /* BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. CRTSCTS : output hardware flow control (only used if the cable has all necessary lines. See sect. 7 of Serial-HOWTO) CS8 : 8n (8bit,no parity), needed for sending arb. waveforms, see page 195 in the HP user manual. CSTOPB : 2 stopbits (remove for 1) CLOCAL : local connection, no modem contol CREAD : enable receiving characters */ //newtio.c_cflag = BAUDRATE | CS8 | CSTOPB | CLOCAL | CREAD; newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; /* IGNPAR : ignore bytes with parity errors ICRNL : map CR to NL (otherwise a CR input on the other computer will not terminate input) otherwise make device raw (no other input processing) */ newtio.c_iflag = IGNPAR; /* Raw output. */ newtio.c_oflag = 0; /* ICANON : enable canonical input disable all echo functionality, and don't send signals to calling program */ newtio.c_lflag = ICANON; /* now clean the modem line and activate the settings for the port */ tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &newtio); /* terminal settings done, now we can handle input */ } /***************************************************************************/ void init_hp(int fd) { /* Initialisation of the function generator */ write_port(fd, "SYSTEM:REMOTE"); write_port(fd, "*RST"); write_port(fd, "*CLS"); write_port(fd, "APPL:SIN 401"); //usleep(1000); //write_char(fd, "S"); //write_port(fd, "FORM:BORD SWAP"); /* Swap data bytes, send LSB first */ //write_port(fd, "OUTP:LOAD 50"); /* Output termination is 50 Ohm */ } /***************************************************************************/ void write_port(int fd, char *buffer) { /* Write a command to the HP. All commands are terminated by a \n. */ int end; end = strlen(buffer); char buf[end]; strcpy(buf, buffer); end = strlen(buf); buf[end] = '\n'; /* Add a newline to the command */ buf[end + 1] = 0; write(fd, buf, strlen(buf)); } void write_char(int fd, char *buffer) { write(fd, buffer, 1); } /***************************************************************************/ void restore_settings(int fd, struct termios *ios) { /* restore the old port settings */ printf("Restoring tty settings... \n"); tcsetattr(fd, TCSANOW, ios); }