Advertisement
Guest User

Serial Communication Doesn't have

a guest
Dec 14th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using System.IO.Ports;
  4.  
  5. namespace SerialStupid
  6. {
  7.     public partial class Form1 : Form
  8.     {
  9.         bool isConnected = false;
  10.         string[] ports;
  11.         SerialPort port;
  12.         public Form1()
  13.         {
  14.             InitializeComponent();
  15.             disableControls();
  16.             getAvailableComPorts();
  17.             foreach(string port in ports)
  18.             {
  19.                 comboBox1.Items.Add(port);
  20.                 Console.WriteLine(port);
  21.                 if (ports[0] != null)
  22.                 {
  23.                     comboBox1.SelectedItem = ports[0];
  24.                 }
  25.             }
  26.         }
  27.        
  28.         private void button1_Click(object sender, EventArgs e)
  29.         {
  30.             if (!isConnected)
  31.             {
  32.                 connectToArduino();
  33.             }
  34.             else
  35.             {
  36.                 disconnectFromArduino();
  37.             }
  38.         }
  39.        
  40.         void getAvailableComPorts()
  41.         {
  42.             ports = SerialPort.GetPortNames();
  43.         }
  44.  
  45.         private void connectToArduino()
  46.         {
  47.             isConnected = true;
  48.             string selectedPort = comboBox1.GetItemText(comboBox1.SelectedItem);
  49.             port = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One);
  50.             port.Open();
  51.             port.Write("#STAR\n");
  52.             button1.Text = "Disconnect";
  53.             enableControls();
  54.         }
  55.  
  56.         private void disconnectFromArduino()
  57.         {
  58.             isConnected = false;
  59.             port.Write("#STOP\n");
  60.             port.Close();
  61.             button1.Text = "Connect";
  62.             disableControls();
  63.             resetDefaults();
  64.         }
  65.  
  66.         private void button2_Click(object sender, EventArgs e)
  67.         {
  68.             if (isConnected)
  69.             {
  70.                 port.Write("#TEXT" + textBox1.Text + "#\n");
  71.             }
  72.         }
  73.  
  74.         private void enableControls()
  75.         {
  76.             groupBox1.Enabled = true;
  77.             button2.Enabled = true;
  78.             textBox1.Enabled = true;
  79.         }
  80.  
  81.         private void disableControls()
  82.         {
  83.             groupBox1.Enabled = false;
  84.             button2.Enabled = false;
  85.             textBox1.Enabled = false;
  86.         }
  87.  
  88.         private void resetDefaults()
  89.         {
  90.             textBox1.Text = "";
  91.         }
  92.  
  93.         private void Form1_Load(object sender, EventArgs e)
  94.         {
  95.  
  96.         }
  97.        
  98.     }
  99. }
  100.  
  101.  
  102.  
  103. #include <LiquidCrystal.h>
  104.  
  105. LiquidCrystal liq(12, 11, 5, 4, 3, 2);
  106.  
  107. String inputString = "";
  108. bool stringComplete = false;
  109. String commandString = "";
  110. bool isConnected = false;
  111.  
  112. void setup(){
  113.     Serial.begin(9600);
  114.     initDisplay();
  115. }
  116.  
  117. void loop(){
  118.     if (stringComplete){
  119.         stringComplete = false;
  120.         getCommand();
  121.         if (commandString.equals("STAR")){
  122.             liq.clear();
  123.         }
  124.         if (commandString.equals("STOP")){
  125.             liq.clear();
  126.             liq.write("Ready To Connect");
  127.         }
  128.         else if (commandString.equals("TEXT")){
  129.             String text = getTextToPrint();
  130.             printText(text);
  131.         }
  132.     }
  133. }
  134.  
  135. void initDisplay(){
  136.     liq.begin(16, 2);
  137.     liq.write("Ready To Connect");
  138. }
  139.  
  140. void getCommand(){
  141.     if (inputString.length() > 0){
  142.         commandString = inputString.substring(1, 5);
  143.     }
  144. }
  145.  
  146. String getTextToPrint(){
  147.     String value = inputString.substring(5, inputString.length() - 2);
  148.     return value;
  149. }
  150.  
  151. void printText(String text){
  152.     liq.clear();
  153.     liq.setCursor(0, 0);
  154.     if (text.length() < 16){
  155.         liq.print(text);
  156.     }
  157.     else{
  158.         liq.print(text.substring(0, 16));
  159.         liq.setCursor(0, 1);
  160.         liq.print(text.substring(16, 32));
  161.     }
  162. }
  163.  
  164. void serialEvent(){
  165.     while(Serial.available()){
  166.         char inChar = (char)Serial.read();
  167.         inputString += inChar;
  168.         if (inChar == '\n'){
  169.             stringComplete = true;
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement