I am trying to convert my WPF app to UWP app. My app uses the Serial Port class to communicate with a device. After I switched to UWP the same code that opens the connection to the device, using the Serial Port class, stopped working.
public MotorControl(string portName, Settings settings)
{
this.settings = settings;
this.serialPort = new SerialPort(portName, 9600)
{
DtrEnable = true,
RtsEnable = true
};
this.serialPort.ErrorReceived += Port_ErrorReceived;
this.serialPort.DataReceived += Port_DataReceived;
}
public void Connect()
{
this.serialPort.Open();
Thread.Sleep(this.settings.delayBetweenConnectAntWifi);
this.SetNetworkMode("wifi");
}
When I am trying to open the connection I am getting this error:
System.IO.IOException
HResult=0x80070000
Message=The operation completed successfully
Source=System.IO.Ports
StackTrace:
at System.IO.Ports.InternalResources.WinIOError(Int32 errorCode, String str)
at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)
I tried to use Serial Device
public MotorControl(string portName, Settings settings)
{
this.settings = settings;
this.portName = portName;
}
private async Task SetupSerialDevice()
{
string aqs = SerialDevice.GetDeviceSelector(this.portName);
var myDevices = await DeviceInformation.FindAllAsync(aqs, null);
if (myDevices.Count == 0)
{
return;
}
this.serialDevice = await SerialDevice.FromIdAsync(myDevices.First().Id);
this.serialDevice.BaudRate = 9600;
this.serialDevice.DataBits = 8;
this.serialDevice.StopBits = SerialStopBitCount.One;
this.serialDevice.Parity = SerialParity.None;
this.serialDevice.Handshake = SerialHandshake.None;
this.serialDevice.ReadTimeout = TimeSpan.FromMilliseconds(1000);
this.serialDevice.WriteTimeout = TimeSpan.FromMilliseconds(1000);
this.dataWriter= new DataWriter(this.serialDevice.OutputStream);
this.dataReader= new DataReader(this.serialDevice.InputStream);
this.readPortThread = new Thread(this.ReadPort);
this.readPortThread.Start();
}
public async void Connect()
{
await this.SetupSerialDevice();
Thread.Sleep(this.settings.delayBetweenConnectAntWifi);
this.SetNetworkMode("wifi");
}
But, SerialDevice.FromIdAsync keeps returning null.
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:genTemplate="http://schemas.microsoft.com/appx/developer/windowsTemplateStudio"
xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp genTemplate iot rescap">
.
.
.
<Capabilities>
<rescap:Capability Name="confirmAppClose"/>
<Capability Name="internetClient" />
<iot:Capability Name="lowLevelDevices"/>
<DeviceCapability Name="serialCommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
</Capabilities>
The problem was that before I tried to connect to the device I used this function to find all ports:
public async static Task<ObservableCollection<string>> GetPortNames()
{
string aqs = SerialDevice.GetDeviceSelector();
var deviceCollection = await DeviceInformation.FindAllAsync(aqs);
ObservableCollection<string> portNamesList = new ObservableCollection<string>();
foreach (var item in deviceCollection)
{
SerialDevice serialDevice = await SerialDevice.FromIdAsync(item.Id);
string portName = serialDevice.PortName;
portNamesList.Add(portName);
}
return portNamesList;
}
and i didn't do a serialDevice.Dispose();
at the end of the foreach
.
After adding serialDevice.Dispose();
I was able to use the good old SerialPort
.
User contributions licensed under CC BY-SA 3.0