void sendByte(int byte) { byte &= 0xFF; putByte(byte); char buf; if (read(com, &buf, 1)!=1) comErr("Loopback failed, nothing was received\n"); int rx=buf &0xFF; if (byte != rx) comErr("Loopback failed, sent 0x%02X, got 0x%02X\n", byte, rx); rx = getByte(); if (byte != rx) comErr("Target echo failed, sent 0x%02X, got 0x%02X\n", byte, rx); }
void putByte(int byte) { char buf = byte; if (verbose>3) flsprintf(stdout,"TX: 0x%02X\n", byte); int n = write(com, &buf, 1); if (n != 1) comErr("Serial port failed to send a byte, write returned %d\n", n); }
void putByte(int byte) { int n; if (verbose>3) flsprintf(stdout,"TX: 0x%02X\n", byte); WriteFile(port_handle, &byte, 1, (LPDWORD)((void *)&n), NULL); if (n != 1) comErr("Serial port failed to send a byte, write returned %d\n", n); }
int getByte() { unsigned char buf[2]; int n; ReadFile(port_handle, buf, 1, (LPDWORD)((void *)&n), NULL); if (verbose>3) flsprintf(stdout,n<1?"RX: fail\n":"RX: 0x%02X\n", buf[0] & 0xFF); if (n == 1) return buf[0] & 0xFF; comErr("Serial port failed to receive a byte, read returned %d\n", n); return -1; // never reached }
int getByte() { char buf; int n = read(com, &buf, 1); if (verbose>3) flsprintf(stdout,n<1?"RX: fail\n":"RX: 0x%02X\n", buf & 0xFF); if (n == 1) return buf & 0xFF; comErr("Serial port failed to receive a byte, read returned %d\n", n); return -1; // never reached }
void putBytes (unsigned char * data, int len) { /* int i; for (i=0;i<len;i++) putByte(data[i]); */ int n; WriteFile(port_handle, data, len, (LPDWORD)((void *)&n), NULL); if (n != len) comErr("Serial port failed to send a byte, write returned %d\n", n); }
void initSerialPort() { com = open(COM, O_RDWR | O_NOCTTY | O_NDELAY); if (com <0) comErr("Failed to open serial port\n"); fcntl(com, F_SETFL, 0); struct termios opts; tcgetattr(com, &opts); opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); opts.c_cflag |= (CLOCAL | CREAD); opts.c_cflag &= ~PARENB; opts.c_cflag |= CSTOPB; // two stop bits opts.c_cflag &= ~CSIZE; opts.c_cflag |= CS8; opts.c_oflag &= ~OPOST; opts.c_iflag &= ~INPCK; opts.c_iflag &= ~(IXON | IXOFF | IXANY); opts.c_cc[ VMIN ] = 0; opts.c_cc[ VTIME ] = 10;//0.1 sec cfsetispeed(&opts, baudRate); cfsetospeed(&opts, baudRate); setHandshake(); if (tcsetattr(com, TCSANOW, &opts) != 0) { perror(COM); abort(); } tcflush(com,TCIOFLUSH); // just in case some crap is the buffers if (!terminalMode) { char buf = -2; while (read(com, &buf, 1)>0) { if (verbose) flsprintf(stderr,"Unexpected data from serial port: %02X\n",buf & 0xFF); } } }
void initSerialPort() { baudRate=B57600; if (verbose>2) printf("Opening: %s at %d\n",COM,baudRate); com = open(COM, O_RDWR | O_NOCTTY | O_NDELAY); if (com <0) comErr("Failed to open serial port"); struct termios opts; memset (&opts,0,sizeof (opts)); fcntl(com, F_SETFL, 0); if (tcgetattr(com, &opts)!=0) printf("Err tcgetattr\n"); cfsetispeed(&opts, baudRate); cfsetospeed(&opts, baudRate); opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); opts.c_cflag |= (CLOCAL | CREAD); opts.c_cflag &= ~PARENB; opts.c_cflag &= ~CSTOPB; opts.c_cflag &= ~CSIZE; opts.c_cflag |= CS8; opts.c_oflag &= ~OPOST; opts.c_iflag &= ~INPCK; opts.c_iflag &= ~ICRNL; //do NOT translate CR to NL opts.c_iflag &= ~(IXON | IXOFF | IXANY); opts.c_cc[ VMIN ] = 0; opts.c_cc[ VTIME ] = 10;//0.1 sec if (tcsetattr(com, TCSANOW, &opts) != 0) { perror(COM); printf("set attr error"); abort(); } tcflush(com,TCIOFLUSH); // just in case some crap is the buffers }