COM端口被拒绝 [英] COM port is denied
问题描述
您好我想使用COM端口读取采用modbus协议的一些寄存器,一切,直到我rebote Modbus从站设备工作正常,那么我有一个COM是被拒绝的错误,有什么我能做的就是或rebot计算机或拔出和早在USB至COM转换器。看来,这个设备不具有串口妥善处理。
使用(端口=新的SerialPort(相称))
{
USHORT []寄存器= NULL;
试
{
port.BaudRate = boudRate;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();
// MODBUS读寄存器
port.Close();
返回寄存器;
}
赶上(例外五)
{
Logs.AddToLog(e.Message);
返回寄存器;
}
}
如果您正在使用FTDI USB /串口适配器,可以直接从托管包装(的 FTDI管理驱动封装)并重新初始化基于连接状态的串行端口。
原谅我缺乏与FTDI设备的experiance,但这应该重置您的R-232适配器:
FTD2XX_NET.FTDI设备=新FTD2XX_NET.FTDI();
线口;
device.GetCOMPort(输出端口);
如果(string.IsNullOrEmpty(端口)及!及(port.Equals(目标))及和放大器; device.IsOpen)
{
device.CyclePort( );
device.ResetDevice();
device.ResetPort();
}
这是我的理解 device.CyclePort()
将关闭所有活动连接(电话 FT_CLOSE
),卸载USB设备,并从USB总线重新枚举该设备。就好象您物理移除,并且重新插入适配器这应该是完全相同的
此外,根据用于Perl包装为FTDI装置库中的文件:
与其他总线的控制,有5-8秒的
CyclePort哪里任何API后一个
等待期间调用
需要直接连接到
设备,像GetSerialByIndex()
等,将与FT_INVALID_HANDLE
失败,直到它已完全稳定。
申请应占
这个观望期,或设置轮询
环路检测返回
状态的变化。
块引用>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; } }
解决方案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:
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(); }
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:
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屋!