COM 端口被拒绝 [英] COM port is denied

查看:81
本文介绍了COM 端口被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用 COM 端口使用 modbus 协议读取一些寄存器,一切正常,直到我重新启动 modbus 从设备,然后我有错误 com 被拒绝,我可以做的是或重新安装计算机或拔出并回到'usb to com转换器'.似乎此设备无法正确处理 com 端口.

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;
   }
}

推荐答案

如果您使用的是 FTDI USB/串行适配器,您可以直接从托管包装器中检索状态 (FTDI Managed Driver Wrapper)并根据连接状态重新初始化您的串行端口.

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.

请原谅我缺乏使用 FTDI 设备的经验,但这应该会重置您的 R-232 适配器:

Forgive my lack of experiance with FTDI devices, but this should reset your R-232 adapter:

FTD2XX_NET.FTDI device = new FTD2XX_NET.FTDI();
string port;
device.GetCOMPort(out port);

if (!string.IsNullOrEmpty(port) && (port.Equals(target)) && device.IsOpen)
{
    device.CyclePort();
    device.ResetDevice();
    device.ResetPort();
}

据我了解,device.CyclePort() 将关闭任何活动连接(调用 FT_CLOSE),卸载 USB 设备,并从 USB 总线重新枚举设备.这应该与您物理移除并重新插入适配器完全相同.

By my understanding device.CyclePort() will close any active connection (calls FT_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.

另外,根据 FTDI 设备库的 Perl 包装器的文档:

Also, according to the documentation for the Perl wrapper for the FTDI device library:

与其他总线控制一样,有一个等待 5-8 秒后任何 API 调用的 CyclePort需要直接连接到设备,例如 GetSerialByIndex()等,将因 FT_INVALID_HANDLE 而失败直到完全稳定.申请应考虑这个等待期,或设置轮询循环检测回报的变化状态.

As with other bus controls, there is a wait period of 5-8 seconds after a CyclePort where any API call that requires direct connection to the device, like GetSerialByIndex() etc, will fail with FT_INVALID_HANDLE until it has completely stabilized. The application should account for this wait period, or setup a polling loop to detect the change in return status.

这篇关于COM 端口被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆