Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

serial communication error : empty buffer

Hello, 

 

I want to read GPS data from a GPS acquisition card and parse the data on screen. I use the rs232.h library but i don't know why every time I run the program, it says that the buffer is always empty. I also get the error "I/O operation timed out" as a result with the detection functions errors but after changing the timeout, nothing changes. I know the GPS card functions and can display data with the manufacturer software. Does anybody know what mistakes i did ?

 

Here is my code, I know it is not fully optimised, sorry about that : 

 

#include <stdio.h>
#include <stdlib.h>
#include <ansi_c.h>
#include <rs232.h>
#include <formatio.h>
#include <string.h>

#define BUFFER_SIZE 1024 
#define MAX_DEVICE_ID_LEN 200


int parseportRS232()
{
    int portNum = 48; // Com port COM48
    //int baudRate = 9600; // Baudrate
    int comStat;//pour les stats du port en lecture
    char buffer[BUFFER_SIZE]; // reading input data 
    int bufferRead; // in order to stock the return value of ComRd function
    int i = 0; //in order to count the number of attempts to read the buffer
    int inqlen; //in order to get the buffer's size in input
    int errorNumber;
    // open serial port
    printf("Trying to open serial port... \n");
    if (OpenCom(portNum, "COM48") != 0)
    {
        printf("Return RS232 Error : %d ", ReturnRS232Err());
        printf("Error while opening serial port\n");
    }
    //errors detection 
    printf("function OpenCom state: %d, %s \n", 
    ReturnRS232Err(),GetRS232ErrorString(ReturnRS232Err()) );
    printf("function GetComStat state: %d, %s \n", GetComStat(portNum), 
    GetRS232ErrorString(ReturnRS232Err())); 
    

    printf("Serial port opened successfully \n\n");
    /*SetComTime :
    For read operations, timeout occurs when the COM port input queue is empty 
    and no byte is read into the queue for the duration of the timeout time.
    For write operations, timeout occurs when the COM port output queue is full 
    and LabWindows/CVI cannot write to the queue for the duration of the timeout 
    time.
    SetComTime returns an error if you have not opened the port or if you pass 
    an invalid parameter value.*/
    SetComTime(portNum, 40); 
    printf("Trying to read input data...\n");
    //main loop
    while (i<=10)
    {
        printf("test reading a buffer, \n");
        bufferRead = ComRd(portNum, buffer, BUFFER_SIZE - 1);
        printf("function bufferRead state : %d, %s, \n", 
        ReturnRS232Err(),GetRS232ErrorString(ReturnRS232Err()) );
        GetComStat(portNum);
        printf("function GetComStat state : %d, %s, \n", 
        ReturnRS232Err(),GetRS232ErrorString(ReturnRS232Err()) );  
        if(GetComStat(portNum)>0) 
        {
           if (bufferRead > 0)
           {
              buffer[bufferRead] = '\0'; 
              printf("Donnees recues : %s\n", buffer);
           }
           else  {

             printf("error : %d \n", bufferRead );
           }
     }
     else 
     {
        inqlen = GetInQLen (portNum);
        //errors detection
        printf("function GetInQLen state : %d, %s \n", 
        ReturnRS232Err(),GetRS232ErrorString(ReturnRS232Err()) ); 
        printf("input buffer lenght %d, ", inqlen);
        printf("No available data, test %d \n\n", i);
        i++; 
    }
    //printf("Return RS232 Error : %d \n", ReturnRS232Err());

    }
    printf("End of serial port reading...\n");
    //Close serial port
    CloseCom(portNum);
    printf("Closing serial port...\n");
    return 0;
}
0 Kudos
Message 1 of 2
(604 Views)

Are you using a NI RS-232 card?

 

Are you able to use a program like Tera Term to send a query and receive something back?

0 Kudos
Message 2 of 2
(542 Views)