Sunday, 15 July 2012

Monitoring multiple com ports using C# -


Background: We are developing an application (or Windows service) that will listen for communication from various devices connected to virtual serial ports. The information of each device will be captured manually, so I know that the device will be on 1Com 5, the device is 2Com 6, etc. Any time a device transmits data, I have to capture it and write it elsewhere (originally in the log file).

With this in mind, is it possible to monitor more than one serial port on a thread, or I should create a new bag for each port (keep in mind that I know which Ports are monitored)

Or do you guys think that's a good way?

The prototype code I'm working on looks like this (only reads a port):

  class program {public static zero main () {SerialPort mySerialPort = New SerialPort ("COM5"); MySerialPort.BaudRate = 9600; MySerialPort.Parity = Equality One; MySerialPort.StopBits = StopBits.One; MySerialPort.DataBits = 8; Messierport Airport Handshake = Handshake.No; MySerialPort.DataReceived + = New SerialDataReceivedEventHandler (DataReceivedHandler); MySerialPort.Open (); Console.light line ("press any key to continue ..."); Console.WriteLine (); Console.ReadKey (); MySerialPort.Close (); } Private static blank Deseecewandler (Object Sender, Serial Data RecivilEventErgus E) {Serialport SP = (Serial Port) Sender; String indenta = sp.ReadExisting (); Console.WriteLine (indata); Debug Print ("Received Data:"); Debug.Print (indata); }}  

I am basically thinking that I should create a class for each port code in the console application, or if I should go to old school and write a huge procedural app Which once monitors them all.

If the COM port devices are all the same, handle each class with an instance for each device Recovering the data is the way to go if you need different functionality, you can always get it from the base class. If you slogan 'Write a huge procedural app', then in the future it will be more difficult to maintain and modify.

For many sources, the events that are using the handler, many ports can be read without affecting other functions in their programs, they can be considered as their thread, the action between them Should be handled as cross-thread operating (such as changing form properties).


No comments:

Post a Comment