-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSerial.cs
62 lines (49 loc) · 1.42 KB
/
Serial.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
namespace Aida64_Esp8266_DisplayControler
{
public class Serial
{
private SerialPort _serial;
public string LastError { get; private set; }
public bool IsOpen { get; private set; }
public Serial(string portname, SerialDataReceivedEventHandler handler)
{
IsOpen = false;
_serial = new SerialPort(portname);
_serial.BaudRate = 115200;
_serial.DataBits = 8;
_serial.Parity = Parity.None;
_serial.StopBits = StopBits.One;
_serial.Handshake = Handshake.None;
_serial.ReceivedBytesThreshold = 13;
_serial.RtsEnable = false;
_serial.DtrEnable = false;
_serial.DataReceived += new SerialDataReceivedEventHandler(handler);
}
public bool Open()
{
try
{
_serial.Open();
}
catch (Exception ex)
{
LastError = ex.Message;
return false;
}
IsOpen = true;
return true;
}
public void SendText(string str)
{
if (_serial.IsOpen)
{
byte[] ba = Encoding.UTF8.GetBytes(str);
_serial.Write(ba, 0, ba.Length);
}
}
}
}