This repository was archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialPort.cs
138 lines (124 loc) · 3.4 KB
/
SerialPort.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Author: Pawel Walczak (pewusoft)
* Date: 2015-01-12 20:50
*
*/
using System;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
namespace SDRSharp.SerialController
{
/// <summary>
/// Description of SerialPort.
/// </summary>
public class SerialPortCtrl
{
bool _enableLogging = true;
public bool EnableLogging {
set { this._enableLogging = value; }
get { return this._enableLogging; }
}
StreamWriter logger;
SerialPort _port;
public delegate void FrequencyChangeHandler(object sender, long freq);
public event FrequencyChangeHandler OnFrequencyChange;
public static string[] GetAllPorts()
{
try {
return SerialPort.GetPortNames();
} catch {
MessageBox.Show("Exception while getting available serial ports", "SerialController", MessageBoxButtons.OK, MessageBoxIcon.Error);
return new string[0];
}
}
public bool openPort(string portName) {
try {
if (_port != null && _port.IsOpen) {
return false;
}
if (portName == null || (portName.Trim().Equals("")))
return false;
_port = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One);
_port.DataReceived += Port_DataReceived;
if (_port != null) {
_port.Open();
if (_enableLogging) {
prepareLogger();
log("Port " + _port.PortName + " opened");
}
return true;
}
return false;
} catch (Exception) {
MessageBox.Show("Couldn't open port "+portName, "SerialController", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
public bool closePort() {
if (_port != null) {
if (_port.IsOpen) {
try {
_port.Close();
if (_enableLogging) {
log("Port " + _port.PortName + " closed");
closeLogger();
}
return true;
} catch (IOException) {
return false;
}
} else {
return false;
}
}
return false;
}
void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (OnFrequencyChange == null) return;
string data = _port.ReadLine();
// log commands to file
log("Received on COM port: "+data);
// AR-ONE RF command parse, as simple as can be, but faster than regex
if (data.StartsWith("RF", StringComparison.Ordinal)) {
long freq;
if (long.TryParse(data.Substring("RF".Length), out freq)) {
OnFrequencyChange(this, freq);
log("Changing frequency to: "+freq.ToString("N0"));
}
}
}
void log(String str) {
if (logger!=null) {
logger.WriteLine("[" + DateTime.Now + "]: " + str.Trim());
}
}
void prepareLogger() {
try {
if (logger != null) {
logger.Close();
}
logger = new StreamWriter(new FileStream("serial.log",
FileMode.Append,
FileAccess.Write,
FileShare.ReadWrite,
1024,
FileOptions.WriteThrough));
logger.AutoFlush = true;
} catch (Exception) {
logger = null;
}
}
void closeLogger() {
try {
if (logger != null) {
logger.Close();
}
logger = null;
} catch (Exception) {
logger = null;
}
}
}
}