Advertisement
CuriousScientist

Arduino Serial Terminal for PC

Oct 16th, 2019
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. //If you found my video helpful, please SUBSCRIBE:  https://www.youtube.com/channel/UCKp1MzuAceJnDqAvsZl_46g
  2. //The code belongs to the following tutorial video: https://youtu.be/rQTDD0bpNWE
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.IO.Ports;  //use the ports for serial communications (1)
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System.Threading;
  15.  
  16. namespace ArduinoSerialTutorial
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.         private SerialPort SerPort; //serial port
  21.         private string ReceivedData; //received data
  22.  
  23.  
  24.  
  25.  
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.  
  30.             FetchAvailablePorts(); //run the port-fetching
  31.         }
  32.  
  33.         void FetchAvailablePorts()
  34.         {
  35.             String[] ports = SerialPort.GetPortNames();//Put the name of the available USB ports into the ports string array
  36.             AvailablePortsBox.Items.AddRange(ports); //Add the string into the dropdown list we made
  37.         }
  38.  
  39.         private void ConnectToPort_Click(object sender, EventArgs e)
  40.         {
  41.             //This is some predefined thing that you can get from Google
  42.             SerPort = new SerialPort();
  43.             SerPort.BaudRate = 9600;
  44.             SerPort.PortName = AvailablePortsBox.Text;
  45.             SerPort.Parity = Parity.None;
  46.             SerPort.DataBits = 8;
  47.             SerPort.StopBits = StopBits.One;
  48.             SerPort.ReadBufferSize = 20000000;
  49.             SerPort.DataReceived += SerPort_DataReceived;
  50.  
  51.  
  52.             try
  53.             {
  54.                 SerPort.Open(); //We open the port
  55.                 Thread.Sleep(1000); //We wait a sec
  56.                
  57.             }
  58.             catch (Exception ex)
  59.             {
  60.                 MessageBox.Show(ex.Message, "Error!");
  61.  
  62.             }
  63.         }
  64.  
  65.         private void SerPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
  66.         {
  67.             ReceivedData = SerPort.ReadLine(); //We read the serial port
  68.  
  69.             this.Invoke(new Action(ProcessingData)); //execute the delegate (ProcessingData)
  70.         }
  71.  
  72.         private void SendSerialButton_Click(object sender, EventArgs e)
  73.         {
  74.             SerPort.WriteLine(SenderTextBox.Text.ToString()); //send the content of the textbox to the serial port
  75.         }
  76.  
  77.         private void ProcessingData()
  78.         {
  79.             ReceivedDataBox.Text += ReceivedData.ToString() + Environment.NewLine;
  80.             //We put the received data in the textbox and add a linebreak
  81.  
  82.         }
  83.  
  84.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  85.         {
  86.             try
  87.             {
  88.                 SerPort.Close(); //close the port on from closing
  89.             }
  90.             catch
  91.             {
  92.             }
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement