Use Proteus to quickly implement a simple host computer

Many finished modules often need to read data through the serial port when in use, so in order to quickly read the module data, I use Proteus software to simulate an arduino, and then program to achieve data interaction.

Create a Proteus project

Create 2560 projectInsert picture description here
First, create a new arduino Mega 2560 project, and draw the schematic diagram according to your needs. The part of the development board on the left in the above figure is the existence after the project is successfully created. The first component on the right is the VIRTUAL TERMINAL, which is equivalent to the serial port assistant. The second component is the serial port connection component, and the third component is the DS1302 clock. It is to display the time, this component is not necessary. Where to find the first two can see the picture below.
Insert picture description here
Insert picture description here
The specific usage method of these two components will not be repeated (note that the circuit connection method of COMPIM is different from the usual serial communication connection method). Double-click to enter the property interface when using it, and the parameters are clear at a glance.

The next step is to write the program in the Source Code interface.
Insert picture description here

Programming

If there is no Source Code interface when opening the project, you can click the red arrow in the figure above to open it.
Then write a few necessary functions, the specific code is as follows.

void setup()
 {
    
     
    Serial.begin(9600);  //与COMPIM元器件连接
    Serial1.begin(9600); //与VIRTUAL TERMINAL元器件连接
    delay(200);
	Serial1.println("Start!!!");
 }
 
void loop()
 {
    
     
	Send_Data(0x0055, 0x000C);//发送函数
	delay(100);
	Receive_Data();//接收函数
	delay(2000);
 }

void Send_Data(uint16_t function0, uint16_t adrr0)//根据模块协议自行编写
{
    
    
  data[0] = 0x01;
  data[1] = 0x03;
  data[2] = function0>>8;
  data[3] = (uint8_t)function0;
  data[4] = adrr0>>8;
  data[5] = (uint8_t)adrr0;
  uint16_t crc_data = crcx::crc16(data, len-2);//计算数据CRC数据
  data[6] = (uint8_t)crc_data;
  data[7] = crc_data>>8; //计算CRC校验
  Serial.write(data, len);//发送消息
}

void Receive_Data()
{
    
    
  if(Serial.available()&&(rx_flag==0)) //判断串口缓存区 
   {
    
    
     Serial.readBytes(serialBuffer, bufferLength); //读取一帧数据
     uint16_t crc_data = (uint16_t)serialBuffer[28]<<8 | serialBuffer[27];//读取消息中CRC数据
     uint16_t crc_cal = crcx::crc16(serialBuffer, bufferLength-2);//计算数据CRC校验
     if(crc_data == crc_cal) 
     {
    
     
       rx_flag=1;
     }  
	else  Serial1.println("rec err!!");
   }
   if(rx_flag==1)
   {
    
    
     Data_Deal();//调用数据处理函数,根据模块协议自行编写即可
     Serial1.println("rec ok!!");
     rx_flag=0;
  }
}

The CRCx library is used in the above code, which can be searched and added in the Arduino IDE.
After writing the program according to the module, you can install the virtual serial port software yourself, and then use the serial port assistant to verify whether the data is correct.
In this process, one more thing can be said. You can use the following code to convert a four-byte hexadecimal number into single-precision floating-point data.

uint32_t  x=0;
float *Rec_data = (float *)(&x);
Data=*Rec_data;

After the schematic diagram and the program are set, the simulation results are as follows (if there is no Virtual Terminal window, you can find it in the Debug menu in the menu bar):
Insert picture description here
In short, the implementation method is very simple. When you don't have a development board in your hand, and you don't know how to write the host computer in C#, it is quite useful as a rapid development.

Guess you like

Origin blog.csdn.net/weixin_44625313/article/details/112697856