Für .net 1.1 habe ich OpenNETCF.IO.Serial verwendet, weil serielle Unterstützung zu .net in Version 2.0 hinzugefügt wurde. Es ist für das kompakte Framework, aber ich habe es sowohl für kompakte Geräte und regelmäßige Windows-Apps verwendet. Sie erhalten den Quellcode, damit Sie ihn selbst modifizieren können, was ich getan habe.
Es erstellt im Grunde ein C# -Wrapper rund um die serielle Funktion aus Kernel32.dll importiert.
Sie könnten auch einen Blick haben wollen bei How to capture a serial port that disappears because the usb cable gets unplugged
Hier ist der Code, den ich zu nennen pflegte es
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();
-1 für Caps und nicht die Zeit, um Ihre Frage in kohärenten Sätzen richtig zu formatieren. Wenn Sie Hilfe brauchen, haben Sie den Anstand, Ihre Frage wie ein menschliches Wesen zu formulieren. – mpen
Es ist nicht fair zu down abstimmen, wenn Formatierung der Grund ist. – rahul
Englisch könnte nicht seine Muttersprache – eKek0