Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Copyright (c) Microsoft. All rights reserved.
- using System;
- using System.Collections.ObjectModel;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using Windows.Devices.Enumeration;
- using Windows.Devices.SerialCommunication;
- using Windows.Storage.Streams;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Collections.Specialized;
- using System.Net;
- namespace SerialSample
- {
- public sealed partial class MainPage : Page
- {
- private SerialDevice serialPort = null;
- DataWriter dataWriteObject = null;
- DataReader dataReaderObject = null;
- private ObservableCollection<DeviceInformation> listOfDevices;
- private CancellationTokenSource ReadCancellationTokenSource;
- public MainPage()
- {
- this.InitializeComponent();
- comPortInput.IsEnabled = false;
- sendTextButton.IsEnabled = false;
- listOfDevices = new ObservableCollection<DeviceInformation>();
- ListAvailablePorts();
- }
- private async void ListAvailablePorts()
- {
- try
- {
- string aqs = SerialDevice.GetDeviceSelector();
- var dis = await DeviceInformation.FindAllAsync(aqs);
- status.Text = "Select a device and connect";
- for (int i = 0; i < dis.Count; i++)
- {
- listOfDevices.Add(dis[i]);
- }
- DeviceListSource.Source = listOfDevices;
- comPortInput.IsEnabled = true;
- ConnectDevices.SelectedIndex = -1;
- }
- catch (Exception ex)
- {
- status.Text = ex.Message;
- }
- }
- private async void comPortInput_Click(object sender, RoutedEventArgs e)
- {
- var selection = ConnectDevices.SelectedItems;
- if (selection.Count <= 0)
- {
- status.Text = "Select a device and connect";
- return;
- }
- DeviceInformation entry = (DeviceInformation)selection[0];
- try
- {
- serialPort = await SerialDevice.FromIdAsync(entry.Id);
- // Disable the 'Connect' button
- comPortInput.IsEnabled = false;
- // Configure serial settings
- serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
- serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
- serialPort.BaudRate = 9600;
- serialPort.Parity = SerialParity.None;
- serialPort.StopBits = SerialStopBitCount.One;
- serialPort.DataBits = 8;
- // Display configured settings
- status.Text = "Serial port configured successfully!\n ----- Properties ----- \n";
- status.Text += "BaudRate: " + serialPort.BaudRate.ToString() + "\n";
- status.Text += "DataBits: " + serialPort.DataBits.ToString() + "\n";
- status.Text += "Handshake: " + serialPort.Handshake.ToString() + "\n";
- status.Text += "Parity: " + serialPort.Parity.ToString() + "\n";
- status.Text += "StopBits: " + serialPort.StopBits.ToString() + "\n";
- // Set the RcvdText field to invoke the TextChanged callback
- // The callback launches an async Read task to wait for data
- rcvdText.Text = "Waiting for data...";
- // Create cancellation token object to close I/O operations when closing the device
- ReadCancellationTokenSource = new CancellationTokenSource();
- // Enable 'WRITE' button to allow sending data
- sendTextButton.IsEnabled = true;
- }
- catch (Exception ex)
- {
- status.Text = ex.Message;
- comPortInput.IsEnabled = true;
- sendTextButton.IsEnabled = false;
- }
- }
- private async void sendTextButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- if (serialPort != null)
- {
- // Create the DataWriter object and attach to OutputStream
- dataWriteObject = new DataWriter(serialPort.OutputStream);
- //Launch the WriteAsync task to perform the write
- await WriteAsync();
- }
- else
- {
- status.Text = "Select a device and connect";
- }
- }
- catch (Exception ex)
- {
- status.Text = "sendTextButton_Click: " + ex.Message;
- }
- finally
- {
- // Cleanup once complete
- if (dataWriteObject != null)
- {
- dataWriteObject.DetachStream();
- dataWriteObject = null;
- }
- }
- }
- private async Task WriteAsync()
- {
- Task<UInt32> storeAsyncTask;
- if (sendText.Text.Length != 0)
- {
- // Load the text from the sendText input text box to the dataWriter object
- dataWriteObject.WriteString(sendText.Text);
- // Launch an async task to complete the write operation
- storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
- UInt32 bytesWritten = await storeAsyncTask;
- if (bytesWritten > 0)
- {
- status.Text = sendText.Text + '\n';
- status.Text += "Bytes written successfully!";
- }
- sendText.Text = "";
- }
- else
- {
- status.Text = "Enter the text you want to write and then click on 'WRITE'";
- }
- }
- private async void rcvdText_TextChanged(object sender, TextChangedEventArgs e)
- {
- try
- {
- if (serialPort != null)
- {
- dataReaderObject = new DataReader(serialPort.InputStream);
- await ReadAsync(ReadCancellationTokenSource.Token);
- }
- }
- catch (Exception ex)
- {
- if (ex.GetType().Name == "TaskCanceledException")
- {
- status.Text = "Reading task was cancelled, closing device and cleaning up";
- CloseDevice();
- }
- else
- {
- status.Text = ex.Message;
- }
- }
- finally
- {
- // Cleanup once complete
- if (dataReaderObject != null)
- {
- dataReaderObject.DetachStream();
- dataReaderObject = null;
- }
- }
- }
- 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;
- // Create a task object to wait for data on the serialPort.InputStream
- loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
- // Launch the task and wait
- UInt32 bytesRead = await loadAsyncTask;
- if (bytesRead > 0)
- {
- //Send data to send via HTTP protocal
- string UID = dataReaderObject.ReadString(bytesRead);
- rcvdText.Text = UID;
- status.Text = "\nBytes read successfully!";
- using (var wb = new WebClient())
- {
- var data = new NameValueCollection();
- data["field1"] = UID;
- var response = wb.UploadValues("http://127.0.0.1/myprocessingscript.php", "POST", data);
- }
- }
- }
- private void CancelReadTask()
- {
- if (ReadCancellationTokenSource != null)
- {
- if (!ReadCancellationTokenSource.IsCancellationRequested)
- {
- ReadCancellationTokenSource.Cancel();
- }
- }
- }
- private void CloseDevice()
- {
- if (serialPort != null)
- {
- serialPort.Dispose();
- }
- serialPort = null;
- comPortInput.IsEnabled = true;
- sendTextButton.IsEnabled = false;
- rcvdText.Text = "";
- listOfDevices.Clear();
- }
- private void closeDevice_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- status.Text = "";
- CancelReadTask();
- CloseDevice();
- ListAvailablePorts();
- }
- catch (Exception ex)
- {
- status.Text = ex.Message;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement