//-----------------------------------------------------------------------
//
// Copyright © 2012 Nils Hammar. All rights reserved.
//
//-----------------------------------------------------------------------
/*
* Software to access vehicle information via the OBD-II connector.
*
* Copyright © 2012 Nils Hammar
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* Alternative licensing is possible, see the licensing document.
*
* The above text may not be removed or modified.
*/
namespace Protocol
{
using System;
using System.IO.Ports;
using global::SharedObjects.Protocol;
///
/// Class for handling interaction with serial port.
///
public class SerialPortHandler : IDisposable
{
///
/// Parameters for port.
///
private ISerialPortParameters serialPortParameters;
///
/// Serial port instance.
///
private SerialPort serialPort = null;
///
/// Initializes a new instance of the class.
///
/// Parameters for port.
public SerialPortHandler(ISerialPortParameters serialPortParameters)
{
this.serialPortParameters = serialPortParameters;
}
///
/// Open the serial port.
///
/// Notice that it is closed and re-opened if it's already open.
///
///
public void open()
{
this.close();
this.serialPort = new SerialPort(
this.serialPortParameters.serialPortName,
this.serialPortParameters.serialPortSpeed,
this.serialPortParameters.serialPortParity,
this.serialPortParameters.serialPortDatabits,
this.serialPortParameters.serialPortStopbits);
this.serialPort.Open();
}
///
/// Implementation of IDisposable.
///
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Close the serial port.
///
public void close()
{
if (this.serialPort != null)
{
this.serialPort.Close();
this.serialPort = null;
}
}
///
/// Write the string to the serial port.
///
/// String to be written.
public void write(string str)
{
this.serialPort.Write(str);
}
///
/// Write bytes in hex to the serial port.
///
/// This method converts bytes to hex form before writing to the port.
///
///
/// Data to write.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Reviewed.")]
public void write(byte[] ba)
{
bool first = true;
foreach (byte b1 in ba)
{
if (!first)
{
this.serialPort.Write(" ");
}
else
{
first = false;
}
this.serialPort.Write(b1.ToString("x2"));
}
this.serialPort.Write(ba, 0, ba.Length);
}
///
/// Write Raw to the serial port.
///
/// This method writes bytes 'as is' to the serial port, which allows for writing of binary data.
///
///
/// Data to write.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Reviewed.")]
public void writeRaw(byte[] ba)
{
this.serialPort.Write(ba, 0, ba.Length);
}
///
/// Read from the serial port.
///
/// If 'true' read what's in the buffers and return right away.
/// String read from serial port.
public string read(bool noWait)
{
noWait = false;
string data = string.Empty;
try
{
if (noWait)
{
data = this.serialPort.ReadExisting();
}
else
{
// data = this.serialPort.ReadTo("\r>");
// data = this.serialPort.ReadTo("\n>");
data = this.serialPort.ReadTo(">");
}
}
catch
{
// Ignore any exception thrown.
}
return data;
}
///
/// Dispose the object.
///
/// Unused parameter.
protected virtual void Dispose(bool disposing)
{
this.close();
}
}
}