-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
32 lines (30 loc) · 1.07 KB
/
Main.cs
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
using System;
using System.IO.Ports;
using System.Threading;
using System.Threading.Tasks;
class SerialPortTest {
const string handshakeRequest = "HMT";
const string handshakeResponse = "RDY";
const string dataRequest = "DAT";
private const string LF = "\n";
static void Main(string[] args) {
foreach (var s in SerialPort.GetPortNames()) {
Console.WriteLine(s);
var serialPort = new SerialPort(s, 9600);
serialPort.ReadTimeout = 1000;
serialPort.WriteTimeout = 1000;
serialPort.RtsEnable = true;
serialPort.DtrEnable = true;
serialPort.NewLine = LF;
serialPort.Open();
while (!serialPort.IsOpen);
serialPort.WriteLine(handshakeRequest);
var res = serialPort.ReadLine().Trim(new char[] {'\r', '\n'});
Console.WriteLine(res);
if (res != handshakeResponse) return;
serialPort.WriteLine(dataRequest);
Console.WriteLine(serialPort.ReadLine());
serialPort.Close();
}
}
}