打印
[技术问答]

单片机串口通讯收发数据C#上位机界面显示出来

[复制链接]
248|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
robincotton|  楼主 | 2023-8-28 09:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
今天的任务是,把测试得到的数据在上位机的界面上显示出来,于是键盘手花了两天的时间模仿着巨人的肩膀通过了用C#编写的界面程序,界面很简单就像下面显示的一样。

下面就一步一步给大伙展示一下我的程序吧。
C#非常的强大而且友好,串口的通信可以通过编程环境(这里我用的是Visual Studio 2010),如果有需要的话可以给我信息,我有完整版的安装包。如下图,简单的调用和选择就完成了串口的基本配置。

下面主要就是编程的问题了,在窗体Load的进程中可以完成串口的启动

而后就是读取数据的操作,这时候用到事件
1 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
在这个事件里面编程就可以了,但是在对窗体内文本进行操作的时候会发现出现了线程的冲突和错误。网上给出的一种解决方法是采用代理的方式具体的程序如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Net.Http;
using System.Collections;

namespace SerialPortDataUploader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBoxSerialPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            serialPort1.PortName = comboBoxSerialPort.Items[comboBoxSerialPort.SelectedIndex].ToString();
        }

        private void comboBoxSerialPort_MouseClick(object sender, MouseEventArgs e)
        {
            comboBoxSerialPort.Items.Clear();
            comboBoxSerialPort.Items.AddRange(SerialPort.GetPortNames());
        }

        private void listBoxLog_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxLog.SelectedItem != null)
                textBoxLog.Text = listBoxLog.SelectedItem.ToString();
        }

        Queue<char> DataQueue = new Queue<char>();
        volatile private int lightZ = 50, tempZ = 28, wetZ = 30;
        volatile private int lightX = 50, tempX = 28, wetX = 30;
        volatile private int lightL = 50, tempL = 28, wetL = 30;

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            char[] tmp = new char[1000];
            int count = serialPort1.BytesToRead;
            serialPort1.Read(tmp, 0, count);
            for (int i = 0; i < count; i++)
                DataQueue.Enqueue(tmp[i]);

            while (DataQueue.Count >= 11)
            {
                switch (DataQueue.Peek())
                {
                    case 'z':
                    case 'l':
                    case 'x':
                        {
                            char[] tmp1 = new char[100];
                            for (int i = 0; i < 11; i++)
                                tmp1[i] = DataQueue.Dequeue();
                            try
                            {
                                int light = (tmp1[2] - 0x30) * 10 + (tmp1[3] - 0x30);
                                int temp = (tmp1[5] - 0x30) * 10 + (tmp1[6] - 0x30);
                                int wet = (tmp1[8] - 0x30) * 10 + (tmp1[9] - 0x30);
                                switch (tmp1[0])
                                {
                                    case 'z': lightZ = light; tempZ = temp; wetZ = wet; break;
                                    case 'x': lightX = light; tempX = temp; wetX = wet; break;
                                    case 'l': lightL = light; tempL = temp; wetL = wet; break;
                                }
                            }
                            catch (Exception) { }
                            break;
                        }
                    default:
                        DataQueue.Dequeue();
                        break;
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxSerialPort.Items.Clear();
            comboBoxSerialPort.Items.AddRange(SerialPort.GetPortNames());
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            buttonConnect.Enabled = false;
            comboBoxSerialPort.Enabled = false;
            labelStatu.ForeColor = Color.Green;
            timer1.Start();
        }
        private string HTTPGet(string url)
        {
            using (var client = new HttpClient())
            {
                var responseString = client.GetStringAsync(url);
                return responseString.Result;
            }
        }
        private async Task<string> HTTPPost(string url, Dictionary<string, string> values)
        {
            using (var client = new HttpClient())
            {
                var content = new FormUrlEncodedContent(values);

                var response = await client.PostAsync(url, content);

                var responseString = await response.Content.ReadAsStringAsync();
                return responseString.ToString();
            }
        }

        delegate void listBoxLogAddMessage_Callback(string mwssage);
        private void listBoxLogAddMessage(string message)
        {
            if (listBoxLog.InvokeRequired)
            {
                listBoxLogAddMessage_Callback d = new listBoxLogAddMessage_Callback(listBoxLogAddMessage);
                this.Invoke(d, new object[] { message });
            }
            else
            {
                listBoxLog.Items.Add(message);
                listBoxLog.SelectedIndex = listBoxLog.Items.Count - 1;
            }
        }

        bool Timer1Flag = false;
        int timer = 0, TimeSet = 5;
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer++;
            if (Timer1Flag)
                return;
            string tmp = HTTPGet(BaseURL + "API.aspx?api=contralget1");
            try
            {
                TimeSet = int.Parse(tmp);
            }
            catch (Exception) { }
            if (TimeSet > 0 && timer >= TimeSet)
            {
                timer -= TimeSet;
                Timer1Flag = true;
                try
                {
                    string url = BaseURL + "API.aspx?api=upload&lightZ={LIGHTZ}&lightX={LIGHTX}&lightL={LIGHTL}&tempZ={TEMPZ}&tempX={TEMPX}&tempL={TEMPL}&wetZ={WETZ}&wetX={WETX}&wetL={WETL}".Replace("{LIGHTZ}", lightZ.ToString()).Replace("{LIGHTX}", lightX.ToString());
                    url = url.Replace("{LIGHTL}", lightL.ToString()).Replace("{TEMPZ}", tempZ.ToString()).Replace("{TEMPX}", tempX.ToString()).Replace("{TEMPL}", tempL.ToString()).Replace("{WETZ}", wetZ.ToString()).Replace("{WETX}", wetX.ToString()).Replace("{WETL}", wetL.ToString());

                    string message = HTTPGet(url);
                    if (!message.Equals("{\"statu\":\"ok\"}"))
                        listBoxLogAddMessage(DateTime.Now.ToString() + "  服务器返回异常:" + message);
                    else
                        listBoxLogAddMessage(DateTime.Now.ToString() + "  已上传至服务器。");
                }
                catch (Exception ext)
                {
                    listBoxLogAddMessage(DateTime.Now.ToString() + "  上传至服务器失败:" + ext.Message);
                }
                Timer1Flag = false;
            }
        }
    }
}


使用特权

评论回复
沙发
huangcunxiake| | 2023-9-26 20:37 | 只看该作者
C#语法好学 吗

使用特权

评论回复
板凳
598330983| | 2023-9-27 11:33 | 只看该作者
主要是这个做上位机容易。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

7

主题

1088

帖子

0

粉丝