Weiterführende Infos zum Thema Serial Port: Hanselman und MSDN
using System.IO.Ports;
public class CRS232
{
private SerialPort _sp = new SerialPort();
public CRS232(string sPortName, bool bRTSEnable, int iBaudRate, int iDataBits, int iReadTimeOut, int iWriteTimeOut, StopBits eStopBits, Parity eParity, Handshake eHandShake, Encoding eEncoding)
{
_sp.PortName = sPortName;
_sp.RtsEnable = bRTSEnable;
_sp.BaudRate = iBaudRate;
_sp.DataBits = iDataBits;
_sp.ReadTimeout = iReadTimeOut;
_sp.WriteTimeout = iWriteTimeOut;
// StopBits.None;
// StopBits.One;
// StopBits.OnePointFive;
// StopBits.Two;
_sp.StopBits = eStopBits;
// Parity.Even;
// Parity.Odd;
// Parity.None;
// Parity.Space;
// Parity.Mark;
_sp.Parity = eParity;
// Handshake.XOnXOff;
// Handshake.None;
_sp.Handshake = eHandShake;
_sp.Encoding = eEncoding;
}
public string[] GetPorts()
{
return SerialPort.GetPortNames();
}
public bool Open()
{
bool bRetVal = false;
try
{
if (!_sp.IsOpen)
{
_sp.Open();
}
bRetVal = true;
}
catch (Exception)
{
}
return bRetVal;
}
public bool Close()
{
bool bRetVal = false;
try
{
if (!_sp.IsOpen)
{
_sp.Close();
}
bRetVal = true;
}
catch (Exception)
{
}
return bRetVal;
}
public string ReadLine()
{
string sRetVal = string.Empty;
try
{
if (_sp.IsOpen)
{
sRetVal = _sp.ReadLine();
}
}
catch (Exception)
{
}
return sRetVal;
}
public bool WriteLine(string sText)
{
bool bRetVal = false;
try
{
if (_sp.IsOpen)
{
_sp.WriteLine(sText);
}
}
catch (Exception)
{
}
return bRetVal;
}
}