-1

I have a sensor connected to an ODROID UART (Basically an RPi just a different name for the UART port) and using the WiringPi Serial library.

I am able to echo/cat to the sensor via two terminal windows and read it's data but using my C code I cannot. The sensor requires me to send it a "\r" to send back data.

cat /dev/ttySAC0
echo -e "\r" > /dev/ttySAC0

I'm wondering if my logic to write/read is wrong because the serialDataAvail (fd) is never true?

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>

int main ()
{
    int fd;

    // Setup serial port on ODROID
    if ((fd = serialOpen ("/dev/ttySAC0",9600) < 0)) {
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
    }
    if (wiringPiSetup () == -1) {
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
        return 1 ;
    }

    delay(1000);
    serialPrintf(fd,"\r"); // send enter key to read data from sensor
    delay(1000);    

    while (serialDataAvail (fd)) {
            printf ("%c", serialGetchar(fd));
    }

    serialClose(fd);    
}
2

Thanks for the responses. Looks like I messed up the serialOpen(). The brackets were done wrong. Amateur mistake.

Here's working code.

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>

int main ()
{
    int fd;

    // Setup serial port on ODROID
    if ((fd = serialOpen ("/dev/ttySAC0",9600)) < 0) {
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
        return 1 ;
    }
    if (wiringPiSetup () == -1) {
        fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
        return 1 ;
    }

    serialPrintf(fd,"\r"); // send enter key to read data from sensor
    delay(1000);

    while (serialDataAvail (fd)) {
            printf ("%c", serialGetchar(fd));
    }

    serialClose(fd);    
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.