The host computer obtains the single chip microcomputer data through the serial port

demand

When we usually use MCU to make projects, we always hope to display some data in real time and process these data for use. Then the upper computer is essential. The upper computer refers to the computer that can directly send control commands. Generally, various signal changes are displayed on the PC screen. The lower computer is a computer that directly controls the equipment and obtains the equipment status. It is generally a single chip microcomputer. The command sent by the upper computer is first given to the lower computer, and the lower computer then interprets the command into the corresponding timing signal to directly control the corresponding equipment. The lower computer reads the equipment status data (generally analog quantity) from time to time, converts it into digital signal and feeds it back to the upper computer. In short, the actual situation is very different, but everything changes: both upper and lower computers need programming and have special development systems.
Conceptually, the controller and the service provider are the upper computer, and the controlled and the served are the lower computer. It can also be understood as the relationship between the host and the slave, but the upper computer and the lower computer can be converted.
So I will make such a host computer today, which is mainly used to obtain the data of the development board, judge and display different prompts. It can also send relevant data to MCU.

prepare

  1. Single chip microcomputer with serial port (used for testing, no problem)
  2. Visual Studio 2019

functional design

  1. Set serial port parameters such as baud rate
  2. Open serial port
  3. Detection serial port
  4. send data
  5. receive data
  6. Adjust format
  7. Detect the total number of bytes of transmitted and received data
  8. Clear the receiving area and sending area
  9. Process the received data
  10. Set the waiting time of serial port data buffer

Interface design

Key procedures

Open serial port

 if (cbbComList.Items.Count <= 0)
            {
                MessageBox.Show("No serial port found,Please check the line!");
                return;
            }

            if (ComDevice.IsOpen == false)
            {
                ComDevice.PortName = cbbComList.SelectedItem.ToString();
                //ComDevice.BaudRate = Convert.ToInt32(cbbBaudRate.SelectedItem.ToString());
                ComDevice.BaudRate = Convert.ToInt32(cbbBaudRate.Text.ToString());
                ComDevice.Parity = (Parity)Convert.ToInt32(cbbParity.SelectedIndex.ToString());
                ComDevice.DataBits = Convert.ToInt32(cbbDataBits.SelectedItem.ToString());
                ComDevice.StopBits = (StopBits)Convert.ToInt32(cbbStopBits.SelectedItem.ToString());
                time = Convert.ToInt32(cbBox_time.Text.Trim());
                try
                {
                    ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);
                    ComDevice.Open();
                    btnSend.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                btnOpen.Text = "Close the serial port";
                pictureBox1.BackgroundImage = Properties.Resources.green;
            }
            else
            {
                try
                {
                    ComDevice.DataReceived -= new SerialDataReceivedEventHandler(Com_DataReceived);//Unbind COM receive
                    Thread.Sleep(1000);
                    ComDevice.Close();
                    btnSend.Enabled = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                btnOpen.Text = "Open serial port";
                pictureBox1.BackgroundImage = Properties.Resources.red;
            }
            cbbComList.Enabled = !ComDevice.IsOpen;
            cbbBaudRate.Enabled = !ComDevice.IsOpen;
            cbbParity.Enabled = !ComDevice.IsOpen;
            cbbDataBits.Enabled = !ComDevice.IsOpen;
            cbbStopBits.Enabled = !ComDevice.IsOpen;
            cbBox_time.Enabled = !ComDevice.IsOpen;

send data

if (ComDevice.IsOpen)
            {
                try
                {
                    ComDevice.Write(data, 0, data.Length);//send data
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Serial port not open", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return false;

receive data

 //Waiting for buffer data
            Thread.Sleep(time);

            byte[] ReDatas = new byte[ComDevice.BytesToRead];
            byte[] DIY = new byte[ComDevice.BytesToRead];
            DIY[0] =0x6F;
            ComDevice.Read(ReDatas, 0, ReDatas.Length);//Read data
            if (ReDatas[0] ==0x63 )
            {
                MessageBox.Show("correct");
            }
            else
                MessageBox.Show("error");
            //Discard receive buffer data
            ComDevice.DiscardInBuffer();

            this.AddData(ReDatas);//output data

Implementation page



Complete source code

address

follow-up

If you want to know more about the Internet of things and smart home projects, you can pay attention to my Programming column.
After subscribing to the column, you can chat privately on WeChat official account and send it to you directly.
Or pay attention to the official account.

It's not easy to write. Thank you for your support.

Keywords: C# Single-Chip Microcomputer

Added by Tensing on Sun, 02 Jan 2022 22:50:48 +0200