The DataReceived event never gets called in a C# .NET service running in session zero BUT does in a console app or service running in the current user session. Am I missing some permission?
/// <summary>
/// Opens the first avaliable COM port that matches the vid/pid
/// </summary>
public static bool OpenComDevice(string vid, string pid, out SerialPort serialPort)
{
serialPort = null;
// find server com port
var regPortNames = ComPortNames(vid, pid);
foreach (string name in SerialPort.GetPortNames())
{
if (regPortNames.Contains(name))
{
try
{
serialPort = new SerialPort(name);
serialPort.DtrEnable = true;
serialPort.Open();
}
catch
{
if (serialPort != null)
{
serialPort.Dispose();
serialPort = null;
}
continue;
}
break;
}
}
return serialPort != null;
}
private void Connect()
{
if (!Utils.OpenComDevice("239A", "801E", out serial)) return;
DebugLog.Log("Connected: " + serial.PortName);
serial.DataReceived += Serial_DataReceived;
serial.PinChanged += Serial_PinChanged;
serial.ErrorReceived += Serial_ErrorReceived;
isConnected = true;
ConnectedEvent?.Invoke(serial.PortName);
}
在用于Windows服务的.NET中,您需要调用Dispatcher.Run()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.