Hi, i want to develop my project by create some interface gui from visual C# to monitor data that receive from Arduino (i used leonardo).
problem is i cannot read data from arduino by usb port but i can send data from visual c# to arduino and arduino response with data that i send.
then how i can read data from arduino.
my code is below.
Visual C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace usb_port_test
{
public partial class main : Form
{
public main()
{
InitializeComponent();
//List port available
foreach (string iPort in SerialPort.GetPortNames())
{
cboPort.Items.Add(iPort);
}
//list baudrate
cboBaud.Text = "9600"; //default
int[] iBaud = new int[] {9600,14400,19200,57600,115200};
foreach ( int i in iBaud )
{
cboBaud.Items.Add(i);
}
richTextBox1.AppendText("Start ...");
}
public void portSetting() // usb port config
{
usbPort.PortName = cboPort.Text;
usbPort.BaudRate = Convert.ToInt32(cboBaud.Text);
usbPort.DataBits = 8;
usbPort.Parity = Parity.None;
usbPort.StopBits = StopBits.One;
usbPort.Encoding = System.Text.ASCIIEncoding.Default;
usbPort.ReceivedBytesThreshold = 1;
usbPort.ReadTimeout = 5000;
usbPort.WriteTimeout = 5000;
usbPort.NewLine = "/n";
}
private void btnOpen_Click(object sender, EventArgs e) // button open port
{
if (usbPort.IsOpen == false)
{
try
{
portSetting();
usbPort.Open();
richTextBox1.AppendText(Environment.NewLine + "Open port " + cboPort.Text + " successful");
}
catch (Exception ex)
{
richTextBox1.AppendText(Environment.NewLine + "Error : " + ex.Message);
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show(cboPort.Text + " is already open");
}
}
private void btnClose_Click(object sender, EventArgs e) // button close port
{
if (usbPort.IsOpen)
{
usbPort.Close();
richTextBox1.AppendText(Environment.NewLine + "Close port " + cboPort.Text + " successful");
}
else
{
MessageBox.Show(cboPort.Text + " is already close");
}
}
private void btnSend0_Click(object sender, EventArgs e) // button send 0 and receive
{
try
{
richTextBox1.AppendText(Environment.NewLine + "Sending 0 ...");
usbPort.Write("0");
richTextBox1.AppendText(" successful");
richTextBox1.AppendText(Environment.NewLine + "Receiving Data ...");
string iRead = usbPort.ReadLine();
richTextBox1.AppendText(" successful and data is " + iRead);
Thread.Sleep(1000);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
richTextBox1.AppendText(Environment.NewLine + "Error : " + ex.Message);
}
}
private void btnSend1_Click(object sender, EventArgs e) // button send 1 and receive
{
try
{
richTextBox1.AppendText(Environment.NewLine + "Sending 1 ...");
usbPort.Write("1");
richTextBox1.AppendText(" successful");
richTextBox1.AppendText(Environment.NewLine + "Receiving Data ...");
string iRead = usbPort.ReadLine();
richTextBox1.AppendText(" successful and data is " + iRead);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
richTextBox1.AppendText(Environment.NewLine + "Error : " + ex.Message);
}
}
private void richTextBox1_TextChanged(object sender, EventArgs e) // auto scroll down
{
richTextBox1.ScrollToCaret();
}
}
}
Arduino Sketch
int getdata = 0;
void setup()
{
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
Serial.begin(9600);
}
void loop()
{
getdata = Serial.read();
test();
}
void test()
{
if(getdata == '0')
{
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
Serial.write("5");
delay(1000);
digitalWrite(0, LOW);
}
else if(getdata == '1')
{
digitalWrite(1, HIGH);
digitalWrite(0, LOW);
Serial.write("6");
delay(1000);
digitalWrite(1, LOW);
}
delay(500);
}