我有这个程序,它使用 winapi 函数从串行端口读取:
#include <string>
#include <Windows.h>
using std::cin;
using std::cout;
DCB blank_dcb = {0};
int main()
{
int ierr, jsu, jcs, jto, jrf, nt;
unsigned char readfile_chars[80];
DWORD nr;
std::string s;
HANDLE port_h;
COMMTIMEOUTS port_timeouts;
DCB dcb;
port_h = CreateFile("COM6", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(INVALID_HANDLE_VALUE == port_h)
{
cout << "Failed to open COM6.\n";
cout << "Press ENTER to exit.\n";
getline(cin, s);
return -1;
}
//
// Call SetupComm().
//
jsu = SetupComm(port_h, 1024, 1024);
if(0 == jsu)
{
cout << "Error while calling SetupComm(): ";
ierr = GetLastError();
cout << ierr << "\n";
cout << "Press ENTER to exit.\n";
getline(cin, s);
return -1;
}
//
// Set the comm state.
//
dcb = blank_dcb;
dcb.DCBlength = sizeof(DCB);
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.fBinary = TRUE;
jcs = (int) SetCommState(port_h,&dcb);
if(0 == jcs)
{
cout << "Error while setting comm state.\n";
cout << "Press ENTER to exit.\n";
getline(cin, s);
return -1;
}
//
// Set the port's timeouts.
//
port_timeouts.ReadIntervalTimeout = MAXDWORD;
port_timeouts.ReadTotalTimeoutConstant = 0;
port_timeouts.ReadTotalTimeoutMultiplier = 0;
port_timeouts.WriteTotalTimeoutConstant = 0;
port_timeouts.WriteTotalTimeoutMultiplier = 0;
jto = SetCommTimeouts(port_h, &port_timeouts);
if(0 == jto)
{
cout << "Error setting comm timeouts.\n";
cout << "Press ENTER to exit.\n";
getline(cin, s);
return -1;
}
Sleep(5000);
nt = 9;
jrf = ReadFile(port_h, (LPVOID) &readfile_chars[0], nt, &nr, NULL);
if(0 == jrf)
{
cout << "ReadFile() failed.\n";
}
cout << "Read " << nr << " characters.\n";
readfile_chars[nr] = '\0';
cout << "Characters read: " << readfile_chars << "\n";
cout << "Press ENTER to exit.\n";
getline(cin, s);
return 1;
}
而这个使用 Boost.Asio 的程序:
#include <iostream>
#include <string>
#define _WIN32_WINNT 0x0601
#include <boost/asio.hpp>
using std::cin;
using std::cout;
int main()
{
char message[80];
std::string s;
boost::asio::io_service io_svc;
boost::asio::serial_port com6(io_svc, "COM6");
com6.set_option(boost::asio::serial_port_base::baud_rate(9600));
com6.set_option(boost::asio::serial_port_base::character_size(8));
com6.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
com6.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
com6.set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::none));
boost::asio::read(com6, boost::asio::buffer(&message[0],9));
message[9] = '\0';
cout << "Characters received: " << message << "\n";
cout << "Press ENTER to exit.\n";
getline(cin, s);
}
我有一个通过 RS232 端口传输 ASCII 字符的数字电平。
数字水平仪成功地将字符传输到 Putty 窗口。
升压程序成功地从通过串行电缆连接到 COM6 的外部 PC 上的超级终端窗口读取字符。
升压程序成功地从数字级别读取字符。
winapi 程序从外部 PC 上的超级终端窗口成功读取字符。
winapi 程序不会从数字级别读取字符。函数 ReadFile() 成功返回,接收的字符数设置为零,表明它认为数字级别处于文件末尾。
为什么boost程序从数字级别读取成功,而winapi程序却没有?