Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows;
- using System.IO.Ports;
- public partial class MainWindow : Window
- {
- int MaxNumberOfItemsAllowed = 1000;
- SerialPort mainport = new SerialPort();
- public MainWindow()
- {
- InitializeComponent();
- GetSerialDevices(); //gets the ports and adds them when the program is loaded.
- mainport.DataReceived += Mainport_DataReceived;
- }
- private void Mainport_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- if (mainport.IsOpen)
- {
- try
- {
- Task.Factory.StartNew(() =>
- {
- //ReadLine() i think only returns the port's buffer if there's a '\n' on the very end. if it's in
- //the middle... i dont think it gives one and ignores it, returning null.
- //This whole method might fire 2 or 3 times, and only the last time will it actually add a message... i think.
- WriteReceived($"[RX]> {mainport.ReadLine()}");
- });
- }
- catch(Exception g) { MessageBox.Show(g.Message, "Error receiving data on serial port"); }
- }
- }
- //this method will get all the serial devices on the computer (your computer, probably...)
- private void GetSerialDevices()
- {
- foreach(string deviceport in SerialPort.GetPortNames())
- {
- serialports.Items.Add(deviceport);
- }
- }
- private void WriteReceived(string text)
- {
- //this makes writing to the received listbox easier and will clear if it has too many items
- //to decrease lag.
- Dispatcher.BeginInvoke(new Action(() =>
- {
- received.Items.Add(text);
- if (received.Items.Count >= MaxNumberOfItemsAllowed)
- {
- received.Items.Clear();
- }
- }));
- }
- private void WriteSent(string text)
- {
- //this makes writing to the received listbox easier and will clear if it has too many items
- //to decrease lag.
- Dispatcher.BeginInvoke(new Action(() =>
- {
- sent.Items.Add(text);
- if (sent.Items.Count >= MaxNumberOfItemsAllowed)
- {
- sent.Items.Clear();
- }
- }));
- }
- private void Connectbutton_Click(object sender, RoutedEventArgs e)
- {
- if (!mainport.IsOpen)
- {
- try
- {
- mainport.BaudRate = 115200;
- mainport.DataBits = 8;
- mainport.Handshake = Handshake.None; //dont need to define this, but whatever :)
- mainport.Parity = Parity.None;
- mainport.StopBits = StopBits.One;
- }
- catch (Exception ggg)
- {
- MessageBox.Show(ggg.Message, "Error defining variables for serial port");
- return;
- }
- try
- {
- if (serialports.SelectedItem != null)
- {
- mainport.PortName = serialports.SelectedItem.ToString();
- }
- }
- catch (Exception ggg)
- {
- MessageBox.Show(ggg.Message, "Error defining the portname for serial port");
- return;
- }
- ///quite alot of try/catches but whatever ;)
- try
- {
- if (!mainport.IsOpen) //double precaution ;)
- {
- try
- {
- mainport.Open();
- connectbutton.Content = "Disconnect";
- }
- catch (Exception dfjfd)
- {
- MessageBox.Show(dfjfd.Message); return;
- }
- }
- else
- {
- try
- {
- mainport.Close();
- connectbutton.Content = "Connect";
- }
- catch (Exception dfjfd)
- {
- MessageBox.Show(dfjfd.Message);
- }
- }
- }
- catch (Exception gg)
- {
- MessageBox.Show(gg.Message);
- }
- }
- else
- {
- try
- {
- mainport.Close();
- connectbutton.Content = "Connect";
- }
- catch (Exception dfjfd)
- {
- MessageBox.Show(dfjfd.Message);
- }
- }
- }
- private void Sendmessage_Click(object sender, RoutedEventArgs e)
- {
- if (mainport.IsOpen)
- {
- if (!string.IsNullOrEmpty(sendtextbox.Text))
- {
- try
- {
- string towrite = sendtextbox.Text;
- mainport.Write(towrite);
- WriteSent($"[TX]> {towrite}");
- }
- catch(Exception gg) { MessageBox.Show(gg.Message); }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment