Using a c-program to read an NMEA-string

444 views Asked by At

I am trying to make a c-program that will will a string, but I want it only to read a very small part of it. The NMEA-telegram that I try to read is $WIXDR, and do receive the necessary strings. Here's 2 examples of strings that I get into the CPU:

$WIXDR,C,1.9,C,0,H,83.2,P,0,P,1023.9,H,0*46
$WIXDR,V,0.01,M,0,Z,10,s,0,R,0.8,M,0,V,0.0,M,1,Z,0,s,1,R,0.0,M,1,R,89.9,M,2,R,0.0,M,3*60

If it were only 1 string (not both C and V), this would not be a problem for me. The problem here is that it's 2 seperate strings. One with the temperature, and one with rain-info.

The only thing that I'm interested in is the value "1.9" from

$WIXDR,C,1.9,C,0......

Here's what I have so far:

void ProcessXDR(char* buffPtr)
{
    char valueBuff[10];
    int result, x;
    float OutSideTemp;
    USHORT uOutSideTemp;
//  char charTemperature, charRain
    IODBerr eCode;

    //Outside Temperature
    result = ReadAsciiVariable(buffPtr, &valueBuff[0], &buffPtr, sizeof(valueBuff));
    sscanf(&valueBuff[0],"%f",&OutSideTemp);
    OutSideTemp *= 10;
    uOutSideTemp = (USHORT)OutSideTemp;
    eCode = IODBWrite(ANALOG_IN,REG_COM_XDR,1,&uOutSideTemp,NULL);
    
}


            // XDR ...
            if(!strcmp(&nmeaHeader[0],"$WIXDR"))
            {
                if(PrintoutEnable)printf("XDR\n");
                ProcessXDR(buffPtr);
                Timer[TIMER_XDR] = 1200;          // Update every minute
                ComStateXDR = 1;
                eCode = IODBWrite(DISCRETE_IN,REG_COM_STATE_XDR,1,&ComStateXDR,NULL);
            }

There's more, but this is the main part that I have.

1

There are 1 answers

1
Pr0t0typ3 On

I have found the answer to my own question. The code that would do as I intented is as follows: What my little code does, is to look for the letter C, and if the C is found, it will take the value after it and put it into "OutSideTemp". The reason I had to look for C is that there is also a similar string received with the letter V (Rain). If someone have any input in a way it could be better, I don't mind, but this little piece here does what I need it to do.

Here's to example telegrams I receive (I wanted the value 3.0 to be put into "OutSideTemp"): $WIXDR,C,3.0,C,0,H,59.2,P,0,P,1026.9,H,04F $WIXDR,V,0.00,M,0,Z,0,s,0,R,0.0,M,0,V,0.0,M,1,Z,0,s,1,R,0.0,M,1,R,89.9,M,2,R,0.0,M,358

void ProcessXDR(char* buffPtr)
{
char valueBuff[10];
int result, x;
float OutSideTemp;
USHORT uOutSideTemp;
//  char charTemperature, charRain
IODBerr eCode;

//  Look for "C"
result = ReadAsciiVariable(buffPtr, &valueBuff[0], &buffPtr, sizeof(valueBuff));
//  sscanf(&valueBuff[0],"%f",&charTemperature);
if (valueBuff[0] == 'C')

//Outside Temperature
result = ReadAsciiVariable(buffPtr, &valueBuff[0], &buffPtr, sizeof(valueBuff));
sscanf(&valueBuff[0],"%f",&OutSideTemp);
OutSideTemp *= 10;
uOutSideTemp = (USHORT)OutSideTemp;
eCode = IODBWrite(ANALOG_IN,REG_COM_XDR,1,&uOutSideTemp,NULL);
}