Fix the mingw build by adding the serial_baud function to osint_mingw.c.

This commit is contained in:
David Betz 2015-02-07 09:40:01 -05:00
parent bfd131d38b
commit d71d7e7395
2 changed files with 46 additions and 26 deletions

View File

@ -147,12 +147,12 @@ static void sigint_handler(int signum)
/** /**
* open serial port * open serial port
* @param port - COMn port name * @param port - COMn port name
* @param baud - baud rate
* @returns hSerial - file handle to serial port * @returns hSerial - file handle to serial port
*/ */
int serial_init(const char* port, unsigned long baud) int serial_init(const char* port, unsigned long baud)
{ {
struct termios sparm; struct termios sparm;
int tbaud = 0;
/* open the port */ /* open the port */
#ifdef MACOSX #ifdef MACOSX
@ -174,8 +174,6 @@ int serial_init(const char* port, unsigned long baud)
return 0; return 0;
} }
fcntl(hSerial, F_SETFL, 0);
/* set the baud rate */ /* set the baud rate */
if (!serial_baud(baud)) { if (!serial_baud(baud)) {
close(hSerial); close(hSerial);
@ -193,7 +191,6 @@ int serial_init(const char* port, unsigned long baud)
cfmakeraw(&sparm); cfmakeraw(&sparm);
sparm.c_cc[VTIME] = 0; sparm.c_cc[VTIME] = 0;
sparm.c_cc[VMIN] = 1; sparm.c_cc[VMIN] = 1;
chk("cfsetspeed", cfsetspeed(&sparm, tbaud));
#else #else
memset(&sparm, 0, sizeof(sparm)); memset(&sparm, 0, sizeof(sparm));
sparm.c_cflag = CS8 | CLOCAL | CREAD; sparm.c_cflag = CS8 | CLOCAL | CREAD;
@ -203,8 +200,6 @@ int serial_init(const char* port, unsigned long baud)
sparm.c_iflag = IGNPAR | IGNBRK; sparm.c_iflag = IGNPAR | IGNBRK;
sparm.c_cc[VTIME] = 0; sparm.c_cc[VTIME] = 0;
sparm.c_cc[VMIN] = 1; sparm.c_cc[VMIN] = 1;
chk("cfsetispeed", cfsetispeed(&sparm, tbaud));
chk("cfsetospeed", cfsetospeed(&sparm, tbaud));
#endif #endif
/* set the options */ /* set the options */
@ -269,8 +264,11 @@ int serial_baud(unsigned long baud)
tbaud = B9600; tbaud = B9600;
break; break;
default: default:
// try just using the supplied baud rate
tbaud = baud;
break;
#if 0
printf("Unsupported baudrate. Use "); printf("Unsupported baudrate. Use ");
tbaud = baud; break;
#ifdef B921600 #ifdef B921600
printf("921600, "); printf("921600, ");
#endif #endif
@ -289,6 +287,7 @@ int serial_baud(unsigned long baud)
printf("115200, 57600, 38400, 19200, or 9600\n"); printf("115200, 57600, 38400, 19200, or 9600\n");
serial_done(); serial_done();
exit(2); exit(2);
#endif
break; break;
} }

View File

@ -72,26 +72,13 @@ int serial_init(const char *port, unsigned long baud)
if (hSerial == INVALID_HANDLE_VALUE) if (hSerial == INVALID_HANDLE_VALUE)
return FALSE; return FALSE;
GetCommState(hSerial, &state); /* set the baud rate */
switch (baud) { if (!serial_baud(baud)) {
case 9600: CloseHandle(hSerial);
state.BaudRate = CBR_9600; return 0;
break;
case 19200:
state.BaudRate = CBR_19200;
break;
case 38400:
state.BaudRate = CBR_38400;
break;
case 57600:
state.BaudRate = CBR_57600;
break;
case 115200:
state.BaudRate = CBR_115200;
break;
default:
return FALSE;
} }
GetCommState(hSerial, &state);
state.ByteSize = 8; state.ByteSize = 8;
state.Parity = NOPARITY; state.Parity = NOPARITY;
state.StopBits = ONESTOPBIT; state.StopBits = ONESTOPBIT;
@ -123,6 +110,40 @@ int serial_init(const char *port, unsigned long baud)
return TRUE; return TRUE;
} }
/**
* change the baud rate of the serial port
* @param baud - baud rate
* @returns 1 for success and 0 for failure
*/
int serial_baud(unsigned long baud)
{
DCB state;
GetCommState(hSerial, &state);
switch (baud) {
case 9600:
state.BaudRate = CBR_9600;
break;
case 19200:
state.BaudRate = CBR_19200;
break;
case 38400:
state.BaudRate = CBR_38400;
break;
case 57600:
state.BaudRate = CBR_57600;
break;
case 115200:
state.BaudRate = CBR_115200;
break;
default:
return FALSE;
}
SetCommState(hSerial, &state);
return TRUE;
}
void serial_done(void) void serial_done(void)
{ {
if (hSerial != INVALID_HANDLE_VALUE) { if (hSerial != INVALID_HANDLE_VALUE) {