string aqs = SerialDevice.GetDeviceSelector("UART0");
var devices = await DeviceInformation.FindAllAsync(aqs);
var dis = devices.FirstOrDefault();
Serial = await SerialDevice.FromIdAsync(dis.Id);
// Configure serial settings
Serial.WriteTimeout = TimeSpan.FromMilliseconds(1000);
Serial.ReadTimeout = TimeSpan.FromMilliseconds(1000);
Serial.BaudRate = 9600;
Serial.Parity = SerialParity.None;
Serial.StopBits = SerialStopBitCount.One;
Serial.DataBits = 8;
Serial.Handshake = SerialHandshake.None;
Serial.ErrorReceived += Serial_ErrorReceived;
DataReader reader = new DataReader(Serial.InputStream);
while (true)
{
try
{
reader.InputStreamOptions = InputStreamOptions.Partial;
uint bytesRead = await reader.LoadAsync(4096).AsTask();
if (bytesRead > 0)
{
string text = reader.ReadString(bytesRead);
Debug.WriteLine(text);
}
}
catch (Exception)
{
}
}
/////////////////////////////////////////////////////////////////////////////////
Now the writing part :
async Task WriteToSerial(string data)
{
// Create the DataWriter object and attach to OutputStream
DataWriter dataWriteObject = new DataWriter(Serial.OutputStream);
try
{
//Serial.OutputStream.AsStreamForWrite().Write(Encoding.ASCII.GetBytes(data), 0, Encoding.ASCII.GetBytes(data).Length);
// Load the text from the sendText input text box to the dataWriter object
dataWriteObject.WriteString(data);
uint bytesWritten = await dataWriteObject.StoreAsync();
if (bytesWritten > 0)
{
Debug.WriteLine(bytesWritten + " bytes written success");
}
}
finally
{
dataWriteObject.DetachStream();
}
}