Write commands to serial port from c# application

In one of my projects I needed to use a DEVICE which used serial port to communicate with any other application. Hence from my C# application I had to somehow send commands to that device in order to operate it.
Now that device used to run on COM Port i.e. Serial Port. Serial port understand commands in ascii format or byte arrays.
This particular device used to connect to serial port through ASCII Commands. Lets see how we can achieve this.

In order to communicate to serial port, first I need to open that designated port with right settings i.e. Baud Rate, Parity etc. for that I used

following code.
public class ComPort
{
public SerialPort SerialPort = new SerialPort();
// Initialize the build serial class instance
//Open up the designated serial port
public int ComOpen(string portName, int baudRate, int parity, int handshaking, bool rts, bool dtr)
{
try
{
const int dataBits = 8;
CloseCom();
SerialPort = new SerialPort(portName, baudRate, (Parity) parity, dataBits)
{
ReceivedBytesThreshold = 5,
Handshake = (Handshake) handshaking,
ReadTimeout = 500,
WriteTimeout = 200,
RtsEnable = rts,
DtrEnable = dtr
};
SerialPort.Open();
}
catch (InvalidOperationException)
{
return (int) ExceptionTypes.OpenPorts; // Ports are open
}
catch (ArgumentOutOfRangeException)
{
return (int) ExceptionTypes.InvalidAttribute; // One or more attributes are invalid
}
catch (ArgumentException)
{
return (int) ExceptionTypes.PortNameNotBeginsWithCom; // Port name doesn’t begins with “COM”
}
catch (IOException)
{
return (int) ExceptionTypes.PortInInvalidState; // This port is in an invalid state
}
catch (UnauthorizedAccessException ex)
{
return (int) ExceptionTypes.AccessDeniedToPort; // Access to the port is denied
}
return ZeroValue;
}
/// <summary>
/// Send the user’s data currently entered in the ‘Command’ box.
/// This method converts text commands to ASCII Format in order to send to serial port
///</summary>
private void SendData(string commandText)
{
var bytes = Encoding.ASCII.GetBytes(commandText+”\r”);
SerialPort.Write(bytes, 0, bytes.Length);
}
/// <summary>
/// Close the serial Port if opened
/// </summary>
/// <returns>0 if success else exception number</returns>
public int CloseCom()
{
try
{
SerialPort.Dispose();
SerialPort.Close();
}
catch (InvalidOperationException)
{
return (int) ExceptionTypes.PortNotOpened; // The specified port is not open
}
return ZeroValue;
}
}
Constructor of this class opens up the serial port.
SendData() sends the command which is entered in textbox after changing it to ascii format to serial port.
CloseCom() close the serial port which was opened in constructor.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s