当前位置:网站首页>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 .
边栏推荐
- Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
- STC-B学习板蜂鸣器播放音乐2.0
- Fundamentals of digital circuits (II) logic algebra
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
- The common methods of servlet context, session and request objects and the scope of storing data in servlet.
- [oiclass] maximum formula
- 移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
- [pointer] counts the number of times one string appears in another string
- To brush the video, it's better to see if you have mastered these interview questions. Slowly accumulating a monthly income of more than 10000 is not a dream.
猜你喜欢
Wang Shuang's detailed notes on assembly language learning I: basic knowledge
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
内网渗透之内网信息收集(三)
Software testing interview summary - common interview questions
王爽汇编语言详细学习笔记二:寄存器
Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
Cadence physical library lef file syntax learning [continuous update]
随机推荐
[pointer] find the value of the largest element in the two-dimensional array
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
Function: find the root of the equation by Newton iterative method
Four methods of exchanging the values of a and B
To brush the video, it's better to see if you have mastered these interview questions. Slowly accumulating a monthly income of more than 10000 is not a dream.
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
Intranet information collection of Intranet penetration (3)
Pointer -- output all characters in the string in reverse order
指针 --按字符串相反次序输出其中的所有字符
Face and eye recognition based on OpenCV's own model
函数:字符串反序存放
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
Wang Shuang's detailed notes on assembly language learning I: basic knowledge
数字电路基础(三)编码器和译码器
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Fundamentals of digital circuits (II) logic algebra
The four connection methods of JDBC are directly coded
“人生若只如初见”——RISC-V