.net Framework に適したシリアル ポート クラスはどれですか?

StackOverflow https://stackoverflow.com/questions/500228

  •  20-08-2019
  •  | 
  •  

質問

.net SerialPort クラスを置き換える良い (できれば無料の) クラスを知っている人はいますか?それは私に問題を引き起こしているので、もう少し柔軟なものが必要です。

私の問題については他のスレッドを参照してください, しかし本質的には、ポートを開くときに IOCTL_SERIAL_SET_DTR および IOCTL_SERIAL_CLR_DTR コマンドが発行されないようにする必要があるため、.net Framework によって提供されるクラスよりも柔軟なものが必要です。

役に立ちましたか?

解決

数年前、私が使用 OpenNETCF.IO.Serial シリアルサポートが.NETに追加される前に。これは、コンパクトなフレームワークですが、私は、コンパクトなデバイスと通常のWindowsアプリケーションの両方のためにそれを使用しました。あなたはそれを自分で変更することができますので、あなたは私が何をしたかであるソースコードを取得します。

これは、基本的にkernel32.dllの外の輸入シリアル機能を中心にC#のラッパーを作成します。

また、USBケーブルを抜いを見を持っている場合がありますP>

ここで私はそれを呼び出すために使用されるコードです。

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

他のヒント

私はこれを試したことはありませんが、おそらく次のことが可能です。

  • からソースコードを取得します System.IO.Ports.SerialPort を使用して リフレクター または類似
  • ソースコードを好きなように変更してください
  • 変更したコピーを別の名前空間で再構築し、それを使用します

標準.NET SERIALPORTはシールクラスではありません - あなたは、サブクラスからあなたが必要とする動作を得ることができ、万が一

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top