|
|
@ -0,0 +1,179 @@ |
|
|
|
|
|
|
|
using System; |
|
|
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
|
|
|
using System.IO.Ports; |
|
|
|
|
|
|
|
using System.Text; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace SerialASCIISample |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public class SerailComm |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
private StringBuilder _buffer; |
|
|
|
|
|
|
|
private SerialPort _serial; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly char CR = (char)0x0D; |
|
|
|
|
|
|
|
private readonly char LF = (char)0x0A; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsOpen { get { return _serial.IsOpen; } } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public event EventHandler<EventMessageArgs> MessageReceived; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SerailComm(string port, int baudRate, Parity parity, int dataBits, StopBits stopBits) : base() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
_buffer = new StringBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_serial = new SerialPort(port, baudRate, parity, dataBits, stopBits); |
|
|
|
|
|
|
|
_serial.DataReceived -= DataReceived; |
|
|
|
|
|
|
|
_serial.DataReceived += DataReceived; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool Open() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
_serial.Open(); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (MessageReceived != null) |
|
|
|
|
|
|
|
MessageReceived(this, new EventMessageArgs(ex.Message)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool Close() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
_serial.Close(); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (MessageReceived != null) |
|
|
|
|
|
|
|
MessageReceived(this, new EventMessageArgs(ex.Message)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void DataReceived(object sender, SerialDataReceivedEventArgs e) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SerialPort serial = sender as SerialPort; |
|
|
|
|
|
|
|
if (serial == null) |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
string received = serial.ReadExisting(); |
|
|
|
|
|
|
|
received = received.Replace(LF.ToString(), ""); |
|
|
|
|
|
|
|
//if (MessageReceived != null) |
|
|
|
|
|
|
|
// MessageReceived(this, new EventMessageArgs(received)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_buffer.Append(received); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string bufferContent = _buffer.ToString(); |
|
|
|
|
|
|
|
int commandEndIndex = -1; |
|
|
|
|
|
|
|
while ((commandEndIndex = bufferContent.IndexOf(CR)) >= 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
string completeCommand = bufferContent.Substring(0, commandEndIndex).Trim(); |
|
|
|
|
|
|
|
_buffer.Remove(0, commandEndIndex + 1); |
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(completeCommand)) |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HandleCommand(serial, completeCommand); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bufferContent = _buffer.ToString(); |
|
|
|
|
|
|
|
commandEndIndex = -1; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (MessageReceived != null) |
|
|
|
|
|
|
|
MessageReceived(this, new EventMessageArgs(ex.Message)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SendSerialMessage(serial, "ERR3"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void HandleCommand(SerialPort serial, string command) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
command = command.ToUpper(); |
|
|
|
|
|
|
|
// Read Data |
|
|
|
|
|
|
|
if (command.StartsWith("RD")) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
string message = "1234.4,234.3,34.2,4.1,1.33,29.5"; |
|
|
|
|
|
|
|
SendSerialMessage(serial, message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Read Status |
|
|
|
|
|
|
|
else if (command.StartsWith("ST")) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
string message = "0,0,0,0,1,1"; |
|
|
|
|
|
|
|
SendSerialMessage(serial, message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Write Data |
|
|
|
|
|
|
|
else if (command.StartsWith("WR")) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
string req = command.Substring(2, command.Length - 2); |
|
|
|
|
|
|
|
string[] parameters = req.Split('='); |
|
|
|
|
|
|
|
if (parameters.Length != 2) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SendSerialMessage(serial, "ERR2"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (parameters[0]) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
case "LA": |
|
|
|
|
|
|
|
case "SH": |
|
|
|
|
|
|
|
case "EF": |
|
|
|
|
|
|
|
case "CV": |
|
|
|
|
|
|
|
case "SP": |
|
|
|
|
|
|
|
case "TP": |
|
|
|
|
|
|
|
case "LK": |
|
|
|
|
|
|
|
WriteData(serial, parameters[0], parameters[1]); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
SendSerialMessage(serial, "ERR2"); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Unknown Command |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SendSerialMessage(serial, "ERR1"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void WriteData(SerialPort serial, string addr, string value) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
float casted = 0f; |
|
|
|
|
|
|
|
if (!float.TryParse(value, out casted)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
SendSerialMessage(serial, "ERR2"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (MessageReceived != null) |
|
|
|
|
|
|
|
MessageReceived(this, new EventMessageArgs($"{addr}: {casted.ToString("N2")}")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SendSerialMessage(serial, "OK"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void SendSerialMessage(SerialPort serial, string message) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
serial.Write(message + CR + LF); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class EventMessageArgs : EventArgs |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public string Message; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public EventMessageArgs(string message) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Message = message; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |