需求分析

因为毕设中要用到上位机通讯,目前暂时需要实现串口通讯和上位机通讯,对接受到的数据应该保证能够实时显示和储存,显示可以包括二维图像显示,三维模型显示,数据显示等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 SerialPort port = new SerialPort();

// 获取可用串口列表
string[] ports = SerialPort.GetPortNames();

// 打印可用串口列表
foreach (string portname in ports)
{
Console.WriteLine("Available port: " + portname);
}
string portBuf = Console.ReadLine();
//Console.WriteLine(portBuf);
port.PortName = ports[int.Parse(portBuf)];

port.BaudRate = 115200; // 设置波特率为9600
port.DataBits = 8; // 设置数据位为8
port.StopBits = StopBits.One; // 设置停止位为1
port.Parity = Parity.None; // 设置校验位为None

port.Open();

while (true)
{
string dataRead = port.ReadExisting();
Console.WriteLine(dataRead);


string dataSend = Console.ReadLine();
port.Write(dataSend);
Thread.Sleep(100);
}
port.Close();