C# 串口测试 ASCII

串口测试 ASCII

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SkinGenieTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static SerialPort serialPort = null;
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(COM.Text.Trim()))
            {
                //ResultDisplay("请填写串口号",Color.Red);
                MessageBox.Show("请填写串口号!");
                return;
            }
            COM.Enabled = false;
            //打開串口
            serialPort = new SerialPort();
            serialPort.PortName = COM.Text;
            serialPort.BaudRate = Convert.ToInt32(9600);
            serialPort.Parity = Parity.None;
            serialPort.DataBits = Convert.ToInt32(8);
            serialPort.StopBits = StopBits.One;
            serialPort.Handshake = Handshake.None;
            serialPort.WriteTimeout = 200;
            serialPort.ReadTimeout = 200;
            if (serialPort.IsOpen == false)
            {
                serialPort.Open();
            }
            if (serialPort.IsOpen == true)
            {
                MessageBox.Show("串口已打开");
            }
        }
        //private string ResultValueOld = "";
        private void Read()
        {
            for (int i = 0; i < 100000; i++)
            {
                R();
                richTextBox1.BeginInvoke(new Action(() => { richTextBox1.Text = richTextBox1.Text + ResultValue; }));
            }
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            //Task t1 = Task.Run(() => CmdTest());
            //Task t2 = Task.Run(() => Read());
        }

        private void F(string cmd)
        {
            try
            {
                if (serialPort.IsOpen == false)
                {
                    serialPort.Open();
                }
                serialPort.DiscardInBuffer();
                serialPort.Write(cmd);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void R()
        {
            try
            {
                SerialPort sp = serialPort;
                sp.ReadExisting();
                sp.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                serialPort.DiscardOutBuffer();
                //开辟接收缓冲区
                byte[] ReDatas = new byte[serialPort.BytesToRead];
                //从串口读取数据
                serialPort.Read(ReDatas, 0, ReDatas.Length);
                //实现数据的解码与显示
                AddData(ReDatas);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private static int OutputLength = 0;
        private static string ResultValue = "";
        public void AddData(byte[] data)
        {
            try
            {
                Thread.Sleep(200);
                //if (data.Length<15)
                //{
                //    return;
                //}
                //OutputLength = data.Length;
                //ResultValue = Encoding.Default.GetString(data);
                ResultValue = Encoding.UTF8.GetString(data);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
        DataTable dt = new DataTable();
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i][2] = "";
                }
                dataGridView1.DataSource = dt;
                serialPort.DiscardInBuffer();
                serialPort.DiscardOutBuffer();
                richTextBox1.Clear();
                Task t1 = Task.Run(() => DisplayInfo());
            }
            catch
            { }
        }
        private void DisplayInfo()
        {
            try
            {
                string SkinGenieNum="";

                char[] SendChars = @"AT+INQ
".ToCharArray();
                int CharsLength = SendChars.Length;


                serialPort.Encoding = System.Text.Encoding.GetEncoding("GB2312");  //解决中午乱码问题,国标2312编码格式
                serialPort.Write(SendChars, 0, CharsLength);
                //richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss :"));
                richTextBox1.BeginInvoke(new Action(() => { richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss :")+@"
"); }));
                string StopString="";
                for (int i = 0; i < 26; i++)
                {
                    Thread.Sleep(500);
                    serialPort.Encoding = System.Text.Encoding.GetEncoding("GB2312");//解决中午乱码问题,国标2312编码格式 
                    //richTextBox1.AppendText(serialPort.ReadExisting());
                    richTextBox1.BeginInvoke(new Action(() => { 
                        richTextBox1.AppendText(serialPort.ReadExisting());
                        StopString = richTextBox1.Text;
                    }));
                    if (StopString.Contains("STOP"))
                    {
                        break;
                    }
                }
                richTextBox1.BeginInvoke(new Action(() => { richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss :")); }));
                //找SkinGenie出现的行
                richTextBox1.BeginInvoke(new Action(() => {
                    for (int j = 0; j < richTextBox1.Lines.Length; j++)
                    {
                        if (richTextBox1.Lines[j].Contains("SkinGenie"))
                        {
                            //MessageBox.Show(j.ToString());
                            SkinGenieNum = richTextBox1.Lines[j].Substring(5,1);
                            Task t1 = Task.Run(() => Conn(SkinGenieNum));
                            richTextBox1.AppendText("设备号是:"+ SkinGenieNum+@"
");
                            break;
                        }
                    }
                    if (SkinGenieNum=="")
                    {
                        //ResultDisplay("未找到设备", Color.Red);
                        richTextBox1.BeginInvoke(new Action(() => { richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss :")+ 
                            "未找到设备"); }));
                    }
                }));
                
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }
        }

        private void Conn(string SkinGenieNum)
        {
            //发送链接命令
            char[] SendChars = ("AT+CONN" + SkinGenieNum + @"
").ToCharArray();
            int CharsLength = SendChars.Length;

            int RowCount = 0;

            serialPort.Encoding = System.Text.Encoding.GetEncoding("GB2312");
            serialPort.Write(SendChars, 0, CharsLength);
            //richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss :"));
            richTextBox1.BeginInvoke(new Action(() => { richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss :") + @"
"); }));
            string StopString = "";
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(500);
                serialPort.Encoding = System.Text.Encoding.GetEncoding("GB2312");
                //richTextBox1.AppendText(serialPort.ReadExisting());
                richTextBox1.BeginInvoke(new Action(() => {
                    richTextBox1.AppendText(serialPort.ReadExisting());
                    StopString = richTextBox1.Text;
                }));
                if (StopString.Contains("+CONNECTED"))
                {
                    //Task t1 = Task.Run(() => SendSC());
                    string ReturnString = "";
                    ReturnString=SendCMD(@"SC
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2]= ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"SP
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"SB
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"SI
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"ST
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"P0
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"P1
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"P2
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"P3
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"P4
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"IL
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"IM
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"IH
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"ID
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"TD
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                        RowCount = RowCount + 1;
                    }));
                    

                    Thread.Sleep(1001);
                    ReturnString = SendCMD(@"T0
");
                    dataGridView1.BeginInvoke(new Action(() => {
                        dt.Rows[RowCount][2] = ReturnString;
                        dataGridView1.DataSource = dt;
                    }));
                    break;
                }
            }
        }

        private string SendCMD(string cmd)
        {
            char[] SendChars = cmd.ToCharArray();
            int CharsLength = SendChars.Length;

            //serialPort.Encoding = System.Text.Encoding.GetEncoding("GB2312");
            serialPort.Encoding = System.Text.UTF32Encoding.GetEncoding("GB2312");
            serialPort.Write(SendChars, 0, CharsLength);
            //richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss :"));
            richTextBox1.BeginInvoke(new Action(() => { richTextBox1.AppendText(@""+DateTime.Now.ToString("HH:mm:ss :") + cmd + @""); }));
            string ReturnString = "";
            Thread.Sleep(200);
            serialPort.Encoding = System.Text.Encoding.GetEncoding("GB2312");
            ReturnString = serialPort.ReadExisting();
            richTextBox1.BeginInvoke(new Action(() => {
                richTextBox1.AppendText(ReturnString);
            }));
            return ReturnString;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            
        }
        //private void ResultDisplay(string ResultString, Color color)
        //{
        //    if (Result_1.Visible == true && Result_2.Visible == true)
        //    {
        //        Result_1.Text = ResultString;
        //        Result_1.ForeColor = color;
        //        Result_2.Visible = false;
        //        Result_2.Text = "";
        //    }
        //    else if (Result_1.Visible == true && Result_2.Visible == false)
        //    {
        //        Result_1.Visible = false;
        //        Result_1.Text = "";
        //        Result_2.Visible = true;
        //        Result_2.Text = ResultString;
        //        Result_2.ForeColor = color;
        //    }
        //    else if (Result_1.Visible == false && Result_2.Visible == true)
        //    {
        //        Result_2.Visible = false;
        //        Result_2.Text = "";
        //        Result_1.Visible = true;
        //        Result_1.Text = ResultString;
        //        Result_1.ForeColor = color;
        //    }
        //}
        private void Form1_Load(object sender, EventArgs e)
        {
            int TestRow = 0;
            dt.Clear();
            dt.Columns.Add("测试命令", typeof(string));
            dt.Columns.Add("测试描述", typeof(string));
            dt.Columns.Add("测试结果", typeof(string));

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "SC";
            dt.Rows[TestRow][1] = "get contact status";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "SP";
            dt.Rows[TestRow][1] = "get program status";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "SB";
            dt.Rows[TestRow][1] = "get battery status";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "SI ";
            dt.Rows[TestRow][1] = "get intensity status";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "ST";
            dt.Rows[TestRow][1] = "get timer status";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "P0";
            dt.Rows[TestRow][1] = "Turn off program";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "P1";
            dt.Rows[TestRow][1] = "run program 1";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "P2";
            dt.Rows[TestRow][1] = "run program 2";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "P3";
            dt.Rows[TestRow][1] = "run program 4";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "P4";
            dt.Rows[TestRow][1] = "run program 4";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "IL";
            dt.Rows[TestRow][1] = "set low intensity";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "IM";
            dt.Rows[TestRow][1] = "set medium intensity";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "IH";
            dt.Rows[TestRow][1] = "set high intensity";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "ID";
            dt.Rows[TestRow][1] = "standard intensity";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "TD";
            dt.Rows[TestRow][1] = "timer disabled";
            TestRow = TestRow + 1;

            dt.Rows.Add();
            dt.Rows[TestRow][0] = "T0";
            dt.Rows[TestRow][1] = "turn off now";
            TestRow = TestRow + 1;

            dataGridView1.DataSource = dt;
        }
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值