using System.IO.Ports;
namespace ArduinoWireless
{
public partial class Form1 : Form
{
string dataIN;
string dataIN2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
string[] ports = SerialPort.GetPortNames();
cbCom1.Items.AddRange(ports);
}
private void btnWrite2Port_Click(object sender, EventArgs e)
{
try
{
if (serialPort1.IsOpen)
{
serialPort1.WriteLine(tbDataOut1.Text + Environment.NewLine);
tbReceiveData1.Clear();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
dataIN1 = serialPort1.ReadExisting();
this.Invoke(new EventHandler(ShowData));
}
private void ShowData(object sender, EventArgs e)
{
int dataINLength = dataIN1.Length;
lblDataInLength.Text = string.Format("{0:00}", dataINLength);
if (cbAlwaysUpdate.Checked)
{
tbReceiveData1.Text = "";
tbReceiveData1.Text = dataIN1;
string s = serialPort1.ReadExisting();
if (s.Contains("1234"))
{
tbReceiveData1.Text = s.ToString();
}
if (s.Contains("4321"))
{
tbReceiveData1.Text = s.ToString();
}
else
{
}
}
}
private void tbReceiveData1_TextChanged(object sender, EventArgs e)
{
if (tbReceiveData1.Text == "1234\r\n")
{
MessageBox.Show("1234 entered port");
}
if (tbReceiveData1.Text == "4321\r\n")
{
MessageBox.Show("4321 entered port");
}
}
}
}