SmartDisplay RS485 Introduction

|29 november 2022

The concept of control methods for cross-platform
 

I. Preface:

Smart Display has been widely used in various fields such as medical, industrial control, and automotive after products launched. Simply put, it is a display combined with a microcontroller, which can handle complex graphics loading, setting, display, and even input, and the outside device (host i.e.) can perform interactive operations with simple commands, so it is very suitable for various needs. Various types of applications displayed or controlled.

Smart Display supports a variety of communication interfaces. Currently the most commonly used ones are CAN/CANopen, RS485/Modbus, and more choices will be provided in the future. Among them, RS485/Modbus is a popular communication interface in the industry, and various RS485 devices at reasonable prices are easily available on the market. The line structure is simple, as long as two wires (RS485_A/RS485_B) can communicate, and the communication interface is usually presented in the form of “Serial Port” on an operating system, and each platform has a corresponding development function library. In addition, the Modbus protocol is quite easy to understand, so we put it as an example as following for experiments and explanations.

This article will introduce how to control Smart Display from simple to deep, understand its control principle, and finally introduce how to make cross-platform applications. Whether you are an industrial equipment expert or a DIY player, welcome to the world of Smart Display with us!

II. Requirements:
 

Smart Display_RS485

A Smart Display that supports RS485 interface is required for connecting test.

RS485 Dongle

Since a host like PC/Raspberry Pi normally does not have RS485 port, we can utilize various USB to RS485 dongle if necessary.

Figure 2-1 Types of USB to RS485 Dongle

Figure 2-1 Types of USB to RS485 Dongle

 

HOST Device

When reach the above requirements, then can choose a test bench execution environment as a Host. The only condition for a host device is to be able to read and write the series port of host no matter the OS is Windows, MacOS, various Linux (including Raspbian for Raspberry Pi), or the platform is Single Board Computer, Arduino etc.

We create a single-board computers have built-in RS485 of Demo.set for convenience, which can be used as a host device as below picture directly.

Model No. WUACLB120V0000N0NX01

Model No. WUACLB120V0000N0NX01

 

III. Simple test for control interface:
 

Get the control code

In order to understand how to collaborate in Smart with a host. First issue we need to get the control code. The easiest way is to get it from Smart Display GUI Builder. GUI Builder is an excellent support software for Smart Display. It can help us learn, operate, design, and display user interface without hardware physical device, design UI without writing any program at all. With the WYSIWYG feature, user interface design can be done by dragging or clicking buttons or objects. In addition, there is a built-in simulator that shares source code with Smart Display, thus ensuring the same appearance and behavior as the physical device. Even better, it’s free!

Contact for Smart Display GUI Builder

Figure 3-1 Smart Display GUI Builder

Figure 3-1 Smart Display GUI Builder

 

Double click GUI Builder to run the app in Windows OS, open a project. Here we choose the 5″ RS485 Smart Display WL0F00050000FGDAASA00, and the default scenario below is Vehicle. Input the project name, press Create button to create project. If it has been created before, you can also select project directly from the menu on the right.

Figure3-2 Create project

Figure3-2 Create project

 

When the project is opened, select the interface first

Figure 3-3 Select interface

Figure 3-3 Select interface

 

Figure 3-4 Set interface parameters

Figure 3-4 Set interface parameters

 

Select RS485 for the Interface, and the RS485 Port of host should be set as the actual port of the connected port for RS485 dongle. If there is any doubt, you can check the port name in the device administrator (repeatedly unplug it and see that it disappears or shows which one is displayed). Baudrate can keep the default value of 115200.

Figure 3-5 Com Port display of device administrator

Figure 3-5 Com Port display of device administrator

 

However, if there is no Smart Display RS485 device yet, it does not matter, as long as the Interface selects RS485, the RS485 Port can be ignored for now, because now we will just utilize the simulator, and will not connect to the Smart display device practically.

Click Simulator/Simulator with GUI to run:

Figure 3-6 To run Simulator

Figure 3-6 To run Simulator

 

After execution, you can see the simulation screen, the control items of each object; and the communication data record on the right side of the screen. Change the value of any object to see the content of the command accordingly:

Figure 3-7 Change the content of the object and the control code

Figure 3-7 Change the content of the object and the control code

 

Every action will see two commands TX/RX, because the display device (even if it is actually an emulator now) will send back the same command as acknowledge after receiving the command to indicate that it has been received.

We record several different values for different objects, and the next step can be used to control the physical device.

Table 3-1 Control code

Table 3-1 Control code

Wire connection:

RS485 uses the differential pair to transmit & receive signals, so as long as these two wires can be connected, even the ground wire is not needed. The name of the signal line of RS485 is A/B, just follow it (A connect A, B connect B).

Figure 3-8 RS485 wire connection

Figure 3-8 RS485 wire connection

 

How is a host to transmit control codes from itself? As long as it can write to a serial port. However, there are two things to keep in mind before go ahead for testing.

1. Since the names of the Com Ports on different operating systems are different, please check and confirm before testing. For examples the following table:

Table 3-2 Serial port names for various operating systems

Table 3-2 Serial port names for various operating systems

 

2. The permission of the Com Port of some operating systems is not open by default. It must be set before using it. Taking Linux/Ubuntu as an example, if the name of the Com Port is ttyUSB0, just execute it in the terminal:

sudo chmod 666 /dev/ttyUSB0
 

Below two test methods are provided:

Serial Port Terminal Control

Use the serial port terminal software that supports hexadecimal input, and directly input the hexadecimal control code after connecting:

Figure 3-9 Send Control Codes with Terminal Program

Figure 3-9 Send Control Codes with Terminal Program

 

If it is normal, you should be able to see that the objects on the device act according to different control codes.

But this method is only used to test whether the connection is successful, and it is not very convenient in practical application. To achieve the best results, it has to be done through the program. As for which language? In fact, we can, let’s try it first with the popular cross-platform programming language python.

Use python

The following programs are executed using python3. The program structure is quite simple, just directly write three sets of control codes into the serial port every one second, so that the instrument on the device continues to rotate.
 

import serial
from time import sleep

“””
The name of Com Port varies according to the operating system, For example:
Raspberry Pi: /dev/ttyUSBn (e.g. /dev/ttyUSB0)
Windows: COMn (e.g. COM22)
“””
# Raspberry Pi / Rasbian
# COM_PORT = ‘/dev/ttyUSB0’
COM_PORT = ‘COM22’

v000 = bytes.fromhex(‘7B06000600006251’)
v050 = bytes.fromhex(‘7B0600060032E384’)
v100 = bytes.fromhex(‘7B060006006463BA’)

ser = serial.Serial(COM_PORT, 115200)
while True:
ser.write(v000)
sleep(1.0)
ser.write(v050)
sleep(1.0)
ser.write(v100)
sleep(1.0)
ser.write(v050)
sleep(1.0)

 

The python source code can be executed on a variety of platforms, so as long as the serial port is named correctly, it should work fine with the appropriate connection test bench.

However, these fixed control codes as example must not be in line with practical applications, so we need to understand the content of these instructions in order to give full play to them.

IV. control code analysis

Comparative analysis

Compare commands with different values for the same object:

Table

It is not difficult to find that the blue part is the value of the object we set.

Compare different objects, the same value command:

Table

The blue part obviously refers to some kind of index or address to the object.

As for the data of the last two bytes, it seems that there are no rules. What are those messages meaning though? Let’s explain in following.

Control code analysis

In fact, the content of these 8 bytes is the so-called Modbus message. Simply use a picture to illustrate the meaning of each field:

Figure 4-1 Control code example description

Figure 4-1 Control code example description

 

ID
Each Modbus device must have an identification code. Smart Display’s identification code is 0x7B = 123

Fun(Function Code)
Function code, indicating the purpose of this message, for example, 06 here is Write Single Register, which also writes a value to the specified place.

Register Address
The register address, which is the destination to be written to (that is, the address of the object).

Data
What to write (that is, the value of the object)

CRC
The result of CRC (Cyclic Redundancy Check) on the first 6 bytes is used to check whether there is an error in the data during the transmission process.

Do you have some concepts? If you want to know more, you have to look at what Modbus is.

V. Modbus Introduction

 

Modbus Protocol

The Modbus protocol is actually a data format. It basically defines the communication content of a master-slave architecture. Since it is only the definition of the data structure, it can communicate through various physical interface, such as RS232, RS422, RS485, and even the network.

Since there are already many teaching and explanatory documents on the Internet, Modbus will not be described in detail here. In fact, as long as you know some concepts, you can use Modbus freely.

Conceptual model

Modbus regards data transmission as “Register” access. Each device must define its own register type and address for external reference. The so-called transmission of data to the device is to write to the specified register, and to read the data is to read the specified register, which is simple and clear. In addition, the value stored in each address register is 16-bits.

Function code

According to the characteristics of the data, Modbus defines several methods of reading and writing, which are specified by the function code in the message.

Table 5-1 Modbus Function Codes

Table 5-1 Modbus Function Codes

 

For Smart Display control programs, the most commonly used are 06: Write single register (write a 16-bits value) and Read holding registers (read the value of multiple registers).

CRC

The last 2 bytes seems the most mysterious part is actually not difficult. It’s just a CRC (Cyclic Redundancy Check) to insure the communication data. We don’t necessarily have to study its principle in depth (but in fact, it is just a table lookup and bit operation, and finally a 16-bit check code is obtained), as long as we understand how to use it.

VI. Control Smart Display objects

As mentioned earlier, each Modbus needs to define the type and address of the register, and now we will discuss this topic.

Register classification

Smart Display registers can be roughly divided into three categories:

Device information (e.g. version, device name, etc.)

Use 04: Read Input Registers to read. These data will only be changed when the firmware is updated, usually when it is just connected, to obtain the device characteristic parameters.

Object properties (eg kind, location, etc.)

Use 03: Read Holding Registers/16: Write Multiple Register to read and write. These data affect the appearance of objects and usually do not change outside the design stage. To change the content, the Smart Display must be temporarily turned off before it can be updated.

Object Values (values that the object represents, such as RPM, on or off, percentage, etc., varies from object to object)

This is the main change item in the operation. Each element uses a 16-bit value, so it is set with 06: Write Single Register.

The following is an organized list of these registers:

Input Registers

Table 6-1 Smart Display Input Registers

Table 6-1 Smart Display Input Registers

 

Holding Registers

Table 6-2 Smart Display Holding Registers

Table 6-2 Smart Display Holding Registers

 

Early Smart Display supports only 10 objects, so the widgetID range is 0 ~ 9 (inclusive). The new version of the firmware has been expanded to 64, but the addressing method has been changed, which will not be discussed here.

With these data (although not complete, but enough for now), we can package the control instructions and send them to Smart Display for control. As for how to pack and how to add CRC code, it will be explained in the following chapters.

VII. User Interface

In order to put Smart Display into practical application, a suitable human-machine interface (especially Graphics User Interface, GUI) is still needed to maximize its effectiveness. At present, there are many choices for the cross-platform GUI development platform, and we use Qt to implement it here.

Many people think that Qt is just a GUI library, but in fact it is a fairly complete development framework. If the application is implemented entirely through its class library, the project can be easily ported to other platforms (PC/Windows, Mac/MacOS, Raspberry Pi/Raspbian, Android, iOS, Embedded, etc.) with little modification. The program is a development tool software that is worth investing in learning.

The newer version of Qt actually already supports the Modbus class library. But here for illustration, and to facilitate everyone to rewrite into other languages, we still implement this test program from the most basic serial port reading and writing.

In addition to providing he function of selecting the serial port and transmitting the control code used in the previous example, this example further provides a horizontal lever in users interface of app window, which can set the value of the specified object. If you choose COM for the serial port, remember to fill in the actual number.

The following picture shows the execution screen of this program on Raspberry Pi and Ubuntu. The program does not need to be modified at all.

Figure 7-1 Set WidgetValue screen on Raspberry Pi

Figure 7-1 Set WidgetValue screen on Raspberry Pi

 

Figure 7-2 Set WidgetValuet screen on Ubuntu

Figure 7-2 Set WidgetValuet screen on Ubuntu

 

CRC operation

The CRC operation is actually not complicated. It is probably like below codes with Qt:

QByteArray SetWidgetValue::calculateCRC(QByteArray data)
{
uint16_t crc = 0xFFFF;
for(int idx = 0; idx < data.length(); idx++)
{
uint8_t i = (uint8_t)crc ^ data[idx];
crc >>= 8;
crc ^= crcTable[i];
}
QByteArray ba;
ba.append(char(crc & 0xFF));
ba.append(char(crc >> 8));
return ba;
}

The contents of crcTable[] are:

const uint16_t crcTable[] =
{
0X0000, 0XC0C1, 0XC181, 0X0140, 0XC301, 0X03C0, 0X0280, 0XC241,
0XC601, 0X06C0, 0X0780, 0XC741, 0X0500, 0XC5C1, 0XC481, 0X0440,
0XCC01, 0X0CC0, 0X0D80, 0XCD41, 0X0F00, 0XCFC1, 0XCE81, 0X0E40,
0X0A00, 0XCAC1, 0XCB81, 0X0B40, 0XC901, 0X09C0, 0X0880, 0XC841,
. . .
0X8801, 0X48C0, 0X4980, 0X8941, 0X4B00, 0X8BC1, 0X8A81, 0X4A40,
0X4E00, 0X8EC1, 0X8F81, 0X4F40, 0X8D01, 0X4DC0, 0X4C80, 0X8C41,
0X4400, 0X84C1, 0X8581, 0X4540, 0X8701, 0X47C0, 0X4680, 0X8641,
0X8201, 0X42C0, 0X4380, 0X8341, 0X4100, 0X81C1, 0X8081, 0X4040
};

Not every codes are listed here. There is complete information in github that users can do download by themselves if interesting on programming, in this article we just know how to operate with the data format.

Data packaging

The following is the key function that calculates the control code according to the UI and sends it to Smart Display:

void SetWidgetValue::on_sbValue_valueChanged(int value)
{
int id = ui->edWidgetID->text().toInt();
sendWidgetValue(id, value);
ui->lbValue->setText(QString::number(value));
}

void SetWidgetValue::sendWidgetValue(int id, int value)
{
QByteArray msg;
uint16_t addr = (uint16_t)(id * 100 + 6);
msg.append(deviceID);
msg.append(writeSingleRegister);
msg.append((uint8_t)addr >> 8);
msg.append((uint8_t)addr & 0xFF);
msg.append((uint8_t)value >> 8);
msg.append((uint8_t)value & 0xFF);
QByteArray msgToSend = msg.append(calculateCRC(msg));
writeSerialPortData(msgToSend);
}

If you are already familiar with Qt, the above code should not be difficult to understand. If you are not familiar with it, take a moment to study it, some of its concepts are actually quite good and worth studying in depth. After getting familiar with it, it is a good choice for developing cross-platform applications or quickly prototyping a system.

VIII. Advanced example

The previous example is only for a setting of the object value. There is a more complete example SmartDisplayConsole application program for interested readers to learn more about. The following picture is the execution screen on the Raspberry Pi:

Figure 8-1 SmartDisplayConsole screen on Raspberry Pi

Figure 8-1 SmartDisplayConsole screen on Raspberry Pi

 

There is a github URL at the end of the article, and everyone is welcome to refer to and study together.

IX. Conclusion

We have explained every step of controlling the Smart Display, and also provide a variety of control methods. Using this knowledge and rich imagination, I believe that various sparks can be stirred up and more diverse products can be produced. Let’s create a better future with Smart Display together!

X. References

Serial Terminal Basics
https://learn.sparkfun.com/tutorials/terminal-basics/command-line-windows-mac-linux

Modbus
https://www.modbustools.com/modbus.html
https://www.modbusdriver.com/doc/mbusmaster.net/modbus.htm

XI. Sample source code

GettingStartedWithSmartDisplay
https://github.com/Smart-Display-Series/GettingStartedWithSmartDisplay.git

SmartDisplayConsole
https://github.com/Smart-Display-Series/SmartDisplayConsole.git

If you want more information or if you have any questions, please contact Nijkerk Electronics!

Bekijk alle artikelen