FT_Read失败,除非逐字节读取,其中每个FT_Read后面跟着FT_Purge
FT_Read fails, UNLESS reading byte-by-byte where every FT_Read is followed by FT_Purge
问题:微控制器在调试器控制下为循环传输10个字节(ASCII A、B、C、D、E、F、G、H、I、J)。Windows应用程序(下面抽象的C++/CLI代码)应该接收这些字节。
参考两次不同的FT_Read尝试,第一次在while循环中,第二次在循环中
案例#1:一次性执行循环的微控制器,While循环退出时,RxMessage数组将第一个字节正确保存为"A",其余九个字节为垃圾。其中fsuccess返回为FT_OK,TotalRxBytes=10
情况#2:进入微控制器,使循环逐字节传输,While循环退出,RxMessage数组保持"A"、"0xFF"、"B"、0xFF、"C"、"0"、"D"、"x"、"E"answers"0"。而fsuccess返回为FT_OK和TotalRxBytes=10
案例#3:进入微控制器,循环逐字节传输。一次性执行Windows应用程序For循环。Windows应用程序For循环退出时,RxMessage将所有10个字节正确保存为"A"、"B"、"C"、"D"、"E"、"F"、"G"、"H"、"I"、"J"。
注意:在情况1&2,循环的微控制器需要5次迭代才能使Windows应用程序循环退出,就像"?"在每个微控制器中发送0xFF用于循环迭代。而在案例#3中,循环的微控制器正好需要10次迭代才能退出Windows应用程序循环,就好像FT_Read+FT_Purge删除了不需要的0xFF块
private:
/// Required designer variable.
PVOID fth;
BOOL fSuccess, fthsuccess;
array<wchar_t> ^ TxMessage;
array<unsigned char> ^ RxMessage;
Form1(void) //Constructor
{
fthsuccess = false;
InitializeComponent();
TxMessage = gcnew array<wchar_t> (12);
RxMessage = gcnew array<unsigned char> (12);
}
/*PS. All the FT_xxxx calls below are tested for fsuccess before proceeding ahead */
FT_Open(0, ppfthandle);
FT_SetBaudRate(*ppfthandle, 9600);
unsigned char LatencyTimer;
FT_SetLatencyTimer(*ppfthandle, 2);
FT_GetLatencyTimer(*ppfthandle, &LatencyTimer);
FT_SetDataCharacteristics(*ppfthandle, FT_BITS_8, FT_STOP_BITS_1, FT_PARITY_NONE);
FT_SetTimeouts(*ppfthandle, 10000/*read*/, 1000/*write*/);
if(fthsuccess == true)
{
pin_ptr<FT_HANDLE> ppfthandle = &fth;
pin_ptr<wchar_t> ppTx = &TxMessage[0];
fSuccess = FT_Write(*ppfthandle,&ppTx[0],4,&dwhandled);
/*in absence of Purge below, Tx chars echo as part of Rx chars
ultimately workaround would be needed to avoid Purging of
real time RxData at runtime
*/
FT_Purge(*ppfthandle, FT_PURGE_RX | FT_PURGE_TX);
pin_ptr<unsigned char> ppRx = &RxMessage[0];
DWORD RxBytes,TotalRxBytes;
RxBytes=TotalRxBytes=0;
while(TotalRxBytes<10){
FT_GetQueueStatus(*ppfthandle,&RxBytes);
fSuccess = FT_Read(*ppfthandle,ppRx,RxBytes,&dwhandled);//reading 10 bytes in one go
if(fSuccess != FT_OK){
break;
}
TotalRxBytes += dwhandled;
ppRx = &RxMessage[TotalRxBytes];
}
fSuccess = FT_Purge(*ppfthandle, FT_PURGE_RX | FT_PURGE_TX);
ppRx = &RxMessage[0];//reset ppRx and attempt read in for-loop, the same bytes from micro-controller
for(int i=0;i<10;i++)//reading byte-by-byte in debug steps
{
fSuccess = FT_Read(*ppfthandle,&ppRx[i],1,&dwhandled);
/*in absence of Purge below, alternate characters are read as 0xFF
ultimately workaround would be needed to avoid Purging of
real time RxData at runtime
*/
FT_Purge(*ppfthandle, FT_PURGE_RX | FT_PURGE_TX);
}
}// if (!fthsuccess)
下面的微控制器代码片段:
uint8_t Series[10]={'A','B','C','D','E','F','G','H','I','J'};
for(loopcount=0;loopcount<10;loopcount++)
{
UART1Send(Series+loopcount,1);
}
双击FT_SetDataCharacteristics(*ppfthandle、FT_BITS_8、FT_STOP_BITS_1、FT_PARITY_NONE)是否正确。这两个设备之间的任何不匹配都会导致问题。
- 使用 ImageIO.read() 生成的图像是否仍然使用传递给它的相同内存?
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- write() 和 read() 中几乎没有混淆
- C++ TCP 套接字通信 - 连接按预期工作,几秒钟后失败,没有收到新数据,read() 和 recv() 块
- 为什么调试器引发"read access violation. this was nullptr"异常?
- 对同一文件使用 .read 的次数是否有限制?
- Win32 API - HWND "{unused = ???} Unable to read memory"错误
- 为什么我会"Invalid read of size 8"?(瓦尔格林德)
- QDataStream read to QVector
- asio::read() 需要很长时间,使用 asio::write 没有问题
- Read OpenCV Mat 16bit to QImage 8bit Greyscale
- 如果 I/O read() 处于阻塞阶段,如何使用 Ctrl+C 退出 C++ 程序?
- 如何将 (serverClients[i].read() 传递给 ESP8266 上的字节数组
- 将 Lora.read() 转储到数组
- 无法通过 FUSE 文件系统"read"任何内容
- 指向 std::unrodered_map 中元素的指针返回'Read access violation'
- 封送指向结构异常"cannot Read or Write protected memory"的指针数组的指针数组
- C++HTTP客户端在GET请求后挂起read()调用
- 从带有 std::ifstream::read() 的文件中读取 int 遍历 char * 二进制数据
- Arduino Uno - serial.read to a struct of bits