Question
Write a C# Windows Forms App with a start button, stop button and textbox with a vertical scrollbar and serialport. The serialport commands are written
Write a C# Windows Forms App with a start button, stop button and textbox with a vertical scrollbar and serialport.
The serialport commands are written in the DoCommands function one after another as shown in the code below. Modify this function by writing if else statements to check
Check for Abort=false If Abort=false Execute next command Post completed message to other thread to update text box If Abort=true, ignore commands
When done, post Arrived message and Terminate thread.
So that on the press of the start button the serial port commands are executed and displayed in the textBox and on the press of stop button the serial port commands stop executing instantly without completing all the commands inside the DoCommands Function.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading;
namespace DoCommands_Delegates { public partial class Form1 : Form {
bool Abort = false; public Form1() { InitializeComponent(); }
private void Form1_Load(object sender, EventArgs e) { serialPort.Open(); }
private void btnStart_Click(object sender, EventArgs e) { Abort = false; Thread mythread = new Thread(DoCommands); mythread.Start();
}
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string myData = serialPort.ReadExisting();
BeginInvoke((Action)delegate () { textBox.Text += myData; }); }
/* Inside the DoCommands write if-else statements to check for abort. * Check for Abort=false If Abort=false Execute next command Post completed message to other thread to update text box If Abort=true, ignore commands
When done, post Arrived message and Terminate thread
*/ void DoCommands() { serialPort.Write("8"); Thread.Sleep(4000); // if you need to update the GUI to real time DO NOT USE SLEEP. textBox.Text += (Environment.NewLine + "Forwards" + Environment.NewLine);
serialPort.Write("5"); Thread.Sleep(500); textBox.Text += ("stop" + Environment.NewLine);
serialPort.Write("."); Thread.Sleep(100); textBox.Text += ("Robot rotate right 20 Deg" + Environment.NewLine);
serialPort.Write("."); textBox.Text += ("Robot rotate right 20 Deg" + Environment.NewLine);
serialPort.Write("."); Thread.Sleep(100); textBox.Text += ("Robot rotate right 20 Deg" + Environment.NewLine);
serialPort.Write("5"); Thread.Sleep(100); textBox.Text += ("stop" + Environment.NewLine);
serialPort.Write("8"); Thread.Sleep(200); // if you need to update the GUI to real time DO NOT USE SLEEP. textBox.Text += (Environment.NewLine + "Forward" + Environment.NewLine);
serialPort.Write("5"); Thread.Sleep(500); textBox.Text += ("stop" + Environment.NewLine);
serialPort.Write("."); Thread.Sleep(100); textBox.Text += ("Robot rotate right 20 Deg" + Environment.NewLine);
serialPort.Write("8"); Thread.Sleep(4000); // if you need to update the GUI to real time DO NOT USE SLEEP. textBox.Text += (Environment.NewLine + "Forwards" + Environment.NewLine);
serialPort.Write("."); Thread.Sleep(100); textBox.Text += ("Robot rotate right 20 Deg" + Environment.NewLine);
serialPort.Write("."); Thread.Sleep(100); textBox.Text += ("Robot rotate right 20 Deg" + Environment.NewLine);
serialPort.Write("9"); Thread.Sleep(100); textBox.Text += ("robot drift Right" + Environment.NewLine);
serialPort.Write("5"); Thread.Sleep(500); textBox.Text += ("stop" + Environment.NewLine);
serialPort.Write("5"); Thread.Sleep(100); textBox.Text += ("Robot Stop " + Environment.NewLine);
MessageBox.Show("Mission Accomplished", "Status"); }
private void btnStop_Click(object sender, EventArgs e) {
}
} }
Separate docommandsO thread Main GUI thread Start Draw Form Draw Buttons Open Serial Port Set Abort-false Monitor buttons If 'Go', start thread If 'Abort doCommands0 thread Check for Abort-false If Abort-false set Abort-true send 'stop Update screen with Abort message Execute next command Post completed message to other thread to update t box If Abort-true, ignore commands When done, post 'Arrived' message and Terminate thread Note that the GUI thread doesn't stop the commands - it just sets the 'Abort flag for the other thread to respond to. Threads share variables
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started