FT_Read 无法正常工作

FT_Read 无法正常工作

我正在编写通过 XBEE 进行 FTDI 设备和桌面之间串行通信的串行编程代码。

我正在设备上写入命令(例如)读取电池电压电流和温度。

命令存储在发送缓冲区我将回复存储在接收缓冲区。在存储之前,我会使用以下命令检查需要读取的字节数FT_Getqueuestatus即将来临8 个字节但是当我使用阅读时FT_读取并存储在接收缓冲区它唯一的读物4字节。 请帮助我。

RxBuffer 应该是- 4642442300VIT

其中 V-电压

电流

T-温度。

但它只读取 46424423
代码

#include <stdio.h>

#include <stdlib.h>

#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include "../../ftd2xx.h"


int main(int argc, char *argv[])
{
    FT_STATUS   ftStatus;
    FT_HANDLE   ftHandle;
    DWORD BytesWritten;
    DWORD EventDWord;
    DWORD TxBytes;
    DWORD RxBytes;
    DWORD BytesReceived;
    DWORD libraryVersion = 0;
    int iport,len,i;
char RxBuffer[256]="\0",outword[40];
char TxBuffer[]="\x4E\x45\x58\x23\x00\r\n"; // Contains data to write to device


ftStatus = FT_GetLibraryVersion(&libraryVersion);
if (ftStatus == FT_OK)
{
    printf("Library version = 0x%x\n", (unsigned int)libraryVersion);
}
else
{
    printf("Error reading library version.\n");
    return 1;
}

if(argc > 1) {
    sscanf(argv[1], "%d", &iport);
}
else {
    iport = 0;
}
printf("Opening port %d\n", iport);

ftStatus = FT_Open(iport, &ftHandle);
if(ftStatus != FT_OK) {
    /* 
        This can fail if the ftdi_sio driver is loaded
        use lsmod to check this and rmmod ftdi_sio to remove
        also rmmod usbserial
     */
    printf("FT_Open(%d) failed\n", iport);
    return 1;
}

printf("FT_Open succeeded.  Handle is %p\n", ftHandle);

ftStatus = FT_SetBaudRate(ftHandle, 57600); // Set baud rate to 115200
if (ftStatus == FT_OK) {
    printf("\nFT_SetBaudRate OK");
}
else {
    printf("\nFT_SetBaudRate Failed");
}
// Set 8 data bits, 1 stop bit and no parity
ftStatus = FT_SetDataCharacteristics(ftHandle, FT_BITS_8, FT_STOP_BITS_1,
FT_PARITY_NONE);
if (ftStatus == FT_OK) {
printf("\nFT_SetDataCharacteristics OK");
}
else {
printf("\nFT_SetDataCharacteristics Failed");
}
ftStatus = FT_Purge(ftHandle, FT_PURGE_RX | FT_PURGE_TX); // Purge both Rx and Tx buffers
if (ftStatus == FT_OK) {
printf("\nFT_Purge OK");
}
else {
printf("\nFT_Purge failed");
}
sleep(1);
ftStatus = FT_Write(ftHandle, TxBuffer, sizeof(TxBuffer), &BytesWritten);
if (ftStatus == FT_OK) {
printf("\nFT_Write OK");
}
else {
printf("\nFT_Write Failed");
}
sleep(1);

FT_SetTimeouts(ftHandle,5000,0);
//FT_GetStatus(ftHandle,&RxBytes,&TxBytes,&EventDWord);
FT_GetQueueStatus(ftHandle,&RxBytes);


if (RxBytes > 0) {

    ftStatus = FT_Read(ftHandle,RxBuffer,RxBytes,&BytesReceived);
    printf("\n\n\n%d\n\n\n",BytesReceived);
    if (ftStatus == FT_OK && RxBytes == BytesReceived) {
    len = strlen(RxBuffer);
    printf("\n\n\nlen=%d %d",RxBytes,len);
        if(RxBuffer[len-1]=='\n')
            RxBuffer[--len] = '\0';

        for(i = 0; i<len; i++){
            sprintf(outword+i*2, "%02X", RxBuffer[i]);
     }

 //printf("%s\n", outword);
    printf("\nFT_Read OK--Data-- %s",outword);
    }
    else {
    printf("\nFT_Read Failed");
    }
}
FT_Close(ftHandle);
return 0;

}

答案1

得到答案了。

因为我将 Response 存储在字符串中,而子命令包含 00,这只不过是 null,这就是它终止的原因。为了解决这个问题,我从缓冲区读取 RxBytes,这只不过是从缓冲区接收的字节数。

相关内容