[C# PC must-see] The hand training project you want is here

        Recently, more and more electrical friends have begun to learn C# to do host computer development. Many people have this feeling after studying for a period of time. It seems that they have learned a lot of knowledge, but they don’t know how to apply it, so I found A small real PC project, let everyone practice their hands. This article mainly gives an overall introduction to the project case and development process.

1. Project case introduction

        Many people have a special feeling for microcontrollers and always feel that microcontrollers are very mysterious or complicated. The single-chip microcomputer is abbreviated as MCU, or Micro Control Unit, which integrates CPU, memory (RAM and ROM), various IO interfaces, etc. on a chip to form a chip-level computer. It can be said that in our life and work, the single-chip microcomputer everywhere.

        The use of single-chip MCU is divided into two types, one is single-chip development, which is embedded development, generally using C language programming, and the other is single-chip applications, such as communication control with single-chip microcomputers. As a host computer developer, we are more In favor of the latter, the host computer controls the single-chip microcomputer in accordance with the established protocol. The users of the control system are operators, and they cannot directly operate the single-chip microcomputer. Therefore, the responsibility of the host computer is to connect the single-chip microcomputer and the operator, provide display and control to the operator through the UI interface, and then pass the instructions to the single-chip microcomputer for execution Action, and finally form a complete control system.

        This case is a serial communication case based on a single-chip microcomputer. It mainly realizes the speed control of the motor through the upper computer and displays the real-time speed of the motor. There are many speed control modes, including five modes: motor forward, motor reverse, motor forward and reverse, two-way chaos, and one-way chaos.

2. MCU protocol analysis

The description of the five speed control modes is as follows:

Speed ​​control mode Speed ​​regulation instructions
Motor forward Send a speed greater than 0 to the microcontroller
Motor reverse Send a speed less than 0 to the microcontroller
Motor forward and reverse Send a positive and negative speed value and speed change time to the MCU
One-way chaos Randomly or manually generate N random positive numbers, and send them to the MCU regularly
Two-way chaos Randomly or manually generate N random natural numbers, and send them to the MCU at regular intervals

From the above description, we can see that the essence of speed regulation is to send messages to the microcontroller. Different speed regulation modes affect the value and frequency of transmission. Therefore, an agreement must be agreed between the host computer and the microcontroller. The agreement is as follows:

Function command Protocol format Remarks
Motor forward S+speed value+; The speed value must be a positive number, such as S+40;
Motor forward S+speed value+; The speed value must be a negative number, such as S-40;
Motor forward and reverse T+time+S+speed value+; The time is the speed change time, such as T5+-40;
One-way chaos S+speed value+; The speed value is a positive number, generate a random number, and send it regularly
Two-way chaos S+speed value+; The speed value is a natural number, generates a random number, and sends it regularly
Speed ​​feedback V+speed value+; The returned speed value, such as V+40;
Speed ​​reset S+speed value+; The speed value is set to 0, that is, S+0; or S-0;

The protocol stipulates: The communication format between the single-chip computer and the host computer is: baud rate 9600, data bit 8, stop bit 1, check bit none, set speed range is 10-300rpm, feedback speed range is 0-1000rpm, conversion The time range is 1-10 seconds, and the communication encoding format is ASCII.

Function requirements for host computer development:

  • Connection prompt and disconnect prompt between MCU

  • Free choice of communication port

  • Debug mode can be adjusted at any time

  • Chaotic mode random number supports automatic generation and human input/copy and paste

  • Support quick setting of speed change time

  • Support real-time speed display and control

  • Support real-time data writing and control

  • Support sending commands can be stored and traceable

  • Keep the state when the software was opened last time

3. Host computer interface design

According to the development function requirements, the overall design interface is shown in the following figure:

When two-way chaos or one-way chaos is selected, the chaos value setting interface will pop up, which supports automatic generation and manual input, as shown in the following figure:

4. Function realization of upper computer

  • First create a Windows Forms application project, the project name is thinger.com.MCUPro.

  • Encapsulate the protocol of the single-chip microcomputer into a class MCULib, which is convenient for subsequent direct calls. This class should have the functions of establishing connection, disconnecting, setting speed, and receiving speed.

  • Log display function: Log display is displayed by ListView, and an ImageList is bound to distinguish information, alarms, and errors. The parameters of the log method include log level and log content.

  • Establish connection and disconnection: Here, two functions of establishing connection and disconnection are realized by a single button.

  • Set target speed: Set target speed is only valid for three modes: motor forward, motor reverse and motor forward and reverse.

  • Speed ​​clearing: When the speed is cleared, it is necessary to judge whether the current mode is in chaos mode. If it is in chaos mode, stop the timer first, otherwise, just send the command with speed 0 directly.

  • Chaos mode: When the speed control mode is switched to chaos mode, a timer will be activated. The timer period is consistent with the speed change time. The speed command is sent to the MCU regularly. The speed value comes from a set, which will pass a sub The window is obtained, so the difference between one-way chaos and two-way chaos lies in the difference of sets.

  • Chaos speed setting: The chaos value setting is implemented in a separate window, which can be manually added, randomly added or manually entered. The speed values ​​are separated by spaces. If you enter manually, you need to pay attention to the speed range.

  • Timing sending in chaos mode: If the timer is turned on in chaos mode, the data will be taken out one by one from the set and sent to the MCU, and the timer will be stopped after taking it out.

  • Data receiving processing: The data receiving processing method is also a commissioned prototype method. The read byte array is converted into a string for analysis. After the actual speed is obtained by the analysis, it is judged whether to display the speed. If the speed display is required, the data Add it to the Chart control, and then determine whether it needs to be written to a file. If it needs to be written to a file, write the real-time data to the CSV file.

  • Real-time data writing: The real-time data storage in this case uses CSV. When writing for the first time, the time and title bar will be automatically created, and data will be added continuously later. The software will automatically create a new file according to the current time every time it is opened or restarted to write a file, so it is necessary to write a general method for writing CSV.

  • When the software is opened, keep the state when it was closed last time: The idea of ​​this function is to realize through the configuration file. When the software is closed, the relevant information is stored in the form of configuration files. Reading from the file, because of the large number of parameters, the use of entity classes is also convenient for subsequent expansion.

5. Write at the end

        The overall difficulty of this project is not very large, but it is very representative, very suitable for the upper computer beginners to practice hands. The biggest problem for many beginners is that they don't have a practical project at hand. You can use this as a practical project to practice.

Pay attention to the Xinge Education Service account thinger_as, backstage reply [lottery draw], the prizes are generous, first come first served.

 

Review previous content

 

[Siemens] Build Siemens PLC environment based on PLCSIM-Advanced

[Siemens] Siemens S7 communication protocol, those things you don’t know

[OPCUA] C# host computer realizes OPCUA communication case

[OPCUA] OPCUA+MQTT+Alibaba Cloud, what can you do

[WinCC] C#/.NET combined with WinCC to realize data communication

[WinCC] Teach you how to develop Wincc voice alarm plug-in based on C# [source code attached]

[Cloud APP] Based on C# to realize the mobile phone APP to access Siemens PLC [with source code]

Guess you like

Origin blog.csdn.net/xiketangAndy/article/details/108147981