HANDLE hComm;
std::string port_name = "\\\\.\\COM19"; //change port name
char write_buffer[100];
char read_buffer[100];
DWORD dNoOFBytestoWrite; // No of bytes to write into the port
DWORD dNoOfBytesWritten = 0; // No of bytes written to the port
DWORD bytes_read ;
hComm = CreateFileA(port_name.c_str(), //port name
GENERIC_READ | GENERIC_WRITE, //Read/Write
0, // No Sharing
NULL, // No Security
OPEN_EXISTING,// Open existing port only
0, // Non Overlapped I/O
NULL); // Null for Comm Devices
if (hComm == INVALID_HANDLE_VALUE)
{
std::cerr << "Error in opening serial port”";
return -1;
}
else
std::cerr << "opening serial port successful";
DCB dcbSerialParams = { 0 }; // Initializing DCB structure
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
GetCommState(hComm, &dcbSerialParams);
dcbSerialParams.BaudRate = CBR_9600; // Setting BaudRate = 9600
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT;// Setting StopBits = 1
dcbSerialParams.Parity = NOPARITY; // Setting Parity = None
SetCommState(hComm, &dcbSerialParams);
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 20; // in milliseconds
timeouts.ReadTotalTimeoutConstant = 100; // in milliseconds
timeouts.ReadTotalTimeoutMultiplier = 10; // in milliseconds
timeouts.WriteTotalTimeoutConstant = 100; // in milliseconds
timeouts.WriteTotalTimeoutMultiplier = 10; // in milliseconds
SetCommTimeouts(hComm, &timeouts); // 将超时参数写入设备控制
dNoOFBytestoWrite = sizeof(write_buffer);
WriteFile(hComm, // Handle to the Serial port
write_buffer, // Data to be written to the port
dNoOFBytestoWrite, //No of bytes to write
&dNoOfBytesWritten, //Bytes written
NULL);
Sleep(100);
do
{
ReadFile(hComm, //Handle of the Serial port
&read_buffer, //Temporary character
100, //Size of TempChar
&bytes_read, //Number of bytes read
NULL);
} while (bytes_read <= 0);
char dst[100];
ascii2hex(read_buffer, dst, 100);
cout << endl;
cout << "Read Data : " <<dst << endl;
CloseHandle(hComm);
readfile无论怎么设置都只能接收8个字符,不知道是啥情况?
我用串口调试助手,却没有问题