Question

Does anybody know of a good (hopefully free) class to replace the .net SerialPort class? It is causing me issues and I need one that is slightly more flexible.

See my other thread about my issues, but essentially I need to suppress the IOCTL_SERIAL_SET_DTR and IOCTL_SERIAL_CLR_DTR commands from being issued when I open the port, so I need something more flexible than the class provided by the .net framework.

Was it helpful?

Solution

Several years ago I used OpenNETCF.IO.Serial before serial support was added to .net. It is for the compact framework but I used it for both compact devices and regular windows apps. You get the source code so you can modify it yourself which is what I did.

It basically creates a c# wrapper around the serial function imported out of kernel32.dll.

You might also want to have a look at How to capture a serial port that disappears because the usb cable gets unplugged

Here is the code that I used to call it

     using OpenNETCF.IO.Serial;

     public static Port port;
     private DetailedPortSettings portSettings;
     private Mutex UpdateBusy = new Mutex();

     // create the port
     try
     {
        // create the port settings
        portSettings = new HandshakeNone();
        portSettings.BasicSettings.BaudRate=BaudRates.CBR_9600;

        // create a default port on COM3 with no handshaking
        port = new Port("COM3:", portSettings);

        // define an event handler
        port.DataReceived +=new Port.CommEvent(port_DataReceived);

        port.RThreshold = 1;    
        port.InputLen = 0;      
        port.SThreshold = 1;    
        try
        {
           port.Open();
        }
        catch
        {
           port.Close();
        }
     }
     catch
     {
        port.Close();
     }

     private void port_DataReceived()
     {

        // since RThreshold = 1, we get an event for every character
        byte[] inputData = port.Input;

        // do something with the data
        // note that this is called from a read thread so you should 
        // protect any data pass from here to the main thread using mutex
        // don't forget the use the mutex in the main thread as well
        UpdateBusy.WaitOne();
        // copy data to another data structure
        UpdateBusy.ReleaseMutex();

     }

     private void port_SendBuff()
     {
        byte[] outputData = new byte[esize];
        crc=0xffff;
        j=0;
        outputData[j++]=FS;
        //  .. more code to fill up buff
        outputData[j++]=FS;
        // number of chars sent is determined by size of outputData
        port.Output = outputData;
     }

     // code to close port
     if (port.IsOpen)
     {
        port.Close();
     }
     port.Dispose();

OTHER TIPS

I haven't tried tried this but, perhaps you can:

  • Get the source code from System.IO.Ports.SerialPort by using Reflector or similar
  • Change that source code as you like
  • Rebuilt the modified copy in a different namespace, and use it

The standard .NET SerialPort isn't a sealed class - any chance you can get the behaviour you need from a subclass?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top