串口编程案例

前提准备
虚拟串口
串口调试助手
其他资源


模拟COM1串口发送信息到COM2接收
首先、下载虚拟串口进行安装,安装完后打开软件(汉化过程忽略)
这里写图片描述

右键打开计算机–>管理–>设备管理器–>端口
这里写图片描述


打开VS,新建项目(Windows 应用程序)
这里写图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

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

        enum FLAG : int
        {
            NOT_ACTIVE = 0,
            PASS = 1,
            TOO_LOW = 2,
            TOO_HIGH = 3,
            FAIL = 4
        };
        static System.IO.Ports.SerialPort SPort;
        static Thread th;

        private void Form1_Load(object sender, EventArgs e)
        {
            string args = comboBox1.Text;
            if (args.Length < 1)
            {
                richTextBox1.AppendText("参数无效!\n");
                richTextBox1.AppendText("示例:BTS4002_Demo.exe COM1\n");
            }
            else
            {
                string PortName = args;
                richTextBox1.AppendText("端口号:" + PortName + "\n");
                richTextBox1.AppendText("按任意键清除显示,按q退出\n");
                SPort = new System.IO.Ports.SerialPort();
                SPort.PortName = PortName;
                SPort.BaudRate = 115200;
                SPort.DataBits = 8;
                SPort.Parity = System.IO.Ports.Parity.None;
                SPort.StopBits = System.IO.Ports.StopBits.One;
                SPort.DataReceived += SPort_DataReceived;
                try
                {
                    SPort.Open();
                    //控制终端就续信号
                    SPort.RtsEnable = false;//很重要
                    //请求发送信号
                    SPort.DtrEnable = false;//很重要
                    th = new Thread(new ThreadStart(Task));
                    th.IsBackground = true;
                    th.Start();
                }
                catch (Exception ex)
                {
                    richTextBox1.AppendText("端口号打开失败:" + ex.Message + "\n");
                }
            }


            CheckForIllegalCrossThreadCalls = false;
        }

        private static void SPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            //SPort.ReadExisting
            //SPort.Read
        }

        static int GetFlagPosition(string DataLine, ref FLAG Flag)
        {
            Flag = FLAG.NOT_ACTIVE;
            for (int i = 5; i < DataLine.Length; i++)
            {
                switch (DataLine.Substring(i, 1).ToUpper())
                {
                    case "N":
                        {
                            Flag = FLAG.NOT_ACTIVE;
                            return i;
                        }
                    case "P":
                        {
                            Flag = FLAG.PASS;
                            return i;
                        }
                    case "L":
                        {
                            Flag = FLAG.TOO_LOW;
                            return i;
                        }
                    case "H":
                        {
                            Flag = FLAG.TOO_HIGH;
                            return i;
                        }
                    case "F":
                        {
                            Flag = FLAG.FAIL;
                            return i;
                        }
                }
            }
            return -1;
        }

        void Task()
        {
            Thread.Sleep(100);
            while (true)
            {
                if (SPort.IsOpen)
                {
                    string DataLine = SPort.ReadLine();
                    string Prefix = DataLine.Substring(0, 5).ToUpper();
                    FLAG Flag = FLAG.NOT_ACTIVE;
                    float Value;
                    int Position;
                    Position = GetFlagPosition(DataLine, ref Flag);
                    switch (Prefix)
                    {
                        case "$STA:":
                            {
                                richTextBox1.AppendText("+++++++++++++++++++++++++++++\n");
                                richTextBox1.AppendText("测试开始了\n");
                                break;
                            }
                        case "$PAS:":
                            {
                                richTextBox1.AppendText("测试通过了:)\n");
                                break;
                            }
                        case "$BAD:":
                            {
                                richTextBox1.AppendText("测试失败了:(\n");
                                break;
                            }
                        case "$VOL:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("电压:"+ Value + "伏\n");
                                    }
                                }
                                break;
                            }
                        case "$CHR:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("充电电压:" + Value + "伏\n");
                                    }
                                }
                                break;
                            }
                        case "$IR*:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("内阻:"+Value+ "毫欧\n");
                                    }
                                }
                                break;
                            }
                        case "$DIS:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("放电电压:" + Value + "伏\n");
                                    }
                                }
                                break;
                            }
                        case "$ST*:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("短路时间:" + Value + "微秒\n");
                                    }
                                }
                                break;
                            }
                        case "$SV*:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("短路后电压:" + Value + "电压\n");
                                    }
                                }
                                break;
                            }
                        case "$SRV:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("短路恢复后电压:" + Value + "电压\n");
                                    }
                                }
                                break;
                            }
                        case "$DOT:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("过流时间:" + Value + "毫秒\n");
                                    }
                                }
                                break;
                            }
                        case "$DOC:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("过流电流:" + Value + "安培\n");
                                    }
                                }
                                break;
                            }
                        case "$DOV:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("过流后电压:" + Value + "伏\n");
                                    }
                                }
                                break;
                            }
                        case "$R1*:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("R1:" + Value + "千欧\n");
                                    }
                                }
                                break;
                            }
                        case "$R2*:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("R2:" + Value + "千欧\n");
                                    }
                                }
                                break;
                            }
                        case "$OWV:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("HDQ电压:" + Value + "伏\n");
                                    }
                                }
                                break;
                            }
                        case "$HIR:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("绝缘阻抗:" + Value + "兆欧\n");
                                    }
                                }
                                break;
                            }
                        case "$TMP:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("温度:" + Value + "度\n");
                                    }
                                }
                                break;
                            }
                        case "$TIM:":
                            {
                                if (Position != -1)
                                {
                                    if (float.TryParse(DataLine.Substring(5, Position - 5), out Value))
                                    {
                                        richTextBox1.AppendText("测试时间:" + Value + "毫秒\n");
                                        richTextBox1.AppendText("=============================");
                                    }
                                }
                                break;
                            }
                        default:
                            {
                                richTextBox1.AppendText("未知指令:"+ Prefix);
                                break;
                            }
                    }
                    //Console.WriteLine(DataLine);

                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }



    }
}

下载串口调试助手,打开
这里写图片描述


用串口调试助手发送,效果如图:
这里写图片描述


简单串口编程结束!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值