Reading ()
{
if (_keepReading) p> {
_keepReading = false;
_readThread.Join () ;//block until exits
_readThread = null;
}
}
/// Get the data and pass it on.
private void ReadPort ()
{
while (_keepReading) p> {
if (_serialPort.IsOpen) p> {
byte [] readBuffer = new byte [_serialPort.ReadBufferSize + 1];
try
{
// If there are bytes available on the serial port,
// Read returns up to "count" bytes, but will not block (wait)
// for the remaining bytes. If there are no bytes available
// on the serial port, Read will block until at least one byte
// is available on the port, up until the ReadTimeout milliseconds
// have elapsed, at which time a TimeoutException will be thrown.
int count = _serialPort.Read (readBuffer, 0, _serialPort.ReadBufferSize);
String SerialIn = System.Text.Encoding.ASCII.GetString (readBuffer, 0, count);
DataReceived (SerialIn);
}
catch (TimeoutException) {}
}
else
{
TimeSpan waitTime = new TimeSpan (0, 0, 0, 0, 50);
Thread.Sleep (waitTime);
}
}
}
/// Open the serial port with current settings.
public void Open ()
{
Close ();
try
{
_serialPort.PortName = Settings.Port.PortName;
_serialPort.BaudRate = Settings.Port.BaudRate;
_serialPort.Parity = Settings.Port.Parity;
_serialPort.DataBits = Settings.Port.DataBits;
_serialPort.StopBits = Settings.Port.StopBits;
_serialPort.Handshake = Settings.Port.Handshake;
// Set the read/write timeouts
_serialPort.ReadTimeout = 50;
_serialPort.WriteTimeout = 50;
_serialPort.Open ();
StartReading ();
}
catch (IOException)
{
StatusChanged (String.Format ("{0} does not exist ", Settings.Port.PortName));
}
catch (UnauthorizedAccessException)
{
StatusChanged (String.Format ("{0} already in use ", Settings.Port.PortName));
}
catch (Exception ex)
{
StatusChanged (String.Format ("{0}", ex.ToString ()));
}
// Update the status
if (_serialPort.IsOpen) p> {
string p = _serialPort.Parity.ToString (). Substring (0, 1);// First char
string h = _serialPort.Handshake.ToString ();
if (_serialPort.Handshake == Handshake.None)
h = "no handshake ";// more descriptive than" None "
StatusChanged (String.Format ("{0}: {1} bps, {2} {3} {4}, {5} ",
_serialPort.PortName, _serialPort.BaudRate,
_serialPort.DataBits, p, (int) _serialPort.StopBits, h));
}
else
{
StatusChanged (String.Format ("{0} already in use ", Settings.Port.PortName));
}
}
/// Close the serial port.
public void Close ()
{
StopReading ();
_serialPort.Close ();
StatusChanged ("connection closed ");
}
/// Get the status of the serial port.
public bool IsOpen
{
get
{
return _serialPort.IsOpen;
}
}
/// Get a list of the available ports. Already opened ports
///are not returend.
public string [] GetAvailablePorts ()
{
return SerialPort.GetPortNames ();
}
/// Send data to the serial port after appending line ending.
/// An string containing the data to send.
public void Send (string data)
{
if (IsOpen)
{
string lineEnding = "";
switch (Settings.Option.AppendToSend)
{
case Settings.Option.AppendType.AppendCR:
lineEnding = " R"; break;
case Settings.Option.AppendType.AppendLF:
lineEnding = " N"; break;
case Settings.Option.AppendType.AppendCRLF:
lineEnding = " R n"; break;
}
_serialPort.Write (data + LineEnding);
}
}
}
}
В