我有一個類似的經驗,FTDI設備會進入一個狀態,我不能與它溝通,除非我物理拔掉它。格雷格的回答幫助我想出了一個解決方法。
Greg對FTDI託管包裝的引用非常有幫助,但是他提供的代碼片段並不完全,因爲實際引用FTDI設備需要更多的代碼。以他的想法爲出發點,我調整了來自FTDI的一些示例代碼並編寫了該函數。它能夠將我的FTDI設備恢復到工作狀態,無需物理干預。
/// <summary>
/// Attempts to reset an attached FTDI device and returns true if successful. Note that a
/// 5-8 second delay is recommended after the reset.
/// </summary>
/// <returns></returns>
private bool ResetFTDI() {
UInt32 ftdiDeviceCount = 0;
FTD2XX_NET.FTDI.FT_STATUS ftStatus = FTD2XX_NET.FTDI.FT_STATUS.FT_OK;
// Create new instance of the FTDI device class
FTD2XX_NET.FTDI myFtdiDevice = new FTD2XX_NET.FTDI();
// Determine the number of FTDI devices connected to the machine
ftStatus = myFtdiDevice.GetNumberOfDevices(ref ftdiDeviceCount);
// Check status
if (ftStatus != FTD2XX_NET.FTDI.FT_STATUS.FT_OK) {
Log.WriteLog("Failed to get number of FTDI devices [" + ftStatus.ToString() + "]");
return false;
}
// If no devices available, return
if (ftdiDeviceCount == 0) {
Log.WriteLog("Failed to find any FTDI devices [" + ftStatus.ToString() + "]");
return false;
}
// Allocate storage for device info list
FTD2XX_NET.FTDI.FT_DEVICE_INFO_NODE[] ftdiDeviceList = new FTD2XX_NET.FTDI.FT_DEVICE_INFO_NODE[ftdiDeviceCount];
// Populate our device list
ftStatus = myFtdiDevice.GetDeviceList(ftdiDeviceList);
if (ftStatus != FTD2XX_NET.FTDI.FT_STATUS.FT_OK) {
Log.WriteLog("Failed enumerate FTDI devices [" + ftStatus.ToString() + "]");
return false;
}
// Open first device in our list by serial number
ftStatus = myFtdiDevice.OpenBySerialNumber(ftdiDeviceList[0].SerialNumber);
if (ftStatus != FTD2XX_NET.FTDI.FT_STATUS.FT_OK) {
Log.WriteLog("Failed to open device [" + ftStatus.ToString() + "]");
return false;
}
// Finally, reset the port
myFtdiDevice.CyclePort();
return true;
}
上面的日誌對象是我的項目的內部,所以替代任何適合你。
一些進一步的研究也出現了this question。在答案中,Zach Saw引用了他在.NET SerialPort通信中發現的一個問題。我會嘗試他的解決方案,如果能夠完全解決問題,我會在這裏回覆,因爲我認爲上述內容是一種帶band的方式。
你得到了什麼錯誤(你在哪裏得到它)? – KevinDTimm 2010-12-06 18:44:29
它工作得很好,但是當我重新啓動我的modbus奴隸,並嘗試再次讀取,然後我有個例外,這個COM端口被拒絕 – kosnkov 2010-12-06 18:59:53