0

my raspberry keep getting error after reading some serial input, like this:

enter image description here

i'm using Library wiringPi with this sample code:

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

#include <wiringSerial.h>

int main ()
{
  int fd ;

  if ((fd = serialOpen ("/dev/ttyS0", 9600)) < 0)
  {
    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
    return 1 ;
  }

// Loop, getting and printing characters

  for (;;)
  {
    putchar (serialGetchar (fd)) ;
    fflush (stdout) ;
  }
}

And I'm using Max Rs232 Module for connection from my device to raspberry pi. why this is happend? and what must I do with this error?

5
  • the top picture of -1-1-1-1 .... ... is that the input? ...... what error are you getting?
    – jsotola
    Oct 1, 2018 at 2:58
  • That's error I get at raspberrypi. after some period, my console suddenly getting -1-1-1-1 ... output. and this output never end. Oct 1, 2018 at 3:01
  • how do you know that it is an error? .... maybe that is the data.
    – jsotola
    Oct 1, 2018 at 3:11
  • Because, I already try to pull out the cable from Tx, Rx, and command line still didn't stop showing -1-1-1-1-1-1 ... forever. Oct 1, 2018 at 3:19
  • It's happend after some period. even I send data 1 time, after some period its happend and wont stop until I terminate the terminal. Oct 1, 2018 at 3:27

1 Answer 1

0

Revise your for(;;) loop to include if(serialDataAvail...) first. Also modify the loop to check the serilGetchar timeout. Your code is doing what it suppose to do, it is just too simple LOL!

int   serialDataAvail (int fd) ;

Returns the number of characters available for reading, or -1 for any error condition, in which case errno will be set appropriately.

int serialGetchar (int fd) ;

Returns the next character available on the serial device. This call will block for up to 10 seconds if no data is available (when it will return -1)

void serialFlush (int fd) ;
5
  • Oke thanks give me some time to try this. thanks for your advice, I will post my code ASAP. Oct 1, 2018 at 3:51
  • No hurry, I am about to go to bed - it is 23:00 here ! Best of luck.
    – Jan Hus
    Oct 1, 2018 at 4:00
  • Thanks bro, and Im sorry for bad question because im i'm kinda new at C language. I usually using Python, but I want explore and learn more about C/C++ language. thanks mate. Oct 1, 2018 at 4:02
  • Even I try to use serialDataAvail (fd), and serialFlush (fd), i still get error -1-1-1-1-1 sometime get square question mark. I dont Know what happend, maybe im going to re install my raspberry pi, and im going to update my question later. Oct 1, 2018 at 4:58
  • Well it is your call. You original code was OK, just "too simple". It should not made OS to misbehave. Good luck.
    – Jan Hus
    Oct 1, 2018 at 14:12

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.