0

I am writing code for serial programming for serial communication between FTDI device and Desktop through XBEE.

I am writing command on device for (in example) reading Battery Voltage Current and Temp.

Command is stored in TxBuffer and I am storing response in RxBuffer. Before Storing I am checking how many bytes I have to read by using FT_Getqueuestatus which is coming 8 bytes but when I am reading using FT_Read and storing in RxBuffer its only reading 4 bytes. Plz Help me in this..

RxBuffer should be- 4642442300VIT

where V-Voltage

I-Current

T-Temp.

but It is reading only 46424423
Code

#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 Answer 1

0

Got Answer.

Sinse I was storing Response in String and sub command contains 00 which is nothing but null that is why it terminates. To overcome this problem I am reading from Buffer upto RxBytes which is nothing but, no. of bytes received from buffer.

Not the answer you're looking for? Browse other questions tagged .