当前位置:网站首页>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 .
边栏推荐
- If the position is absolute, touchablehighlight cannot be clicked - touchablehighlight not clickable if position absolute
- Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
- Fundamentals of digital circuits (I) number system and code system
- 四元数---基本概念(转载)
- About the garbled code problem of superstar script
- Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
- {1,2,3,2,5}查重问题
- 关于超星脚本出现乱码问题
- 《统计学》第八版贾俊平第四章总结及课后习题答案
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
猜你喜欢
“Hello IC World”
Want to learn how to get started and learn software testing? I'll give you a good chat today
Cadence physical library lef file syntax learning [continuous update]
C language do while loop classic Level 2 questions
150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
ucore lab2 物理内存管理 实验报告
Database monitoring SQL execution
[HCIA continuous update] advanced features of routing
Quaternion -- basic concepts (Reprint)
随机推荐
Want to learn how to get started and learn software testing? I'll give you a good chat today
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
Login the system in the background, connect the database with JDBC, and do small case exercises
关于超星脚本出现乱码问题
数字电路基础(二)逻辑代数
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
《统计学》第八版贾俊平第四章总结及课后习题答案
Global and Chinese markets of cobalt 2022-2028: Research Report on technology, participants, trends, market size and share
ES全文索引
Wang Shuang's detailed notes on assembly language learning I: basic knowledge
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
Detailed introduction to dynamic programming (with examples)
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
[pointer] solve the last person left
关于交换a和b的值的四种方法
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
Function: string storage in reverse order