0

I am trying to log slow serial data (about 40 bytes per minute at 9600 baud) on the Pi.

I have a rough program using wiringSerial which only provides unbuffered serialGetchar

I could write a serialGets but wonder if/why it hasn't been done before.

PS The last time I wrote something like this was in the 1980s on CP/M

2 Answers 2

1

Do you need to write a program for this or can you just use basic Unix commands? Could you provide us more details on what you want to achieve...

Assuming you serial port is connected through your USB port. You can easily log incoming bytes from serial port by redirecting (or simply listening) the right interface file, such as:

cat /dev/ttySx > /home/myloggin/data.log

In the same way, you can write on the serial port:

echo "command" > /dev/ttySx

To find out on which interface file your device is available, check /var/log/dmesg or issue a lsusb.

3
  • This does not answer the QUESTION which is about a buffered input - specifically to get input line by line. I have already indicated I can unbuffered input. Incidentally the answer has nothing to do with USB and works with any mapped port.
    – Milliways
    Commented Feb 18, 2014 at 11:02
  • Milliways, maybe you should edit your question to include the requirement for unbuffered data. I'm not sure how you'd know you'd got to the end of the string without a buffer, though. Under Linux/Unix, every device is a file, so the standard file I/O routines apply for serial.
    – scruss
    Commented Feb 18, 2014 at 12:48
  • @Milliways, your question and your requirement are not obvious. Could you please reformulate them in order to meet Stack Exchange Q/A standards and rise an answer that will better suits your needs.
    – jlandercy
    Commented Feb 18, 2014 at 14:21
0

I want the equivalent of fgets(), and was going to write this using serialGetchar but the timeout caused problems, requiring post processing.

I realised the required functionality was already in the kernel, it just required different initialisation.

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

#include <wiringPi.h>
#include <wiringSerial.h>

/*
 * serialOpenB:
 *  Open and initialise the serial port, 
 *  setting canonical mode, ignore CR on input, no timeout
 *********************************************************************************
 */
int serialOpenB (const char *device, const int baud, const int max)
{
    struct termios options ;
    int fd = serialOpen(device, baud);
    // Get and modify current options:
    tcgetattr (fd, &options) ;

    options.c_lflag |= ICANON;  // set canonical mode (line by line)
    options.c_iflag |= IGNCR;   // ignore CR on input
    options.c_cc [VMIN] = max-1;    // return if max-1 bytes received
    options.c_cc [VTIME] = 0;   // no timeout

    tcsetattr (fd, TCSANOW | TCSAFLUSH, &options) ;

    usleep (10000) ;    // 10mS

    return fd ;
}


/* Get a newline-terminated string of finite length.
 *********************************************************************************
 */

char * serialGets (char *buf, const int n, const int fd)
{
    int m;
    m = read (fd, buf, n);
    *(buf+m) = '\0';
    return (buf) ;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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