FT_Read não está funcionando corretamente [fechado]

0

Estou escrevendo código para programação serial para comunicação serial entre o dispositivo FTDI e o Desktop através do XBEE.

Estou escrevendo o comando no dispositivo para (no exemplo) ler a corrente e a voltagem da bateria.

O comando é armazenado em TxBuffer e eu estou armazenando a resposta em RxBuffer . Antes de armazenar Estou verificando quantos bytes devo ler usando FT_Getqueuestatus que está chegando 8 bytes , mas quando estou lendo usando FT_Read e armazenando em RxBuffer a sua única leitura 4 bytes. Plz Ajude-me nisso ..

RxBuffer deve ser: 4642442300VIT

onde V-Voltage

I-Current

T-Temp.

mas está lendo apenas 46424423
Código

#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]="
#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]="%pre%",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] = '%pre%';

        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;

}
",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] = '%pre%'; 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; }
    
por keshaw 05.11.2014 / 10:08

1 resposta

0

Tenho resposta.

Sinse Eu estava armazenando Response em String e o sub-comando contém 00, que não é nada, mas é por isso que termina. Para superar este problema estou lendo de Buffer até RxBytes, que não é nada, mas não. de bytes recebidos do buffer.

    
por keshaw 06.11.2014 / 13:26