I am new to the Raspberry subject and only figure out some basics. I want to use a Nextion-Display withe a Raspberry pi 3b+ and communicate over the UART pins. For this I found wiringPi and that it includes a simplified serial port handling library. The UART port is enabled in the config.txt and I made sure that the start sequence of the pi is not showing up over TX. I wrote this little program to write a number in a text box(t0) of the display.

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <wiringPi.h>   
#include <wiringSerial.h>

int main ()
  int fd ;

  if ((fd = serialOpen ("/dev/ttyS0", 9600)) < 0)
    return 1 ;

  printf("Serial open");

  if (wiringPiSetup () == -1)
    return 1 ;

  printf("Wiring Setup ok");



  return 0 ;

When I compile it with...

gcc -Os -o display display.c -lwiringPi -lwiringSerial

I get this error message:

/usr/bin/ld: cannot find -lwiringSerial

What do I miss or making wrong? Is there an easier way to transmit and receive data over UART?

Pages on which I have investigated:





AFAIK there is no wiringSerial library; everything is included in one of the standard libraries. I use the following in all my Makefiles

LDLIBS    = -lwiringPi -lwiringPiDev -lpthread -lm

PS It is recommended you use /dev/serial0

serialPuts(fd,0xff); makes no sense "0xff" is NOT a "nul-terminated string" but it is unclear what you are trying to send (or why you don't just send a single string).

  • OK, call me a nut! I thought everything that you #include that is external has to be linked. I switched to serial0 and only use -wiringPi and now he is building the file. Now I get a memory access error when I want to run it. – Mirco Schubert Apr 10 at 14:31
  • If you want help you need to post error messages (into your question) - the code listed is obviously incomplete – Milliways Apr 10 at 22:07
I am new Raspberry, ... 
I want to use a Nextion-Display communicate over the UART pins, ...

Well, for Rpi UART newbies I highly recommend using python. If you don't insist on C++, you might like to read some of my old posts on Rpi serial.

I check that Nextion instruction is simple:

1. All instructions over serial: are terminated with three bytes of 0xFF 0xFF 0xFF
2. All instructions and parameters are in ASCII
3. All instructions are in lowercase letters

I don't understand what you mean below.

I made sure that the start sequence of the pi is not showing up over TX.

Do you mean that you did not see what you expected to see over Tx? You might like to read my newbie tutorial to first make sure you serial port is working properly.

tlfong01's Rpi UART notes #1 - Serial to Arduino totally non-responsive

tlfong01's Rpi UART notes #2 - Data packet of hex values sent over serial

The test program included in my post is a complete python program. You just copy and paste and off you go, no library necessary :)


Nextion Basic HMI Display - Niextion
How to use Nextion with Rpi - Nextion
WireingPi Serial Library - WiringPi

Update 2019apr10hkt2028

I tried my UART delay loop back function with the following call.

serialPortLoopBack(serialPort0, b'wept 30,20\0xff\0xff\0xff\r\n', 32, 0.030)

So the string b'wept ..." is sent over Tx, and after a delay (you set it to Nextion's 5ms) Rx loops back (I shorted Tx to Rx) the same string.

I pasted the complete listing below. You can just copy, paste and run it.

In case you have problem reading my program, here is a penZu backup.


# uart_test16 tlfong01 2019apr10hkt2021 ***

# Computer = Rpi3B+
# Linux    = $ hostnamectl = raspberrypi Raspbian GNU/Linux 9 (stretch) Linux 4.14.34-v7+ arm 
# Python   = >>> sys.version = 3.5.3 Jan 19 2017

# Test 1   - repeatWriteBytes() - UART port repeatedly send out bytes.  
# Function - Repeat many times sending bytes, pause after each bytes.

# Test 2   - loopBackTest() - UART port send and receive bytes.
# Function - Send one bytes to TX, wait some time (Note 1), then read bytes back from RX. 
# Setup    - Connect Tx pin to Rx pin to form a loop.

# Test 3   - serialBlinkLed() - blink LED many times
# Function - Send b'(/0xff) many times, then '(/0x00) many times
# Setup    - Insert (1k + LED) between Tx and Ground

# Note 1
#   Bolutek BlueTooth BC04 needs at least 10mS to respond

from   time import sleep
import serial

serialPort0 = serial.Serial(port = '/dev/serial0',
        baudrate = 9600,
        parity = serial.PARITY_NONE,
        stopbits = serial.STOPBITS_ONE,
        bytesize = serial.EIGHTBITS,
        timeout= 1)

def setSerialPortBaudRate(serialPort, baudrate):
    serialPort.baudrate = baudrate

def serialPortWriteBytes(serialPort, writeBytes):

def serialPortReadBytes(serialPort, maxBytesLength):
    readBytes = serialPort.read(maxBytesLength)
    return readBytes

def serialPortWriteWaitReadBytes(serialPort, writeBytes, maxBytesLength, waitTime):
    readBytes = serialPortReadBytes(serialPort, maxBytesLength)
    print('        bytes written = ', writeBytes) 
    print('        bytes read    = ', readBytes)
    return readBytes

def repeatWriteBytes(serialPort, writeBytes, pauseTimeBetweenBytes, repeatCount):
    print('       Begin repeatWriteOneByte(), ...')   
    for i in range(repeatCount):
        serialPortWriteBytes(serialPort, writeBytes)                
    print('       End   repeatWriteBytes().')

def serialPortLoopBack(serialPort, writeBytes, maxBytesLength, waitTime): 
    print('        Begin serialPortLoopBack() [Remember to connect Tx to Rx!] , ...')
    serialPortWriteWaitReadBytes(serialPort, writeBytes, maxBytesLength, waitTime)     
    print('        End   serialPortLoopBack(), ...')

def serialBlinkLED(serialPort, repeatCount):
    print('       Begin serialBlinkLed(), ...')   
    for i in range(repeatCount):
        repeatWriteBytes(serialPort0, b'\xff', 0, 200)
        repeatWriteBytes(serialPort0, b'\x00', 0, 200)      
    print('       End   serialBlinkLed().')

# *** Main ***

setSerialPortBaudRate(serialPort0, 9600)
#serialBlinkLED(serialPort0, 100)
serialPortLoopBack(serialPort0, b'AT\r\n', 32, 0.030)
serialPortLoopBack(serialPort0, b'wept 30,20\0xff\0xff\0xff\r\n', 32, 0.030)

''' Sample output  tlfong01 2019apr10hkt2020
=== RESTART: /home/pi/Python_Programs/test1193/uart_test10_2019apr0901.py ===
        Begin serialPortLoopBack() [Remember to connect Tx to Rx!] , ...
        bytes written =  b'AT\r\n'
        bytes read    =  b'AT\r\n'
        End   serialPortLoopBack(), ...
        Begin serialPortLoopBack() [Remember to connect Tx to Rx!] , ...
        bytes written =  b'wept 30,20\x00xff\x00xff\x00xff\r\n'
        bytes read    =  b'wept 30,20\x00xff\x00xff\x00xff\r\n'
        End   serialPortLoopBack(), ...

# End

One thing I usually remind python UART newbies is to disable serial console, see picture below.

disable console

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.