Domanda

Qualcuno conosce una buona classe (si spera gratuita) per sostituire la classe .net SerialPort? Mi sta causando problemi e ne ho bisogno di uno leggermente più flessibile.

Vedi le altre mie discussioni sui miei problemi , ma essenzialmente devo sopprimere IOCTL_SERIAL_SET_DTR e I comandi IOCTL_SERIAL_CLR_DTR vengono emessi quando apro la porta, quindi ho bisogno di qualcosa di più flessibile della classe fornita dal framework .net.

È stato utile?

Soluzione

Diversi anni fa ho usato OpenNETCF.IO.Serial prima che il supporto seriale fosse aggiunto a .net. È per il framework compatto ma l'ho usato sia per i dispositivi compatti che per le normali app di Windows. Ottieni il codice sorgente in modo da poterlo modificare tu stesso che è quello che ho fatto.

Fondamentalmente crea un wrapper c # attorno alla funzione seriale importata da kernel32.dll.

Potresti anche dare un'occhiata a Come catturare una porta seriale che scompare perché il cavo USB viene scollegato

Ecco il codice che ho usato per chiamarlo

     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();

Altri suggerimenti

Non ho provato questo, ma forse puoi:

  • Ottieni il codice sorgente da System.IO.Ports.SerialPort utilizzando Reflector o simile
  • Cambia quel codice sorgente come preferisci
  • Ricostruisci la copia modificata in un altro spazio dei nomi e usala

Il SerialPort .NET standard non è una classe sigillata: hai qualche possibilità di ottenere il comportamento di cui hai bisogno da una sottoclasse?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top