Page 1 of 1

Uart Raspberry Pi 3B+ Program

Posted: Sat Dec 15, 2018 9:22 am
by Xedom
Hello,
I have problem with Uart.
I had write simply program using wiringPi library.

Here is this code in C:

Code: Select all

#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

int main(void)
{
 struct termios options ;
int fd = serialOpen("/dev/ttyS0",9600);
wiringPiSetup ();
 if (wiringPiSetup () == -1)
  {
    fprintf (stdout, "Unable to start wiringPi: %s\n") ;
    return 1 ;
  }
unsigned char sendchar = 154;
serialPutchar(fd,sendchar);
if  (serialDataAvail(fd)){
int recived = serialGetchar(fd);
serialFlush(fd);
serialClose(fd);
printf("%3d \n",recived);
}
else
{
printf("None recived");
}
}
I enabled Serial ports in raspi-config and add core_freq=250 line to /boot/config.txt.
Also I set serial 0 to 9600 hz in \boot\comandline.txt .

When i run my program i have this:

Code: Select all

pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test                57
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
 80
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
 13
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
154
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
154
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
154
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
 97
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
154
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
154
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
154
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
154
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
154
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
154
pi@raspberrypi:~/Desktop/wiringPi/examples/xenitest $ sudo ./test
 97
Some of recived values are correct. Can anybody help me with comunication by Uart?

Re: Uart Raspberry Pi 3B+ Program

Posted: Thu Feb 07, 2019 2:59 am
by rzusman
Try flushing the input buffer before you transmit the character.