Sending strings to the Nextion display can be tricky. It seems like every language does things differently. I like a good challenge and am new to Python, so I figured this would be a fun video to make. It was.
Below are a few items I have used in my videos. They are affiliate links. I do make a small amount of profit if you use them. I use the money to purchase more items and equipment for my shop. Thanks to all that support me.
There is not a lot of code required to write to the display. This is true with every technology I have used to write to the Nexiton. So far, I have used all forms of Arduinos, ESP32, PLCs, and now Python. You must import a few libraries, configure the serial port, and send or receive the command. I have not used Python in any real-world project yet, but I am considering it to monitor the temperature in my freezer and a few water sensors in my basement. I don’t get any water in my basement and would like to keep it that way.
The first step is to add the libraries. The libraries are added using the import command.
import pyvisa as visa
import time
I use the Pyvisa library to access the serial ports. I am also using it to access a USB port in another project. There are other libraries to accomplish the same thing, but so far, this is the one that I prefer. The time library is a standard library that I am using to add delays. If you have followed my videos, you know that I hate synchronous delays. I am still learning Python, and as soon as I feel more comfortable, I will move toward asynchronous delays. I may not like the delay keyword in the Arduino, but I have used it enough to understand that it works in a pinch.
After you import the libraries, you can access the serial ports on your computer. In this example, “ports” will contain an array containing all available serial ports.
ports = visa.ResourceManager()
print(ports.list_resources())
I like printing the available ports to confirm that everything is configured correctly. You can comment out the line later and then use it if you need.
The next step is to choose one of the ports. There are ways to dynamically gather the ports and select the one you want. For this tutorial, I will hardcode the port. This reduces flexibility but gets the point across without adding additional code. I will go through an example with a Python interface if I get good feedback. In my building a tester series, I am implementing a Python GUI using Tkinter, but that can be overwhelming if you are just getting started.
serialPort = ports.open_resource('ASRL3::INSTR')
#serialPort.write_termination="\r"
#serialPort.read_termintation="\r"
serialPort.baud_rate=9600
#serialPort.data_bits=8
#serialPort.parity=visa.constants.Parity.none
#serialPort.flow_control=visa.constants.VI_ASRL_FLOW_NONE
The lines that are commented out above use defaults. You can open a port and not define any of the attributes. I am unsure what the default baud rate is, so I assigned a value. I left the rest as default and addressed any changes in the video. The default write termination is a line feed and a carriage return. I believe all the other settings I commented out are default settings.
This example is going to use an infinite loop. I will create a while statement to check if one is true and loop as long as that condition is true.
while(1):
if(serialPort.bytes_in_buffer):
The first step in the loop will be to see if any data is waiting in the serial port buffer.
while(1):
if(serialPort.bytes_in_buffer):
print(serialPort.read_bytes(serialPort.bytes_in_buffer))
If there is any data, the data will be output to the terminal. I have a Nextion display configured with a button. The button sends a string when pressed.
while(1):
if(serialPort.bytes_in_buffer):
print(serialPort.read_bytes(serialPort.bytes_in_buffer))
print("TOP")
serialPort.write("h0.val=23\xff\xff\xff")
time.sleep(2)
A delay is added to slow down the example. This makes it easier to see what is happening. I also add a print statement with the word “TOP.” If no data comes in the serial port, the print statement lets us know when the while loop makes another pass. On the Nextion portion of this example, I have a slider with the id of “h0.” I also added a line that writes “h0.val=23\xff\xff\xff” to the serial port. I want this string to alter the value of the slider. The statement has three parts. “h0.val” identifies the object. “=23” sets the value of the object to 23. “\xff\xff\xff” is the ending delimiter the Nextion uses to acknowledge the end of the command. I used this command first, hoping it would be as simple as a single line. I was wrong.
while(1):
if(serialPort.bytes_in_buffer):
print(serialPort.read_bytes(serialPort.bytes_in_buffer))
print("TOP")
#serialPort.write("h0.val=23\xff\xff\xff")
serialPort.write("h0.val=23")
serialPort.write_raw(b"\xff\xff\xff")
time.sleep(2)
I am leaving the old code lines in for reference. The write command does not recognize extended ASCII codes. I had to separate out the commands into a write and a write_raw. I also had to add the “b” before the hex string. This signifies a binary string. This did not produce any errors in Python, but it did not alter the slider on the Nextoin display.
while(1):
if(serialPort.bytes_in_buffer):
print(serialPort.read_bytes(serialPort.bytes_in_buffer))
print("TOP")
#serialPort.write("h0.val=23\xff\xff\xff")
#serialPort.write("h0.val=23")
serialPort.write_raw(b"h0.val=23")
serialPort.write_raw(b"\xff\xff\xff")
time.sleep(2)
When you use the write command, Python injects a carriage return and a line feed at the end of the statement. This is similar to println in the Arduino. You need to use write_raw if you want the text to go with no additional characters. You can define the ending delimiters when using write. The ending delimiters can be removed, making write and write_raw behave the same. It is nice to have the command write different from the command write_raw. In the Arduino world, I use print and println in different circumstances.
More in the Video
I go into much more in the video. I cover how to use software called “hterm.” this software allows you to look at the transmitted strings on the binary level. I feel it is better displayed in the video, so I did not add it to this blog post. The software can be downloaded by clicking HERE. I find it easy to use when troubleshooting the strings being sent to the Nextion.