我正在创建一个通过 FT2232H USB/RS232 转换器与设备通信的应用程序。为了进行通信,我使用 FTDI 网站上的 FTD2XX_NET.dll 库。
我正在使用两个线程:
第一个线程不断从设备读取数据
第二个线程是Windows窗体应用程序的主线程
当我尝试在接收器线程运行时将任何数据写入设备时遇到问题。主线程只是挂在 ftdiDevice.Write 函数上。
我尝试同步两个线程,以便只有一个线程可以同时使用读/写功能,但这没有帮助。
下面的代码负责通信。请注意,以下函数是 FtdiPort 类的方法。
接收者线程
private void receiverLoop()
{
if (this.DataReceivedHandler == null)
{
throw new BackendException("dataReceived delegate is not set");
}
FTDI.FT_STATUS ftStatus = FTDI.FT_STATUS.FT_OK;
byte[] readBytes = new byte[this.ReadBufferSize];
while (true)
{
lock (FtdiPort.threadLocker)
{
UInt32 numBytesRead = 0;
ftStatus = ftdiDevice.Read(readBytes, this.ReadBufferSize, ref numBytesRead);
if (ftStatus == FTDI.FT_STATUS.FT_OK)
{
this.DataReceivedHandler(readBytes, numBytesRead);
}
else
{
Trace.WriteLine(String.Format("Couldn't read data from ftdi: status {0}", ftStatus));
Thread.Sleep(10);
}
}
Thread.Sleep(this.RXThreadDelay);
}
}
编写从主线程调用的函数
public void Write(byte[] data, int length)
{
if (this.IsOpened)
{
uint i = 0;
lock (FtdiPort.threadLocker)
{
this.ftdiDevice.Write(data, length, ref i);
}
Thread.Sleep(1);
if (i != (int)length)
{
throw new BackendException("Couldnt send all data");
}
}
else
{
throw new BackendException("Port is closed");
}
}
另一种方法是使用 FTDI 的事件通知机制,这样您就不需要阻塞线程来读取数据:
public FTDISample()
{
private AutoResetEvent receivedDataEvent;
private BackgroundWorker dataReceivedHandler;
private FTDI ftdi;
public FTDISample(string serialNumber){
ftdi = new FTDI();
FTDI.FT_STATUS status = ftdi.OpenBySerialNumber(serialNumber);
receivedDataEvent = new AutoResetEvent(false);
status = mFTDI.SetEventNotification(FTDI.FT_EVENTS.FT_EVENT_RXCHAR, receivedDataEvent);
dataReceivedHandler = new BackgroundWorker();
dataReceivedHandler.DoWork += ReadData;
if (!dataReceivedHandler.IsBusy)
{
dataReceivedHandler.RunWorkerAsync();
}
}
private void ReadData(object pSender, DoWorkEventArgs pEventArgs)
{
UInt32 nrOfBytesAvailable = 0;
while (true)
{
// wait until event is fired
this.receivedDataEvent.WaitOne();
// try to recieve data now
FTDI.FT_STATUS status = ftdi.GetRxBytesAvailable(ref nrOfBytesAvailable);
if (status != FTDI.FT_STATUS.FT_OK)
{
break;
}
if (nrOfBytesAvailable > 0)
{
byte[] readData = new byte[nrOfBytesAvailable];
UInt32 numBytesRead = 0;
status = mFTDI.Read(readData, nrOfBytesAvailable, ref numBytesRead);
// invoke your own event handler for data received...
//InvokeCharacterReceivedEvent(fParsedData);
}
}
}
public bool Write(string data)
{
UInt32 numBytesWritten = 0;
ASCIIEncoding enconding = new ASCIIEncoding();
byte[] bytes = enconding.GetBytes(data);
FTDI.FT_STATUS status = ftdi.Write(bytes, bytes.Length, ref numBytesWritten);
if (status != FTDI.FT_STATUS.FT_OK)
{
Debug.WriteLine("FTDI Write Status ERROR: " + status);
return false;
}
if (numBytesWritten < data.Length)
{
Debug.WriteLine("FTDI Write Length ERROR: " + status + " length " + data.Length +
" written " + numBytesWritten);
return false;
}
return true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)