initial commit
This commit is contained in:
70
Infrastructure/Kwp/KLineInterface.cs
Normal file
70
Infrastructure/Kwp/KLineInterface.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.IO.Ports;
|
||||
|
||||
namespace HC_APTBS.Infrastructure.Kwp
|
||||
{
|
||||
class KLineInterface : IInterface
|
||||
{
|
||||
public KLineInterface(string portName, int baudRate)
|
||||
{
|
||||
_port = new SerialPort(portName)
|
||||
{
|
||||
BaudRate = baudRate,
|
||||
DataBits = 8,
|
||||
Parity = Parity.None,
|
||||
StopBits = StopBits.One,
|
||||
Handshake = Handshake.None,
|
||||
RtsEnable = false,
|
||||
DtrEnable = true,
|
||||
ReadTimeout = 1000,
|
||||
WriteTimeout = 500
|
||||
};
|
||||
|
||||
_port.Open();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_port.Close();
|
||||
}
|
||||
|
||||
public byte ReadByte()
|
||||
{
|
||||
try
|
||||
{
|
||||
var b = (byte)_port.ReadByte();
|
||||
return b;
|
||||
}
|
||||
catch (TimeoutException ex)
|
||||
{
|
||||
throw new TimeoutException("Read timed out");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void WriteByteRaw(byte b)
|
||||
{
|
||||
_buf[0] = b;
|
||||
_port.Write(_buf, 0, 1);
|
||||
}
|
||||
|
||||
public void SetBreakOn()
|
||||
{
|
||||
_port.BreakState = true;
|
||||
}
|
||||
|
||||
public void SetBreakOff()
|
||||
{
|
||||
_port.BreakState = false;
|
||||
}
|
||||
|
||||
public void ClearReceiveBuffer()
|
||||
{
|
||||
_port.DiscardInBuffer();
|
||||
}
|
||||
|
||||
private readonly SerialPort _port;
|
||||
|
||||
private readonly byte[] _buf = new byte[1];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user