123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- #ifndef OPENPORT_SOURCE
- #define OPENPORT_SOURCE
- #include <unistd.h>
- #include <stdlib.h>
- #include <errno.h>
- #if defined (WIN32)
- # include <windows.h>
- #elif defined (__linux__)
- # include <termios.h>
- #elif defined (__APPLE__)
- # include <termios.h>
- # include <net/ethernet.h>
- #elif defined (__OpenBSD__) || defined (__NetBSD__) || defined (__FreeBSD__)
- # include <termios.h>
- #else
- #error "Unknown Environment"
- #endif
- #include "../tools/types.h"
- #include "../tools/files.h"
- #include "../tools/flags.h"
- #include "../tools/error.h"
- #include "../serial/serial.h"
- void openport (struct _file_ * port, flag_t flags)
- {
- #if defined (WIN32)
- HANDLE hSerial;
- COMMTIMEOUTS timeouts;
- hSerial = CreateFile (port->name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- if (hSerial == INVALID_HANDLE_VALUE)
- {
- error (1, errno, "%s", port->name);
- }
- if (_anyset (flags, UART_DEFAULT))
- {
- DCB dcbSerial =
- {
- 0
- };
- dcbSerial.DCBlength = sizeof (dcbSerial);
- if (!GetCommState (hSerial, &dcbSerial))
- {
- error (1, errno, "Can't read state: %s", port->name);
- }
- if (_anyset (flags, UART_VERBOSE))
- {
- printf ("getting %s ", port->name);
- printf ("Baud %6d ", dcbSerial.BaudRate);
- printf ("Data %d ", dcbSerial.ByteSize);
- printf ("Stop %d ", dcbSerial.StopBits);
- printf ("Parity %d\n", dcbSerial.Parity);
- }
- dcbSerial.BaudRate = CBR_115200;
- dcbSerial.ByteSize = DATABITS_8;
- dcbSerial.StopBits = ONESTOPBIT;
- dcbSerial.Parity = NOPARITY;
- if (_anyset (flags, UART_VERBOSE))
- {
- printf ("setting %s ", port->name);
- printf ("Baud %6d ", dcbSerial.BaudRate);
- printf ("Data %d ", dcbSerial.ByteSize);
- printf ("Stop %d ", dcbSerial.StopBits);
- printf ("Parity %d\n", dcbSerial.Parity);
- }
- if (!SetCommState (hSerial, &dcbSerial))
- {
- error (1, errno, "Can't save state: %s", port->name);
- }
- }
- timeouts.ReadIntervalTimeout = 10;
- timeouts.ReadTotalTimeoutConstant = 50;
- timeouts.ReadTotalTimeoutMultiplier = 10;
- timeouts.WriteTotalTimeoutConstant = 50;
- timeouts.WriteTotalTimeoutMultiplier = 10;
- if (!SetCommTimeouts (hSerial, &timeouts))
- {
- error (1, errno, "Can't set timeouts: %s", port->name);
- }
- CloseHandle (hSerial);
- if ((port->file = open (port->name, O_BINARY|O_RDWR)) == -1)
- {
- error (1, errno, "%s", port->name);
- }
- #else
- if ((port->file = open (port->name, O_BINARY|O_RDWR)) == -1)
- {
- error (1, errno, "%s", port->name);
- }
- if (_anyset (flags, UART_DEFAULT))
- {
- struct termios termios;
- #if 1
- tcgetattr (port->file, &termios);
- cfmakeraw (&termios);
- termios.c_cflag |= CS8;
- termios.c_cflag &= ~(CSTOPB);
- cfsetospeed (&termios, B115200);
- #else
- termios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
- termios.c_iflag = IGNPAR;
- termios.c_oflag = 0;
- termios.c_lflag = 0;
- termios.c_cc [VTIME] = 0;
- termios.c_cc [VMIN] = 5;
- #endif
- tcsetattr (port->file, TCSANOW, &termios);
- }
- #endif
- return;
- }
- #endif
|