2

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

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

4

3 に答える 3

4

数年前、シリアルサポートが.netに追加される前に、 OpenNETCF.IO.Serialを使用しました。コンパクトフレームワーク用ですが、コンパクトデバイスと通常のWindowsアプリの両方に使用しました。ソースコードを入手するので、自分で変更することができます。これは私がやったことです。

基本的に、kernel32.dllからインポートされたシリアル関数の周りにac#ラッパーを作成します。

USBケーブルが外れたために消えたシリアルポートをキャプチャする方法もご覧ください。

これが私がそれを呼んでいたコードです

     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();
于 2009-02-01T05:10:21.547 に答える
1

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

  • ReflectorなどSystem.IO.Ports.SerialPortを使用してソースコードを取得する
  • そのソースコードを好きなように変更してください
  • 変更したコピーを別の名前空間で再構築し、それを使用します
于 2009-02-01T05:22:28.307 に答える
0

標準の .NET SerialPort は封印されたクラスではありません。必要な動作をサブクラスから取得できる可能性はありますか?

于 2009-02-01T09:29:45.423 に答える