Introduction
This C# console program illustrates how to use serial port hooks in C#, in a sense. This is because there’s really no readily available API which provides hooking the serial port unlike windows messages, keyboard, and mouse, etc, in which you can use SetWindowsHookEx
API.
Background
So you popped up your favorite inet browser, opened your favorite search engine and type “How do I make hook on serial port in C#?”.
Then your search engine begins to search the vast WWW. Voila!
Unless I’m wrong as I may not have searched the inet long enough to dig one, your search results are not exactly the one’s you’re expecting.
So here’s C# console program that at least illustrates the concept in a very simple example. Firstly, we create a WIN32 serial library (see documentation WIN32 Serial Module.chm ) which provides serial hook interfaces. The serial library takes advantage of the overlap option providing asynchronous I/O thus making the hooks a possibility. Then we import the serial library methods in C#. That’s it.
Using the code
If your interest is to be able to use hardware flow control, this is not the right post for you as the serial library does not provide that functionality in this example. However, with a little patience for a small work, you can extend the existing library to do the job.
So this is an example that provides transparent data transfer from a terminal to the application layer and vice-versa. Yet this does not obstruct you to implement your own messaging protocol (i.e. at its simplest STX + <Data> +STX + LRC).
Just like any other non-static classes, you just need to create an instance of class CommsSerial
to use it on your program as shown below:
Note that this console program requires the serial port number to be passed as a program argument.
namespace UsingWIN32SerialHooksInCSharp
{
class SerialProgram
{
private CommsSerial comSerial;
private void SerialSimulation(ushort port)
{
try
{
comSerial = new CommsSerial();
comSerial.SerialReceptionEvent +=
delegate(object obj, ComEventArgs comEventArgs)
{
Console.WriteLine("\n---Data available notification----");
Console.WriteLine("{0} byte(s) successfully read.",
comEventArgs.Length);
Console.WriteLine("Data: {0}", comEventArgs.ToString());
byte[] dataToWrite = new byte[] { 0x02, (byte)'T',
(byte)'E', (byte)'S', (byte)'T', 0x03 };
Console.WriteLine("\nWriting {0} byte(s) of data. \nData: {1}",
dataToWrite.Length,
Conversion.BytesToAsciiHex
(dataToWrite,dataToWrite.Length)
);
comSerial.Write(dataToWrite, (uint)dataToWrite.Length );
};
comSerial.SerialTransmitEvent +=
delegate(object obj, ComEventArgs comEventArgs)
{
Console.WriteLine("\n---Write finished notification----");
Console.WriteLine("{0} byte(s) successfully written.",
comEventArgs.Length);
};
try
{
Console.Write("\nOpening serial COM port...");
comSerial.Open(port,
CommsSerial.DataSize.EIGHT_BITS,
CommsSerial.ParityBit.NOPARITY,
CommsSerial.StopBit.ONESTOPBIT,
CommsSerial.BaudRate.CBR_115200
);
Console.WriteLine("OK!");
Console.Write("\nInstalling hooks...");
comSerial.InstallHooks();
Console.WriteLine("OK!");
do
{
Console.WriteLine("\nSerial monitor listening...\n");
Thread.Sleep(20000);
Console.WriteLine("Closing serial monitor...");
} while (false);
comSerial.UninstallHooks();
comSerial.Close();
Console.WriteLine("Monitor closed.");
}
catch (SerialManagedException)
{
throw;
}
catch (Exception)
{
throw;
}
}
catch (SerialManagedException ex)
{
Console.WriteLine("A serial exception occurred.");
Console.WriteLine(ex.Message);
}
catch (ArgumentException ex)
{
Console.WriteLine("An argument exception occurred.");
Console.WriteLine(ex.Message);
}
catch(Exception ex)
{
Console.WriteLine("Unknown exception occurred.");
Console.WriteLine(ex.Message);
}
}
static void Main(string[] args)
{
ushort port;
if (args.Length==0)
{
Console.WriteLine("\nError: No program argument provided!");
Console.WriteLine("\nUsage: {0} [serial port]",
Path.GetFileName
(Process.GetCurrentProcess().MainModule.FileName)
);
Console.WriteLine("\n");
Console.ReadKey();
return;
}
port = Convert.ToUInt16(args[0]);
if ( port == 0 || port > 15)
{
port = 1;
}
SerialProgram serialProgram = new SerialProgram();
serialProgram.SerialSimulation(port);
Console.WriteLine("\nSimulation finished.");
Console.WriteLine("<press>");
Console.ReadKey();
}
}
}
History