Reputation: 313
I have been testing RaspberryPi3 with Windows IoT Core to communicate with my existing FreeScale Hardware via RS485. I have been using SerialUART sample as reference. After my UWP successfully init the UART port, I don't seems to be able to receive the RS485 data transmitted by my hardware.
My hardware RS485 UART are configured at 4800baudrate, 8-bit data format, non-parit & stop-in-wait-mode disabled. I managed to successfully init 4800-8-none-one
on UWP sample but the data transmitted by the hardware does not trigger and display on Read Data
text block
.
The data transmitted from my hardware is in hex which is F5-01-55-4B
An error appear during the transmission.
The RS485 circuit is as follow.
Please advise did I miss out anything? Thanks.
Upvotes: 0
Views: 3644
Reputation: 4432
You can refer to following code. Please note that ReadString method requires a length of "code units" to read. This is the reason each string is preceded by its length when "on the wire". In your scenario, you could not make sure the data transmitted from the hardware in code unit. I'm not sure if showing the data in hex format in the TextBox is acceptable for you.
private async Task ReadAsync(CancellationToken cancellationToken)
{
Task<UInt32> loadAsyncTask;
uint ReadBufferLength = 1024;
// If task cancellation was requested, comply
cancellationToken.ThrowIfCancellationRequested();
// Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
using (var childCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken))
{
// Create a task object to wait for data on the serialPort.InputStream
loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(childCancellationTokenSource.Token);
// Launch the task and wait
UInt32 bytesRead = await loadAsyncTask;
if (bytesRead > 0)
{
//rcvdText.Text = dataReaderObject.ReadString(bytesRead);
var bufferArray = dataReaderObject.ReadBuffer(bytesRead).ToArray();
var content = string.Empty;
foreach(var b in bufferArray)
{
content += Convert.ToString(b,16).ToUpper() + " ";
}
rcvdText.Text = content;
status.Text = "bytes read successfully!";
}
}
}
Upvotes: 1