Frederic BOUVIER:
Win32 serial port communication fixes.
This commit is contained in:
+102
-18
@@ -32,9 +32,7 @@
|
|||||||
# include <errno.h>
|
# include <errno.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
|
#if !defined( WIN32 ) || defined( __CYGWIN__) || defined( __CYGWIN32__ )
|
||||||
// maybe include something???
|
|
||||||
#else
|
|
||||||
# include <termios.h>
|
# include <termios.h>
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
# include <sys/stat.h>
|
# include <sys/stat.h>
|
||||||
@@ -75,7 +73,7 @@ bool SGSerialPort::open_port(const string& device) {
|
|||||||
0, // dwShareMode
|
0, // dwShareMode
|
||||||
NULL, // lpSecurityAttributes
|
NULL, // lpSecurityAttributes
|
||||||
OPEN_EXISTING,
|
OPEN_EXISTING,
|
||||||
FILE_FLAG_OVERLAPPED,
|
0,
|
||||||
NULL );
|
NULL );
|
||||||
if ( fd == INVALID_HANDLE_VALUE )
|
if ( fd == INVALID_HANDLE_VALUE )
|
||||||
{
|
{
|
||||||
@@ -165,6 +163,51 @@ bool SGSerialPort::set_baud(int baud) {
|
|||||||
|
|
||||||
#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
|
#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
|
||||||
|
|
||||||
|
DCB dcb;
|
||||||
|
if ( GetCommState( fd, &dcb ) ) {
|
||||||
|
dcb.BaudRate = baud;
|
||||||
|
dcb.fOutxCtsFlow = FALSE;
|
||||||
|
dcb.fOutxDsrFlow = FALSE;
|
||||||
|
dcb.fOutX = TRUE;
|
||||||
|
dcb.fInX = TRUE;
|
||||||
|
|
||||||
|
if ( !SetCommState( fd, &dcb ) ) {
|
||||||
|
LPVOID lpMsgBuf;
|
||||||
|
FormatMessage(
|
||||||
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL,
|
||||||
|
GetLastError(),
|
||||||
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
||||||
|
(LPTSTR) &lpMsgBuf,
|
||||||
|
0,
|
||||||
|
NULL );
|
||||||
|
|
||||||
|
SG_LOG( SG_IO, SG_ALERT, "Unable to update port settings: "
|
||||||
|
<< (const char*) lpMsgBuf );
|
||||||
|
LocalFree( lpMsgBuf );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
LPVOID lpMsgBuf;
|
||||||
|
FormatMessage(
|
||||||
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL,
|
||||||
|
GetLastError(),
|
||||||
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
||||||
|
(LPTSTR) &lpMsgBuf,
|
||||||
|
0,
|
||||||
|
NULL );
|
||||||
|
|
||||||
|
SG_LOG( SG_IO, SG_ALERT, "Unable to poll port settings: "
|
||||||
|
<< (const char*) lpMsgBuf );
|
||||||
|
LocalFree( lpMsgBuf );
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
@@ -227,19 +270,39 @@ bool SGSerialPort::set_baud(int baud) {
|
|||||||
|
|
||||||
string SGSerialPort::read_port() {
|
string SGSerialPort::read_port() {
|
||||||
|
|
||||||
|
const int max_count = 1024;
|
||||||
|
char buffer[max_count+1];
|
||||||
|
string result;
|
||||||
|
|
||||||
#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
|
#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
|
||||||
|
|
||||||
string result = "";
|
DWORD count;
|
||||||
|
if ( ReadFile( fd, buffer, max_count, &count, 0 ) ) {
|
||||||
|
buffer[count] = '\0';
|
||||||
|
result = buffer;
|
||||||
|
} else {
|
||||||
|
LPVOID lpMsgBuf;
|
||||||
|
FormatMessage(
|
||||||
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL,
|
||||||
|
GetLastError(),
|
||||||
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
||||||
|
(LPTSTR) &lpMsgBuf,
|
||||||
|
0,
|
||||||
|
NULL );
|
||||||
|
|
||||||
|
SG_LOG( SG_IO, SG_ALERT, "Serial I/O read error: "
|
||||||
|
<< (const char*) lpMsgBuf );
|
||||||
|
LocalFree( lpMsgBuf );
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
const int max_count = 1024;
|
int count = read(fd, buffer, max_count);
|
||||||
char buffer[max_count+1];
|
|
||||||
int count;
|
|
||||||
string result;
|
|
||||||
|
|
||||||
count = read(fd, buffer, max_count);
|
|
||||||
// cout << "read " << count << " bytes" << endl;
|
// cout << "read " << count << " bytes" << endl;
|
||||||
|
|
||||||
if ( count < 0 ) {
|
if ( count < 0 ) {
|
||||||
@@ -265,8 +328,31 @@ int SGSerialPort::read_port(char *buf, int len) {
|
|||||||
|
|
||||||
#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
|
#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
|
||||||
|
|
||||||
buf[0] = '\0';
|
DWORD count;
|
||||||
return 0;
|
if ( ReadFile( fd, buf, len, &count, 0 ) ) {
|
||||||
|
buf[count] = '\0';
|
||||||
|
|
||||||
|
return count;
|
||||||
|
} else {
|
||||||
|
LPVOID lpMsgBuf;
|
||||||
|
FormatMessage(
|
||||||
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
|
NULL,
|
||||||
|
GetLastError(),
|
||||||
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
||||||
|
(LPTSTR) &lpMsgBuf,
|
||||||
|
0,
|
||||||
|
NULL );
|
||||||
|
|
||||||
|
SG_LOG( SG_IO, SG_ALERT, "Serial I/O read error: "
|
||||||
|
<< (const char*) lpMsgBuf );
|
||||||
|
LocalFree( lpMsgBuf );
|
||||||
|
|
||||||
|
buf[0] = '\0';
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
@@ -299,16 +385,15 @@ int SGSerialPort::write_port(const string& value) {
|
|||||||
|
|
||||||
#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
|
#if defined( WIN32 ) && !defined( __CYGWIN__) && !defined( __CYGWIN32__ )
|
||||||
|
|
||||||
LPCVOID lpBuffer = value.c_str();
|
LPCVOID lpBuffer = value.data();
|
||||||
DWORD nNumberOfBytesToWrite = value.length();
|
DWORD nNumberOfBytesToWrite = value.length();
|
||||||
DWORD lpNumberOfBytesWritten;
|
DWORD lpNumberOfBytesWritten;
|
||||||
OVERLAPPED lpOverlapped;
|
|
||||||
|
|
||||||
if ( WriteFile( fd,
|
if ( WriteFile( fd,
|
||||||
lpBuffer,
|
lpBuffer,
|
||||||
nNumberOfBytesToWrite,
|
nNumberOfBytesToWrite,
|
||||||
&lpNumberOfBytesWritten,
|
&lpNumberOfBytesWritten,
|
||||||
&lpOverlapped ) == 0 )
|
0 ) == 0 )
|
||||||
{
|
{
|
||||||
LPVOID lpMsgBuf;
|
LPVOID lpMsgBuf;
|
||||||
FormatMessage(
|
FormatMessage(
|
||||||
@@ -377,13 +462,12 @@ int SGSerialPort::write_port(const char* buf, int len) {
|
|||||||
LPCVOID lpBuffer = buf;
|
LPCVOID lpBuffer = buf;
|
||||||
DWORD nNumberOfBytesToWrite = len;
|
DWORD nNumberOfBytesToWrite = len;
|
||||||
DWORD lpNumberOfBytesWritten;
|
DWORD lpNumberOfBytesWritten;
|
||||||
OVERLAPPED lpOverlapped;
|
|
||||||
|
|
||||||
if ( WriteFile( fd,
|
if ( WriteFile( fd,
|
||||||
lpBuffer,
|
lpBuffer,
|
||||||
nNumberOfBytesToWrite,
|
nNumberOfBytesToWrite,
|
||||||
&lpNumberOfBytesWritten,
|
&lpNumberOfBytesWritten,
|
||||||
&lpOverlapped ) == 0 )
|
0 ) == 0 )
|
||||||
{
|
{
|
||||||
LPVOID lpMsgBuf;
|
LPVOID lpMsgBuf;
|
||||||
FormatMessage(
|
FormatMessage(
|
||||||
|
|||||||
Reference in New Issue
Block a user