#include #include #include #include #include #include #include "support.h" #include "main.h" #include #include unsigned char uart_timeoutrunning=0; unsigned int uart_portcount=0; unsigned char uart_autoselected_comm[200]; static int serial_fd = -1; void UART_Close (void); void UART_Send (unsigned char *txd, unsigned int size); unsigned char *UART_GetCommNameFromNumber(unsigned char nr) { return g_strdup ("/dev/ttyUSB0"); } static gint UART_timeout (gpointer data) { } void UART_StartScanning(void) { } void UART_StopScanning(void) { } unsigned char UART_Connect(unsigned char tabnr, int baudrate) { struct termios tio; struct serial_struct serstruct; speed_t baud = 0; switch (baudrate) { case 50: baud = B50; break; case 75: baud = B75; break; case 110: baud = B110; break; case 134: baud = B134; break; case 150: baud = B150; break; case 200: baud = B200; break; case 300: baud = B300; break; case 600: baud = B600; break; case 1200: baud = B1200; break; case 1800: baud = B1800; break; case 2400: baud = B2400; break; case 4800: baud = B4800; break; case 9600: baud = B9600; break; case 19200: baud = B19200; break; case 38400: baud = B38400; break; case 57600: baud = B57600; break; case 115200: baud = B115200; break; case 230400: baud = B230400; break; case 460800: baud = B460800; break; case 500000: baud = B500000; break; case 576000: baud = B576000; break; case 921600: baud = B921600; break; case 1000000: baud = B1000000; break; case 1152000: baud = B1152000; break; case 1500000: baud = B1500000; break; case 2000000: baud = B2000000; break; case 2500000: baud = B2500000; break; case 3000000: baud = B3000000; break; case 3500000: baud = B3500000; break; case 4000000: baud = B4000000; break; default: baud = 0; break; } if (serial_fd >= 0) UART_Close (); serial_fd = open ("/dev/ttyUSB0", O_RDWR); if (serial_fd < 0) { perror ("serial, open"); return 1; } if (tcgetattr (serial_fd, &tio) != 0) { perror ("serial, tcgetattr"); return 2; } cfmakeraw (&tio); if (baud != 0) { if (cfsetspeed (&tio, baud) != 0) { perror ("serial, cfsetspeed"); return 2; } } tio.c_cflag &= ~CRTSCTS; tio.c_cflag |= CLOCAL; if (tcsetattr (serial_fd, TCSANOW, &tio) != 0) { perror ("serial, tcsetattr"); return 2; } if (ioctl (serial_fd, TIOCGSERIAL, &serstruct) != 0) { perror ("serial, open, ioctl 1"); return 2; } if (baud == 0) { /* custom baudrate */ serstruct.custom_divisor = serstruct.baud_base / baudrate; if (serstruct.custom_divisor == 0) serstruct.custom_divisor = 1; serstruct.flags &= ~ASYNC_SPD_MASK; serstruct.flags |= ASYNC_SPD_CUST; } else { /* standard baudrate */ serstruct.custom_divisor = 0; serstruct.flags &= ~ASYNC_SPD_CUST; } if (ioctl (serial_fd, TIOCSSERIAL, &serstruct) != 0) { perror ("serial, open, ioctl 2"); return 2; } return 0; } void UART_Close(void) { struct serial_struct serstruct; /* reset custom speed flags */ ioctl (serial_fd, TIOCGSERIAL, &serstruct); serstruct.custom_divisor = 0; serstruct.flags &= ~ASYNC_SPD_CUST; ioctl (serial_fd, TIOCSSERIAL, &serstruct); if (serial_fd >= 0 && close (serial_fd) < 0) perror ("serial, close:"); serial_fd = -1; } void UART_SendByte(unsigned char ch) { UART_Send (&ch, 1); } void UART_Send(unsigned char *txd, unsigned int size) { int ret, bytes_written = 0; if (serial_fd < 0) return; while (bytes_written < size) { ret = write (serial_fd, txd + bytes_written, size - bytes_written); if (ret < 0 && errno != EAGAIN) { perror ("serial, write:"); return; } bytes_written += MAX (ret, 0); } } int UART_ReceiveTimeout(unsigned char *rxd, int size, int timeout) { struct timeval to = { (timeout * 2) / 1000, ((timeout * 2) % 1000) * 1000 }; int ret, bytes_read = 0; fd_set read_fds; FD_ZERO (&read_fds); FD_SET (serial_fd, &read_fds); while (bytes_read < size && select (serial_fd+1, &read_fds, NULL, NULL, &to) == 1) { ret = read (serial_fd, rxd + bytes_read, size - bytes_read); if (ret < 0 && errno != EAGAIN) { perror ("serial, read:"); return 1; } bytes_read += MAX (ret, 0); } return (bytes_read != size); }