Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Windows.UI.Xaml;
- using Windows.UI.Xaml.Controls;
- using System.Threading;
- using Windows.Devices.SerialCommunication;
- using System.Collections.ObjectModel;
- using Windows.Storage.Streams;
- using Windows.Devices.Enumeration;
- using System.Threading.Tasks;
- namespace Serial_Communication_Universal
- {
- 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();
- sendTextBox.IsEnabled = false;
- sendButton.IsEnabled = false;
- reciveTextBox.IsEnabled = false;
- disconnectButton.IsEnabled = false;
- listOfDevices = new ObservableCollection<DeviceInformation>();
- getAvailPorts();
- }
- private async void getAvailPorts()
- {
- string aqs = SerialDevice.GetDeviceSelector();
- var dis = await DeviceInformation.FindAllAsync(aqs);
- for (int i = 0; i < dis.Count; i++)
- {
- listOfDevices.Add(dis[i]);
- }
- DeviceListSource.Source = listOfDevices;
- baudListBox.SelectedIndex = -1;
- }
- private async void connectButton_Click(object sender, RoutedEventArgs e)
- {
- var selection = baudListBox.SelectedItem;
- DeviceInformation entry = (DeviceInformation)selection;
- try
- {
- serialPort = await SerialDevice.FromIdAsync(entry.Id);
- connectButton.IsEnabled = false;
- serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
- serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
- serialPort.BaudRate = 9600;
- serialPort.Parity = SerialParity.None;
- serialPort.StopBits = SerialStopBitCount.One;
- serialPort.DataBits = 8;
- serialPort.Handshake = SerialHandshake.None;
- ReadCancellationTokenSource = new CancellationTokenSource();
- sendButton.IsEnabled = true;
- sendTextBox.IsEnabled = true;
- sendButton.IsEnabled = true;
- reciveTextBox.IsEnabled = true;
- disconnectButton.IsEnabled = true;
- Listen();
- }
- catch (Exception ex)
- {
- connectButton.IsEnabled = true;
- sendButton.IsEnabled = false;
- }
- }
- private async void Listen()
- {
- try
- {
- if (serialPort != null)
- {
- dataReaderObject = new DataReader(serialPort.InputStream);
- while (true)
- {
- await ReadAsync(ReadCancellationTokenSource.Token);
- }
- }
- }
- catch (Exception ex)
- {
- if (ex.GetType().Name == "TaskCanceledException")
- {
- CloseDevice();
- }
- }
- finally
- {
- if (dataReaderObject != null)
- {
- dataReaderObject.DetachStream();
- dataReaderObject = null;
- }
- }
- }
- private async Task ReadAsync(CancellationToken cancellationToken)
- {
- Task<UInt32> loadAsyncTask;
- uint ReadBufferLength = 1024;
- cancellationToken.ThrowIfCancellationRequested();
- dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
- loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);
- UInt32 bytesRead = await loadAsyncTask;
- if (bytesRead > 0)
- {
- reciveTextBox.Text = dataReaderObject.ReadString(bytesRead);
- }
- }
- private void CloseDevice()
- {
- if (serialPort != null)
- {
- serialPort.Dispose();
- }
- serialPort = null;
- connectButton.IsEnabled = true;
- sendButton.IsEnabled = false;
- reciveTextBox.Text = "";
- listOfDevices.Clear();
- }
- private async void sendButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- dataWriteObject = new DataWriter(serialPort.OutputStream);
- await WriteAsync();
- }
- catch (Exception ex)
- {
- }
- finally
- {
- if (dataWriteObject != null)
- {
- dataWriteObject.DetachStream();
- dataWriteObject = null;
- }
- }
- }
- private async Task WriteAsync()
- {
- Task<UInt32> storeAsyncTask;
- if (sendTextBox.Text.Length != 0)
- {
- dataWriteObject.WriteString(sendTextBox.Text);
- storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
- UInt32 bytesWritten = await storeAsyncTask;
- if (bytesWritten > 0)
- {
- }
- sendTextBox.Text = "";
- }
- else
- {
- }
- }
- private void disconnectButton_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- CancelReadTask();
- CloseDevice();
- getAvailPorts();
- }
- catch (Exception ex)
- {
- }
- }
- private void CancelReadTask()
- {
- if (ReadCancellationTokenSource != null)
- {
- if (!ReadCancellationTokenSource.IsCancellationRequested)
- {
- ReadCancellationTokenSource.Cancel();
- }
- }
- }
- private void reciveTextBox_TextChanged(object sender, TextChangedEventArgs e)
- {
- if(scrollCheckBox.IsChecked == true)
- {
- reciveTextBox.SelectionStart = reciveTextBox.Text.Length;
- }
- }
- private void refreshButton_Click(object sender, RoutedEventArgs e)
- {
- getAvailPorts();
- }
- private void mainPage_Loaded(object sender, RoutedEventArgs e)
- {
- }
- private void RefButton_Click(object sender, RoutedEventArgs e)
- {
- Listen();
- }
- }
- }
Add Comment
Please, Sign In to add comment