Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- using System.IO.Ports;
- namespace SerialStupid
- {
- public partial class Form1 : Form
- {
- bool isConnected = false;
- string[] ports;
- SerialPort port;
- public Form1()
- {
- InitializeComponent();
- disableControls();
- getAvailableComPorts();
- foreach(string port in ports)
- {
- comboBox1.Items.Add(port);
- Console.WriteLine(port);
- if (ports[0] != null)
- {
- comboBox1.SelectedItem = ports[0];
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (!isConnected)
- {
- connectToArduino();
- }
- else
- {
- disconnectFromArduino();
- }
- }
- void getAvailableComPorts()
- {
- ports = SerialPort.GetPortNames();
- }
- private void connectToArduino()
- {
- isConnected = true;
- string selectedPort = comboBox1.GetItemText(comboBox1.SelectedItem);
- port = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One);
- port.Open();
- port.Write("#STAR\n");
- button1.Text = "Disconnect";
- enableControls();
- }
- private void disconnectFromArduino()
- {
- isConnected = false;
- port.Write("#STOP\n");
- port.Close();
- button1.Text = "Connect";
- disableControls();
- resetDefaults();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if (isConnected)
- {
- port.Write("#TEXT" + textBox1.Text + "#\n");
- }
- }
- private void enableControls()
- {
- groupBox1.Enabled = true;
- button2.Enabled = true;
- textBox1.Enabled = true;
- }
- private void disableControls()
- {
- groupBox1.Enabled = false;
- button2.Enabled = false;
- textBox1.Enabled = false;
- }
- private void resetDefaults()
- {
- textBox1.Text = "";
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
- #include <LiquidCrystal.h>
- LiquidCrystal liq(12, 11, 5, 4, 3, 2);
- String inputString = "";
- bool stringComplete = false;
- String commandString = "";
- bool isConnected = false;
- void setup(){
- Serial.begin(9600);
- initDisplay();
- }
- void loop(){
- if (stringComplete){
- stringComplete = false;
- getCommand();
- if (commandString.equals("STAR")){
- liq.clear();
- }
- if (commandString.equals("STOP")){
- liq.clear();
- liq.write("Ready To Connect");
- }
- else if (commandString.equals("TEXT")){
- String text = getTextToPrint();
- printText(text);
- }
- }
- }
- void initDisplay(){
- liq.begin(16, 2);
- liq.write("Ready To Connect");
- }
- void getCommand(){
- if (inputString.length() > 0){
- commandString = inputString.substring(1, 5);
- }
- }
- String getTextToPrint(){
- String value = inputString.substring(5, inputString.length() - 2);
- return value;
- }
- void printText(String text){
- liq.clear();
- liq.setCursor(0, 0);
- if (text.length() < 16){
- liq.print(text);
- }
- else{
- liq.print(text.substring(0, 16));
- liq.setCursor(0, 1);
- liq.print(text.substring(16, 32));
- }
- }
- void serialEvent(){
- while(Serial.available()){
- char inChar = (char)Serial.read();
- inputString += inChar;
- if (inChar == '\n'){
- stringComplete = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement