当前位置:网站首页>RS485 signal measurement
RS485 signal measurement
2022-07-27 01:19:00 【lzl_ 0408】
One 、 The experiment purpose
1、 Skillfully use Linux Next io function read、write and epoll etc.
2、 skilled RS485 Signal characteristics of serial port
Two 、 Experimental process
1、 For their own SCM circuit board ( Later called A plate ) Download new hex file .
Then press K3 Key , The circuit board will pass RS485 Send out a set of serial number messages . The signal sequence format is :
0xAA 0x55 4 Byte sequence number
Please put it on the board RS485 Plug the DuPont cable into the interface , Then observe the content and baud rate of the sequence information with an oscilloscope and make a record .

2、 Use the classmate's STC Circuit board ( Later called B plate ), download B plate Hex file :
B The board will pass the computer USB Data sent by serial port , Forwarding to RS485 On the interface . At the same time RS485 Data received by the interface , adopt USB Forward the serial port to the computer . Let's use this next B Board with your own A Board for communication , Read A The password of the board .
B Each time the plate moves the rocker up and down , Its serial port and RS485 The baud rate on the bus will change , At the same time RS485 Output a 0x55. Please plug in the DuPont line , Use an oscilloscope to observe its baud rate .

Please dial up and down B Plate rocker adjustment B Board baud rate , To that of the A Board output RS485 The baud rate matches .


3、 The program used in calculation , call write The function interface sends a password reading command to the serial port . The format of the password reading command is :
0xAA 0x55 A Board serial number 12 Byte student number
Be careful , The baud rate used when sending should be the same as A The baud rate of board communication is consistent . Command to pass B The board is converted to RS485 The signal is sent to A plate .
A The board will respond to the password , The response format is :
0xAA 0x55 4 Byte password
The response password will go through B The board is forwarded to the computer . Please call read The function interface reads the password from the serial port .
#include <stdio.h>
#include "com.h"
#include "com.c"
int main(void)
{
unsigned char tmp[15] = {0}; // Buffer for storing read data
unsigned char w[18] =
{0xAA,0X55,0x20,0x22,0x4B,0xE5,0x02,0x00,0x02,0x00,0x02,0x06,0x00,0x01,0x00,0x05
,0x01,0x06};
int rl; // Length of read data ( Company : byte )
int i;
fd = openSerial("/dev/ttyUSB0"); // Open the serial port ,ttyUSB0 It is a serial port file
if(fd < 0)
{
printf("open com fail!\n");
return 0;
}
EpollInit(fd); // Initialize the terminal event trigger function epoll, Set the events to be monitored and related parameters, etc
write(fd,w,18);
while(1)
{bzero(tmp,sizeof(tmp)); // hold tmp The front of the corresponding memory block sizeof(tmp) Zero bytes
rl = ComRead(tmp,13);// Read 13 Bytes into the cache
// Print the read data
printf("read_len = %d\n", rl);
tmp[rl] = '\0';
for(i = 0; i < rl; i++)
printf(" z %02x", tmp[i]);
printf("\n\n");
}
close(epid);
close(fd);
return 0;
}4、 Please put A Board serial number 、 Student ID and password passed curl The command is sent to the course background system .
The syntax of the upload command is
curl "132.232.98.70:6363/check485?id= Student number &v= Serial number &s= password "
Respond OK Said right , Others indicate errors . for example , The serial number is 20 22 4B E5 , The student number is 202026010516, The password of the response
by 2B EC 20 4B, Then the report result order is :

RS485 Bus data transceiver
One 、 The experiment purpose
1、 Skillfully use Linux Next io function read、write and epoll etc.
2、 Proficient in processing streaming communication data
3、 understand 485 Bus conflicts
Two 、 Experimental process
1、 towards A SCM board download the following program
towards B SCM board download the following program
After downloading the program ,A Board microcontroller will use 1200 Baud rate is about per 300ms Send its own serial number once , The format is
0xAA 0x55 4 Byte sequence number
2、 adopt B Board serial port to RS485 The bus writes its own student number , The format is
0xAA 0x55 Twelve student ID numbers .
For example, student ID 202026010516, The following data should be sent through the serial port
AA 55 02 00 02 00 02 06 00 01 00 05 01 06
3、A After receiving the student number, the single chip microcomputer on the board will 300m Send the first string of passwords , The length of the password is 4 byte . Please resolve the string of passwords and put them in
150ms Send it back to the serial port as it is ( Otherwise, it will cause bus conflict ).STC The single product machine will continue to send the next string of passwords after receiving the return ,
Please continue to parse the string of passwords and then 150ms Send it back to the serial port as it is . In reciprocating , Record the last string of passwords received .
import serial
import requests
import serial.tools.list_ports
# Byte to string
def b2s(data):
return ''.join([f'{ch:0>2x}' for ch in data]).upper()
# Connecting device
ser = serial.Serial(list(serial.tools.list_ports.comports()[0])[0], 1200)
# Determine whether it is connected to the device
assert(ser != None)
# Enter the student id
studentID = input(" Student number :")
# Judge whether the student number is correct
assert(studentID.isdigit() and len(studentID) == 12)
# Read in the serial number
number = b2s(ser.read(6))
# Assert that the magic number is AA55
assert(number[:4] == "AA55")
# Take out the serial number
number = number[4:]
# Judge whether the serial number length is correct
assert(len(number) == 4 * 2)
print(f' Serial number :{number}')
# Use the student number to construct the byte data to be sent
startData = b'\xaa\x55' + bytes([ord(ch) - ord('0') for ch in studentID])
# Write data
ser.write(startData)
password = b'' # Read the password i = 1 # Record the number of cycles
# Open the eternal cycle , until Ctrl+C Pressed
try:
while True:
data = ser.read_all()
sdata = b2s(data)
if data != b'' and sdata[:4] == "AA55":
password = data[-4:]
if len(password) == 4:
print(f'[{i}] password :{b2s(password)}')
# Use the password read this time to construct data , Send it to the device to read the next password
ser.write(b'\xaa\x55' + password)
i += 1
# When Ctr+C Executed when pressed , It is recommended to press... When the read password is no longer changed , In about 256 After one cycle
except KeyboardInterrupt:
print(f'[ end ] The last string of passwords is :{b2s(password)}')
print(f'[ Submitting ]http://132.232.98.70:6363/checkSecret?id={studentID}&v=
{number}&s={b2s(password)}')
# Submit the experimental results to the server
r = requests.get(f"http://132.232.98.70:6363/checkSecret?id={studentID}&v=
{number}&s={b2s(password)}")
# Determine whether the submission is successful
assert(r.status_code == 200 and r.text.isdigit())
print(f'[ Submit successfully ] Student number :{studentID} Number of password cycles :{r.text}')
The serial number is 0822990F, The last string of passwords is 0023980F4、 Matrikl-Nr 、 Serial number 、 The last string of passwords is sent to the background of the course . The syntax of the upload command is
curl "132.232.98.70:6363/check485Secret?id= Student number &v= Serial number &s= password "
for example , The serial number is 0822990F, The student number is [202026010516], The last string of passwords is 0023980F, Then report the result order by :
curl "132.232.98.70:6363/check485Secret?id=202026010516&v=0822990F&s=0023980F"
The number returned in the background indicates the number of the password , return DUP It means that the serial number has been used by other students , Please replace the circuit board and get the password again .
RS485 The bus has two signal lines , It can transmit a logic signal . Computer standard UART Serial port has RX、TX Send and receive two lines , Therefore, data can be received and sent at the same time . and RS485 There is only one logic signal , Therefore, there can only be one subject at a time Data sending ( Therefore, it is called half duplex communication serial port ).
Experimental experience
Through these two experiments , We learned how to RS485 Signal measurement and RS485 Bus data transceiver . Understand that baud rate can be obtained through serial port , It can also be obtained by oscilloscope , As in the last experiment , Need to use Linux Next io function read、write function , We have increased our understanding of these functions
边栏推荐
- 25 common questions in Flink interview (no answer)
- When a transaction encounters a distributed lock
- 李宏毅机器学习(2021版)_P7-9:训练技巧
- ContextCompat.checkSelfPermission()方法
- SQL learning (1) - table related operations
- The shortest way to realize video applets: from bringing goods to brand marketing
- Naive Bayes Multiclass训练模型
- Li Hongyi machine learning (2017 Edition)_ P3-4: Regression
- FaceNet
- onSaveInstanceState和onRestoreInstanceState方法的调用
猜你喜欢

李宏毅机器学习(2017版)_P13:深度学习

SQL学习(3)——表的复杂查询与函数操作

New experience of mlvb cloud live broadcast: millisecond low latency live broadcast solution (with live broadcast performance comparison)

Keil开发环境的搭建送安装包

Create MDK project

SQL学习(2)——表的基础查询与排序

Redis -- cache avalanche, cache penetration, cache breakdown

IDEA导入外部项目时pom文件的依赖无效问题解决

The basic concept of how Tencent cloud mlvb technology can highlight the siege in mobile live broadcasting services

VSCode2015下编译darknet生成darknet.ext时error MSB3721:XXX已退出,返回代码为 1。
随机推荐
ADB shell screen capture command
Jenkins--基础--5.2--系统配置--系统配置
Li Hongyi machine learning (2017 Edition)_ P1-2: introduction to machine learning
Six ways for the Internet of things to improve our lives
pytorch张量数据基础操作
数据库期中(一)
顺序表之OJ题
SQL learning (2) -- basic query and sorting of tables
When a transaction encounters a distributed lock
More than live streaming: what other eye-catching functions does Tencent cloud live mlvb plug-in have besides streaming / streaming
3. 拳王阿里
SQL关系代数——除法
以赛促练-力扣第303场周赛反思
4. Root user login
As 5g becomes more and more popular, what positive effects will our lives be affected
Android——LitePal数据库框架的基本用法
Play guest cloud brush machine 5.9
Game project export AAB package upload Google tips more than 150m solution
Jenkins -- Basic -- 5.3 -- system configuration -- global security configuration
玩客云搭配zerotier保姆级教学,保证学废