COM 端口被拒绝
您好,我正在尝试使用 COM 端口使用 modbus 协议读取一些寄存器,一切正常,直到我重新启动 modbus 从设备,然后出现 com 被拒绝的错误,我能做的就是重新启动计算机或拔出并重新插入' USB 到 COM 转换器'。似乎该设备无法正确处理 com 端口。
using (port = new SerialPort(comPort))
{
ushort[] registers = null;
try
{
port.BaudRate = boudRate;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
// modbus reading registers
port.Close();
return registers;
}
catch (Exception e)
{
Logs.AddToLog(e.Message);
return registers;
}
}
Hi I am trying to use COM port to read some registers using modbus protocol, everything works fine until I rebote modbus slave device, then I have error that com is denied, what I can do is or rebot computer or plug out and back in 'usb to com converter'. Seems that this device doesn't handle with com port properly.
using (port = new SerialPort(comPort))
{
ushort[] registers = null;
try
{
port.BaudRate = boudRate;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
// modbus reading registers
port.Close();
return registers;
}
catch (Exception e)
{
Logs.AddToLog(e.Message);
return registers;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 FTDI USB/串行适配器,则可以直接从托管包装器检索状态 (FTDI 托管驱动程序包装器)并根据连接状态重新初始化串行端口。
请原谅我缺乏 FTDI 设备的经验,但这应该重置您的 R-232 适配器:
据我了解
device.CyclePort()
将关闭任何活动连接(调用FT_CLOSE
) ,卸载 USB 设备,并从 USB 总线重新枚举该设备。这应该与您物理移除并重新插入适配器的情况完全相同。另外,根据 FTDI 设备库的 Perl 包装器的文档:
If you are using an FTDI USB/Serial adapter, you can retrieve the state directly from the managed wrapper (FTDI Managed Driver Wrapper) and reinitialize your serial port based on the connected state.
Forgive my lack of experiance with FTDI devices, but this should reset your R-232 adapter:
By my understanding
device.CyclePort()
will close any active connection (callsFT_CLOSE
), unmounts the usb device, and reenumerates the device from the usb bus. This should be exactly the same as if you physically removed, and reinserted the adapter.Also, according to the documentation for the Perl wrapper for the FTDI device library:
我有过类似的经历,FTDI 设备会进入一种状态,除非我物理拔掉它,否则我无法与其通信。格雷格的回答帮助我想出了一个解决方法。
Greg 对 FTDI 托管包装器的引用非常有帮助,但他提供的代码片段并不完整,因为实际引用 FTDI 设备需要更多代码。以他的想法为起点,我改编了 FTDI 的一些示例代码并编写了这个函数。它能够让我的 FTDI 设备恢复工作状态,无需物理干预。
上面的 Log 对象是我的项目的内部对象,因此可以替换任何适合您的对象。
一些进一步的研究也发现了这个问题。在答案中,Zach Saw 提到了他在 .NET SerialPort 通信中发现的一个问题。我将尝试他的解决方案,如果这完全解决了问题,我将在这里发帖,因为我认为上述内容有点像创可贴。
I had a similar experience where an FTDI device would enter a state where I couldn't communicate with it unless I physically unplugged it. Greg's answer helped me come up with a workaround.
Greg's reference to FTDI's managed wrapper was very helpful, but the snippet he provided isn't quite there because it takes a bit more code to actually reference an FTDI device. With his idea as a starting point, I adapted some example code from FTDI and wrote this function. It was able to put my FTDI device back into a working state without physical intervention.
The Log object above is internal to my project so substitute whatever suits you.
Some further research also turned up this question. In the answers, Zach Saw references a problem he discovered with .NET SerialPort communications. I'm going to try his solution and I'll post back here if that resolves the problem entirely because I consider the above to be something of a bandaid.