System.IO.Ports不可用.net 1.1
答案 0 :(得分:2)
当我需要在1.1中进行一些串口工作时,我发现Noah Coad写的一篇文章使用了MSComm OCX控件,最终为我工作。你可以在http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=320找到他的文章。祝你好运!
答案 1 :(得分:1)
对于.net 1.1,我使用了OpenNETCF.IO.Serial,因为版本2.0中的.net中添加了串行支持。它适用于紧凑的框架,但我将它用于紧凑型设备和常规Windows应用程序。你得到了源代码,所以你可以自己修改它,这就是我所做的。
它基本上是在从kernel32.dll导出的串口函数周围创建一个c#包装器。
您可能还想查看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();
答案 2 :(得分:0)
您是否受限于.NET 1.1?如果您可以使用.NET 2.0,则可以使用SerialPort.GetPortNames方法检索本地主机上的串行端口列表。 SerialPort类位于System.IO.Ports命名空间中。