c-关闭串行端口设备的句柄花费太长时间,如何使其更快



我创建了一个按钮,它发送字节来打开多个类似设备的LED。我发现,如果我不关上手柄,我会得到即时速度,这正是我所需要的,但当然,如果我没有关上它,我就不能再使用它了,这意味着按钮只工作一次。如果我真的关上把手,它就会工作,但速度要慢得多,我可以看到LED一个接一个地亮起。怎么办?

这是sendBytes函数的代码,GUI的代码是不必要的,因为我使用barebones C控制台程序可以获得相同的速度。

int sendBytes(char* command, char* COM) {
HANDLE hSerial2;
BOOL Write_Status;
DCB dcbSerialParams = { 0 };                    // Initializing DCB structure
hSerial2 = CreateFileA(COM,
GENERIC_READ | GENERIC_WRITE,
0,    // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0,    // not overlapped I/O
NULL  // hTemplate must be NULL for comm devices
);
printf("opening serial port successful");
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
Write_Status = GetCommState(hSerial2, &dcbSerialParams);     //retreives  the current settings
if (Write_Status == FALSE) {
printf("n   Error! in GetCommState()");
CloseHandle(hSerial2);
return 1;
}
dcbSerialParams.BaudRate = CBR_57600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
Write_Status = SetCommState(hSerial2, &dcbSerialParams);  //Configuring the port according to settings in DCB
if (Write_Status == FALSE)
{
printf("n   Error! in Setting DCB Structure");
CloseHandle(hSerial2);
return 1;
}
// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (SetCommTimeouts(hSerial2, &timeouts) == 0)
{
//  printf("Error setting timeoutsn");
CloseHandle(hSerial2);
return 1;
}
///*----------------------------- Writing a Character to Serial Port----------------------------------------*/
int length = strlen(command);
char send[20];
strcpy(send, command);
send[length + 1] = 13;
send[length + 2] = 10;
DWORD  dNoOFBytestoWrite;              // No of bytes to write into the port
DWORD  dNoOfBytesWritten = 0;          // No of bytes written to the port
dNoOFBytestoWrite = length + 2; // Calculating the no of bytes to write into the port

if (!WriteFile(hSerial2, send, dNoOFBytestoWrite,
&dNoOfBytesWritten, NULL))
{
printf("Error writing text to %sn", COM);
}
else
{
printf("n %d bytes written to %sn", dNoOfBytesWritten, COM);
}

CloseHandle(hSerial2);//Closing the Serial Port
printf("n ==========================================n");

return 0;
}

我设法找到了一个解决方案,它相当简单。

我所做的只是为每个设备创建一个HANDLE变量,用相应的COM端口初始化它们,并将该HANDLE传递给一个稍微修改过的sendBytes函数,这样它就不必在每次按下按钮时关闭并创建一个新的。

现在的问题是读取字节很慢,下面是代码:

int sendBytes(char* command, char* COM, HANDLE hSerial, int read) {
BOOL Write_Status;
DCB dcbSerialParams = { 0 };                    // Initializing DCB structure

dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
Write_Status = GetCommState(hSerial, &dcbSerialParams);     //retreives  the current settings
if (Write_Status == FALSE) {
printf("n   Error! in GetCommState()");
CloseHandle(hSerial);
return 1;
}

dcbSerialParams.BaudRate = CBR_57600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
Write_Status = SetCommState(hSerial, &dcbSerialParams);  //Configuring the port according to settings in DCB
if (Write_Status == FALSE)
{
CloseHandle(hSerial);
return 1;
}
///*----------------------------- Writing a Character to Serial Port----------------------------------------*/
int length = strlen(command);
char send[20];
strcpy(send, command);
send[length + 1] = 13;
send[length + 2] = 10;
DWORD  dNoOFBytestoWrite;              // No of bytes to write into the port
DWORD  dNoOfBytesWritten = 0;          // No of bytes written to the port
dNoOFBytestoWrite = length + 2; // Calculating the no of bytes to write into the port
if (!WriteFile(hSerial, send, dNoOFBytestoWrite, &dNoOfBytesWritten, NULL))
printf("Error writing text to %sn", COM);
if (read) {
int maxChars = 256;
BOOL  Read_Status;                      // Status of the various operations 
DWORD dwEventMask;                      // Event mask to trigger
char  SerialBuffer[256];               // Buffer Containing Rxed Data
DWORD NoBytesRead;                     // Bytes read by ReadFile()
///*------------------------------------ Setting Receive Mask ----------------------------------------------*/
Read_Status = SetCommMask(hSerial, EV_RXCHAR); //Configure Windows to Monitor the serial device for Character Reception
if (Read_Status == FALSE)
printf("nn    Error! in Setting CommMask");
else
printf("nn    Setting CommMask successfull");

///*------------------------------------ Setting WaitComm() Event   ----------------------------------------*/
printf("nn    Waiting for Data Reception");
Read_Status = WaitCommEvent(hSerial, &dwEventMask, NULL); //Wait for the character to be received
// /*-------------------------- Program will Wait here till a Character is received ------------------------*/
if (Read_Status == FALSE)
{
printf("n    Error! in Setting WaitCommEvent()");
}
else //If  WaitCommEvent()==True Read the RXed data using ReadFile();
{
printf("nn    Characters Received t");

if (!ReadFile(hSerial, SerialBuffer, maxChars, &NoBytesRead, NULL))
{
printf("wrong character");
return 1;
}
printf("noBytes: %dn", NoBytesRead);
/*------------Printing the RXed String to Console----------------------*/
int j = 0;
char readArray[256];
while (SerialBuffer[j] != 'r') {
printf("%c", SerialBuffer[j]);
readArray[j] = SerialBuffer[j];
j++;
}
readArray[j++] = '';

}
}
}

最新更新