zoukankan      html  css  js  c++  java
  • C# 编写的串口通信程序

    如果,翻看我之前的博客,会找到一篇用I/O模拟IIC通信的程序文章。好吧,如果找不到可以点击这里,这里就不在赘述了,系统也已经完全调试通过了。

    今天的任务是,把测试得到的数据在上位机的界面上显示出来,于是键盘手花了两天的时间模仿着巨人的肩膀通过了用C#编写的界面程序,界面很简单就像下面显示的一样。

    下面就一步一步给大伙展示一下我的程序吧。

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

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

     1         private void Form1_Load(object sender, EventArgs e)
     2         {
     3             serialPort1.PortName = "COM4";
     4             serialPort1.BaudRate = 9600;
     5             serialPort1.Open();
     6             this.textBox1.Clear();
     7             thread = new Thread(CrossThreadFlush);
     8             thread.IsBackground = true;
     9             thread.Start();
    10         }

    而后就是读取数据的操作,这时候用到事件

     1 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 

    在这个事件里面编程就可以了,但是在对窗体内文本进行操作的时候会发现出现了线程的冲突和错误。网上给出的一种解决方法是采用代理的方式具体的程序如下

      1  public partial class Form1 : Form
      2     {
      3         private delegate void FlushClient(); //代理
      4         Thread thread = null;
      5         uint DateTemp;
      6         uint Datefalg = 0;
      7         uint counter = 0;
      8         uint DateTemp1 = 0;
      9         uint TMP1;
     10         uint RH1;
     11         uint PRESS1;
     12         double TMP;
     13         double RH;
     14         double PRESS;
     15 
     16         public Form1()
     17         {
     18             InitializeComponent();
     19         }
     20 
     21 
     22         private void Form1_Load(object sender, EventArgs e)
     23         {
     24             serialPort1.PortName = "COM4";
     25             serialPort1.BaudRate = 9600;
     26             serialPort1.Open();
     27             this.textBox1.Clear();
     28             thread = new Thread(CrossThreadFlush);
     29             thread.IsBackground = true;
     30             thread.Start();
     31         }
     32 
     33       
     34         private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
     35         {
     36             counter=counter+1;
     37             DateTemp = (uint)this.serialPort1.ReadByte();
     38             if (DateTemp == 0xF1)
     39             {
     40                 Datefalg = 1;
     41                 DateTemp1 = 0;
     42                 DateTemp = 0;
     43                 counter=0;
     44             }
     45             if (DateTemp == 0xF2)
     46             {
     47                 Datefalg = 2;
     48                 DateTemp1 = 0;
     49                 DateTemp = 0;
     50                 counter=0;
     51             }
     52             if (DateTemp == 0xF3)
     53             {
     54                 Datefalg = 3;
     55                 DateTemp1 = 0;
     56                 DateTemp = 0;
     57                 counter=0;
     58             }
     59             if (Datefalg == 1 && DateTemp != 0xF1)
     60             {   if(counter==1)
     61                 DateTemp1 = DateTemp;
     62             if (counter == 2)
     63             {
     64                 TMP1 = DateTemp1 * 256 + DateTemp;
     65                 TMP = (0.01 * TMP1) - 40;
     66             }
     67             
     68             }
     69             if (Datefalg == 2 && DateTemp != 0xF2)
     70             {
     71                 if (counter == 1)
     72                     DateTemp1 = DateTemp;
     73                 if (counter == 2)
     74                 {
     75                     RH1 = DateTemp1 * 256 + DateTemp;
     76                     RH = (0.0405 * RH1) - 4-0.0000028*RH1*RH1;
     77                 }
     78 
     79             }
     80             if (Datefalg == 3 && DateTemp != 0xF3)
     81             {
     82                 if (counter == 1)
     83                     DateTemp1 = DateTemp;
     84                 if (counter == 2)
     85                 {
     86                     PRESS1 = DateTemp1 * 256 + DateTemp;
     87                     PRESS = (PRESS1-16384)*90/16384 + 30;
     88                 }
     89             }
     90         }
     91 
     92         private void CrossThreadFlush()
     93         {
     94             while (true)
     95             {
     96                 //将sleep和无限循环放在等待异步的外面
     97                 Thread.Sleep(500);
     98                 ThreadFunction();
     99             }
    100         }
    101     
    102         private void ThreadFunction()
    103         {
    104             if (this.textBox1.InvokeRequired)//等待异步
    105             {
    106                 FlushClient fc = new FlushClient(ThreadFunction);
    107                 this.Invoke(fc); //通过代理调用刷新方法
    108                 
    109             }
    110             else
    111             textBox1.Text = TMP.ToString("0.00");
    112             if (this.textBox2.InvokeRequired)//等待异步
    113             {
    114                 FlushClient fc = new FlushClient(ThreadFunction);
    115                 this.Invoke(fc); //通过代理调用刷新方法
    116 
    117             }
    118             else
    119                 textBox2.Text = RH.ToString("0.00");
    120             if (this.textBox3.InvokeRequired)//等待异步
    121             {
    122                 FlushClient fc = new FlushClient(ThreadFunction);
    123                 this.Invoke(fc); //通过代理调用刷新方法
    124 
    125             }
    126             else
    127                 textBox3.Text = PRESS.ToString("0.00");
    128             
    129         }
    130       
    131         
    132     }

    通过这样的一番调试之后才得以程序正确的运行,由于工科程序员和计算机程序员的编程思想还是有差别的,所以写的不好的地方请轻喷。

    【重要通知】

    为了丰富给位程序狗&硬件狗们的业余生活,我们做了一个艰难的决定,我们“泡泡鱼工作室”开通了“硬件为王”公共订阅号,微信号:king_hardware。关注智能硬件和互联网创业,以及日常学习资料/搞笑段子推送。请猛扫下面的二维码!

    Powered By Bubble_fish ; Email:bubble_fish@yeah.net

    原谅我这一生不羁放纵爱自由,也怕有天我会跌倒!
  • 相关阅读:
    java小知识点5
    java小知识点4
    java小知识点3
    编程之法:面试和算法心得(寻找最小的k个数)
    389. Find the Difference
    104. Maximum Depth of Binary Tree
    485. Max Consecutive Ones
    693. Binary Number with Alternating Bits
    463. Island Perimeter
    566. Reshape the Matrix
  • 原文地址:https://www.cnblogs.com/zhaoming510/p/3965061.html
Copyright © 2011-2022 走看看