有沒有人知道一個好的(希望免費)類來取代.net SerialPort類?它引起我的問題,我需要一個稍微靈活一點的問題。.net框架的良好串行端口類?
See my other thread about my issues,但基本上我需要禁止在打開端口時發出IOCTL_SERIAL_SET_DTR和IOCTL_SERIAL_CLR_DTR命令,所以我需要比.net框架提供的類更靈活。
有沒有人知道一個好的(希望免費)類來取代.net SerialPort類?它引起我的問題,我需要一個稍微靈活一點的問題。.net框架的良好串行端口類?
See my other thread about my issues,但基本上我需要禁止在打開端口時發出IOCTL_SERIAL_SET_DTR和IOCTL_SERIAL_CLR_DTR命令,所以我需要比.net框架提供的類更靈活。
幾年前,我在將串行支持添加到.net之前使用了OpenNETCF.IO.Serial。這是爲了緊湊的框架,但我使用它的小型設備和普通的Windows應用程序。你得到源代碼,所以你可以自己修改它,這是我做的。
它基本上創建一個c#包裝器,圍繞從kernel32.dll導出的串行函數。
你可能也想看看How to capture a serial port that disappears because the usb cable gets unplugged
這裏是我用來調用它的代碼
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不是密封類 - 任何機會,你可以讓你從一個子類需要的行爲?
不,但是SerialPort使用SerialStream類來完成實際的工作,這是內部的和密封的,而UnsafeNativeMethods類是內部的。 – Jeremy 2009-02-02 17:12:16
謝謝。我已經檢查過這一個。我不喜歡使用另一種軟件包,因爲我不想保留它,但看起來像我一直被迫。雖然我已經做了一些小改動來增強錯誤報告,但這個軟件包還是不錯的。謝謝。 – Jeremy 2009-02-02 23:00:34