我有一个C程序,它打开COM端口的句柄,向它写入一些字节,读取一些字节,然后关闭句柄并退出。但是,当我连续运行10次这样的程序时,它开始需要很长时间才能完成GetCommState
功能并陷入SetCommState
功能。在C#中使用简单的SerialPort
对象也会发生同样的事情。
我能找到的唯一解决办法是将设备重新连接到端口。是否有一些更优雅的方法来摆脱这种冻结?它可能只是一些PC配置错误吗?
更新
我重写了代码,使其使用DeviceIoControl
代替SetCommState
。但是,这里的问题完全相同。
device =
CreateFileW(
L"\\\\.\\COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, NULL);
static int SetBaudRate (HANDLE device) {
int error = 0;
int success = 0;
OVERLAPPED overlapped = {0};
overlapped.hEvent = CreateEvent(NULL, TRUE, 0, NULL);
if (overlapped.hEvent) {
SERIAL_BAUD_RATE baudRate = {0};
baudRate.BaudRate = SERIAL_BAUD_115200;
error =
DeviceIoControl(
device, IOCTL_SERIAL_SET_BAUD_RATE, &baudRate,
sizeof(SERIAL_BAUD_RATE), NULL, 0, NULL, &overlapped);
if (error || (!error && GetLastError() == ERROR_IO_PENDING)) {
DWORD bytes = 0;
if (GetOverlappedResult(device, &overlapped, &bytes, TRUE)) {
success = 1;
}
}
}
CloseHandle(overlapped.hEvent);
return success;
}
第一个问题:DeviceIoControl
没有立即返回(虽然是异步调用)并且挂起大约两分钟。
第二个问题:在这两分钟后失败,错误代码为121(ERR_SEM_TIMEOUT:“信号量超时时间已过期。”)。
usbser.sys
更新2
样本C#代码也会冻结(如上面的C程序):
using System;
using System.IO.Ports;
sealed class Program {
static void Main (string[] args) {
int i = 0;
while (true) {
Console.WriteLine(++i);
SerialPort p =
new SerialPort("com3", 115200, Parity.None, 8, StopBits.One);
p.DtrEnable = true;
p.RtsEnable = true;
p.ParityReplace = 0;
p.WriteTimeout = 10000;
p.ReadTimeout = 3000;
try {
p.Open();
Console.WriteLine("Success!");
} catch (Exception e) {
Console.WriteLine(e.GetType().Name + ": " + e.Message);
}
p.Close();
Console.ReadLine();
}
}
}
示例输出如下:
1 (device not yet connected)
IOException: The port 'com3' does not exist.
2 (device connected but not yet in windows device manager)
IOException: The port 'com3' does not exist.
3
IOException: The port 'com3' does not exist.
4 (device connected and recognized)
Success!
5
Success!
[...] (with about one second between each enter press)
15
Success!
16 (device still connected and recognized - nothing touched! after two minutes of freeze, semaphore timeout exactly as in the C version)
IOException: The semaphore timeout period has expired.
17 (device disconnected during the two minutes of freeze. it then returns instantly)
IOException: A device attached to the system is not functioning.
18 (device still disconnected - note that the exception is a different one than the one in the beginning although it's the same case: device not connected)
IOException: The specified port does not exist.
19
IOException: The port 'com3' does not exist.
答案 0 :(得分:0)
这是我的c ++类的一个工作部分,用于处理串口通信的变化,以适应C
和您的需求。如果它不起作用,那么它可能是你的虚拟端口驱动程序错误
DCB SerialPortSettings;
HANDLE SerialPort;
int OpenPort(WCHAR* PortName,int BaudRate)
{
SerialPort = CreateFileW(PortName,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
if(SerialPort==INVALID_HANDLE_VALUE)
return -2;
RtlZeroMemory(&SerialPortSettings,sizeof(DCB));
SerialPortSettings.DCBlength = sizeof(DCB);
if(!GetCommState(SerialPort,&SerialPortSettings))
{
CloseHandle(SerialPort);
return -3;
}
//8n1 RS485
SerialPortSettings.BaudRate = BaudRate;
SerialPortSettings.ByteSize = 8;
SerialPortSettings.Parity = NOPARITY;
SerialPortSettings.StopBits = ONESTOPBIT;
SerialPortSettings.fRtsControl = RTS_CONTROL_TOGGLE;
if(!SetCommState(SerialPort,&SerialPortSettings))
{
CloseHandle(SerialPort);
return -4;
}
return 0;
}
编辑:尝试从http://www.ftdichip.com/Drivers/VCP.htm下载驱动程序,因为您所描述的问题很可能是驱动程序或设备问题。为Etan工作:)