当前位置:网站首页>Report on the double computer experiment of scoring system based on 485 bus
Report on the double computer experiment of scoring system based on 485 bus
2022-07-06 14:59:00 【Wuhu hanjinlun】
Originally, I was thinking about making long machines , But the teacher has already graded it . To compress to 6 A lot of content has been deleted from the page , Make do with it
be based on 485 Experimental report of scoring system of bus
The goal of the experiment :
Deepen the understanding through this case RS485 communication mode , The main controller of the upper computer communicates with all the lower computers .
B Level task
The experimental requirements :
Use two STC Download the provided in the previous section .hex file , build 485 Dual computer communication circuit , stay linux Program code for scoring in .
Experimental resources :Windows10 The computer ,Ubuntu18.04, Single chip microcomputer , DuPont line
The experimental steps :
Read the flow chart of the program system , Clarify the functional requirements of dual computer communication .
Be familiar with the simulation in the previous section MODBUS Packet structure of the protocol , Relevant function codes and additional data definitions
review 485 Bus data transceiver experiment , Build a dual computer communication circuit .
PC The end serial port settings are as follows : Serial port baud rate :9600 Data bits :8 position Check bit : nothing Stop bit :
5. Written PC The end program should complete a complete scoring process by referring to the communication protocol in the previous section :
You need to include the settings of the serial port
The master node initiates the slave detection process : Send the normal detection packet of the specified slave number , Judge whether the response query packet conforms to the communication protocol in the previous section .
The master obtains the scoring process of the slave : Send the data package related to the specified slave scoring , Judge whether the response query packet conforms to the communication protocol in the previous section .
The host initiates the process of ending the scoring , If reset succeeds ,STC From the plane 1 Position and number 8 position LED The light will go out .
Display serial port related information , Display the detected slave number and the score of the slave .
Code design ideas :
1. Define several functions to be used in this experiment :
string format(const vector<uchar> &data); // Data is processed into strings
int check(serial &pipe, int addr); // Equipment inspection
int get_score(serial &pipe, int addr); // Get points
void reset(serial &pipe); // Slave reset
2. Next is the main function , First, read the data from the MCU , The baud rate is set to 9600( The title has been given )
serial pipe("/dev/ttyUSB0", B9600);
3. Then select the desired function according to the requirements of the topic and user input . The first is to find the corresponding score according to the number of MCU , The second is to reset the slave , The third is to exit the program , Repeat the user's selection by setting a cycle . The specific code is as follows :
while(1){
cout<<"-------------------- be based on RS485 Bus scoring system -------------------------"<<endl;
cout<<" Enter description :\n1. Enter the legal slave address to query the score \n2. Input -2 Reset the slave \n3. Input -1 Exit procedure \n Please enter the command :";
cin>>addr;
if(addr == -1) break;
if(addr == -2) {
reset(pipe);
continue;
}
check_ret = check(pipe, addr);
if(check_ret == 1){
cout<<" Equipment detection is normal "<<endl;
sleep(1);
if(!(score = get_score(pipe, addr)))
cout<<" Failed to get score "<<endl;
else
cout<<" fraction :"<<score<<endl;
}
else if(check_ret == 0){
cout<<" Wrong address , Please enter the address and try again "<<endl;
continue;
}
else{
cout<<" Data transmission error , Please restart the slave "<<endl;
}
//reset(pipe);
cout<<" The slave has been reset , Input -1 sign out "<<endl;
}
You can see , Input -1 To exit the loop , then return 0
Exit procedure . Input -2 Called when the reset
Function to reset the slave . After inputting the correct slave number, the corresponding result will be output , Mainly by calling check
Function to check whether the slave number is correct , call get_score
To get grades , Next, we will analyze the implementation ideas of these three functions .
4.reset
Function complete code :
void reset(serial &pipe){
vector<uchar> code = {
0x5a, 0x00, 0x01,0x00,0x5b};
//cout <<"reset:"<< format(code) << endl;
/* Send packet */
for(int i=0;i<600;i++) {
pipe.myWrite(code);
}
return;
}
The parameter value is serial Serial data , According to the experimental guidance, we can know , When the master wants to send a reset signal to the slave , Its serial number should be 5a + Broadcast address 00 + Reset function code 01 + 00 + Check byte
, So the implementation idea is to define a string serial number , Then the host sends data packets to the slave constantly , Finally, the slave is reset .
5.check
Function complete code :
int check(serial &pipe,int addr){
int check_code = 117 + addr, ret; // The check code is cumulative sum
vector<uchar> code = {
0x5a, 0x08, 0x13};
vector<uchar> rec;
code.insert(code.begin()+1, (uchar)addr); // Insert slave address
code.push_back((uchar)check_code); // Insert the check code
//cout << format(code) << endl;
/* Write and receive response packets */
pipe.myWrite(code);
sleep(1);
rec = pipe.myRead(5);
if(format(rec) == format(code)) // Check whether the received packet is the same as the sent one , If it is the same, the slave address is correct
ret = 1;
else{
if(rec[3] == 0x6f) ret = 0;
else ret = -1;
}
return ret;
}
The parameters of the function are serial port data and slave address . The serial number sent by the host for data detection is 5a + Slave address + Detect function code 08 + 13 + Check code
. The implementation idea is to write the data detection serial number , Then judge whether the slave address is correct according to the received data packet . If you return 1 The address is correct , return 0 The address is wrong , return -1 Explain data error .
6.get_score
Function complete code :
int get_score(serial &pipe, int addr){
int check_code = 93 + addr, ret; // The check code is cumulative sum
vector<uchar> code = {
0x5a, 0x00, 0x03};
vector<uchar> rec;
code.insert(code.begin()+3, (uchar)addr); // Insert slave address
code.push_back((uchar)check_code); // Insert the check code
//cout << format(data) << endl;
/* Write and receive response packets */
pipe.myWrite(code);
sleep(1);
rec = pipe.myRead(5);
if(rec[3] == 0x6f) ret = -1; // Check for errors
else {
ret = (int)rec[3]; // Turn to numbers
if(ret < 0 || ret > 100) ret =-2; // Check whether the number is legal
}
return ret;
}
The parameters of the function are serial port data and slave address . The host sends for score acquisition, and the serial number is 5a + 00 + Read function code 03 + Slave address + Check code
. The implementation idea is the same as that of the previous function , Write the data detection serial number to , Then score according to the received packets , At the same time, we should judge the legitimacy of the score . If it is correct, the correct score is returned , return -1 It indicates that the slave is not ready , return -2 Description slave error .
7.format
The complete code of the function is as follows :
string format(const vector<uchar> &data) {
std::string str(2 * data.size() + 1, '\x00');
for (int i = 0; i < data.size(); i++) {
sprintf(&str[i * 2], "%02X", data[i]);
}
return str;
}
The main function of the function is to process data into strings , stay check
Use in a function
Verification and result analysis :
Here, the number of the lower computer is set to 2 Number , The score is 66 branch
open programs , Enter the slave number after operation 2 Attainable achievements , Input -2 Resettable slave , Input -1 Exit program
You can see , Results the correct , When inputting the correct slave address 2 when , The score is output 66. When you enter -2 when , The slave will reset , The effect is shown in the first figure , The first and eighth lights on the MCU go out . When the input -1 when , Exit the program directly .
Summary of the experiment :
- The humanities : Recognize the importance of communication . Many problems that I don't understand have been effectively solved by communicating with my classmates . At the same time, you can ask the teacher modestly when you encounter problems .
- knowledge : Master the knowledge based on 485 The working principle and specific implementation of the scoring system of bus . Learned how to obtain serial port data , And use serial port data to make some judgments . Also learned how to use c++ To realize based on 485 Dual computer scoring of bus scoring system .
- Skill : Can quickly write c++ Code , At the same time, the use of SCM and serial assistant software is more handy , Some keys of single chip microcomputer can be used simply .
边栏推荐
- 我的第一篇博客
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
- High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
- Function: calculates the number of uppercase letters in a string
- Using flask_ Whooshalchemyplus Jieba realizes global search of flask
- Function: find the maximum common divisor and the minimum common multiple of two positive numbers
- The four connection methods of JDBC are directly coded
- 【指针】八进制转换为十进制
- {1,2,3,2,5} duplicate checking problem
- Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
猜你喜欢
Wang Shuang's detailed learning notes of assembly language II: registers
数字电路基础(一)数制与码制
How to transform functional testing into automated testing?
Want to learn how to get started and learn software testing? I'll give you a good chat today
Query method of database multi table link
Es full text index
Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
Opencv recognition of face in image
5分钟掌握机器学习鸢尾花逻辑回归分类
Fundamentals of digital circuits (II) logic algebra
随机推荐
If the position is absolute, touchablehighlight cannot be clicked - touchablehighlight not clickable if position absolute
[issue 18] share a Netease go experience
函数:求方程的根
My first blog
Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
STC-B学习板蜂鸣器播放音乐2.0
Fundamentals of digital circuits (III) encoder and decoder
Don't you even look at such a detailed and comprehensive written software test question?
《统计学》第八版贾俊平第一章课后习题及答案总结
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Public key box
数字电路基础(二)逻辑代数
Pointeurs: maximum, minimum et moyenne
[pointer] the array is stored in reverse order and output
数字电路基础(一)数制与码制
Functions: Finding Roots of equations
[pointer] use the insertion sorting method to arrange n numbers from small to large
Face and eye recognition based on OpenCV's own model
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
Wang Shuang's detailed notes on assembly language learning I: basic knowledge