串口通信C#

1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Threading.Tasks;
6: using System.IO.Ports;
7: using System.Threading;
8:
9: namespace ADAM-4150模拟实验
10:{
11:    class Program
12:    {
13:        static void Main(string[] args)
14:        {
15:            SerialPort ComDevice = new SerialPort(); 
16:            //新建串行端口对象,命名空间:using System.IO.Ports
17:            string portName = "COM2";//定义端口号
18:            int baudRate = 9600;//定义波特率
19:            Parity parity = Parity.None;//定义校验位
20:            int dataBits = 8;//定义数据位
21:            StopBits stopBits = StopBits.One;//定义停止位
22:            Encoding EncodingType = Encoding.ASCII;//定义用ASCII编码类型
23:            string SendData;//定义发送的命令
24:
25:            //初始化:
26:            ComDevice.PortName = portName;//端口号初始化
27:            ComDevice.BaudRate = baudRate;//波特率初始化
28:            ComDevice.Parity = parity;//校验位初始化
29:            ComDevice.DataBits = dataBits;//数据位初始化
30:            ComDevice.StopBits = stopBits;//停止位初始化
31:            ComDevice.Encoding = EncodingType;//初始化解码方式
32:            ComDevice.Open();//打开串行端口
33:
34:            //发送数据:
35:            SendData = "#011001\r";
36:            //设置发送的命令为#011001+回车符,对应控制DO0通道打开
37:            ComDevice.Write(SendData);//发送命令
38:
39:            SendData = "#011200\r";//设置发送的命令为控制DO2通道断开
40:            ComDevice.Write(SendData);//发送命令
41:
42:            SendData = "$016\r";//设置发送的命令为查询状态信息
43:            ComDevice.Write(SendData);//发送命令
44:            Thread.Sleep(1000);
45:            //线程等待1秒来接收数据,命名空间:using System.Threading
46:
47:            //接收数据:
48:            byte[] ReDatas = new byte[ComDevice.BytesToRead];
49:            ComDevice.Read(ReDatas, 0, ReDatas.Length);//读取数据
50:            string content = new UTF8Encoding().GetString(ReDatas);
51:            //将接收数据转成字符串
52:            Console.WriteLine(content);//转成字符串格式后输出显示
53:
54:            //数据解析:       
55:            int[] Redatas_int= new int[7];
56:            for(int i = 0; i < 7; i++)//将字符串转存成十六进制数组
57:            {
58:                if (content[i] <= '9' && content[i] >= '0')
59:                    Redatas_int[i] = content[i] - '0';
60:                else if (content[i] <= 'Z' && content[i] >= 'A')
61:                    Redatas_int[i] = content[i] - 'A' + 10;
62:                else
63:                    Redatas_int[i] = content[i];
64:            }
65:            int[] DOstatus = new int[8];//定义记录各DO通道状态数组DOstatus
66:            int wei= Redatas_int[2];//取第三位
67:            for (int i = 0; i < 4; i++)//解析第三位存入DOstatus数组
68:            {
69:                DOstatus[i] = wei & 1;
70:                wei=wei >> 1;
71:            }
72:            wei = Redatas_int[1];//取第二位
73:            for (int i = 0; i < 4; i++)//解析第二位存入DOstatus数组
74:            {
75:                DOstatus[i+4] = wei & 1;
76:                wei = wei >> 1;
77:            }
78:            for(int i=0;i<8;i++)//将DO各通道状态输出
79:            {
80:                Console.WriteLine("通道DO"+i+": "+DOstatus[i]);
81:            }
82:            
83:            int[] DIstatus = new int[7];//定义记录各DI通道状态数组DOstatus
84:            wei = Redatas_int[4];//取第五位
85:            for (int i = 0; i < 4; i++)//解析第五位存入DOstatus数组
86:            {
87:                DIstatus[i] = wei & 1;
88:                wei = wei >> 1;
89:            }
90:            wei = Redatas_int[3];//取第四位
91:            for (int i = 0; i < 3; i++)//解析第四位存入DOstatus数组
92:            {
93:                DIstatus[i + 4] = wei & 1;
94:                wei = wei >> 1;
95:            }
96:            for (int i = 0; i < 7; i++)//将DI各通道状态输出
97:            {
98:                Console.WriteLine("通道DI" + i + ": " + DIstatus[i]);
99:            }
100:
101:           ComDevice.Close();//关闭串行端口
102:       }
103:    }
104:}

 

你可能感兴趣的:(串口通信C#)