Harware needed:
1. Arduino Pro Mini https://amzn.to/2xy4yFn
2. UART PCB https://amzn.to/2we150O
3. Computer (installed Visual Studio)
(1) Arduino will sent data to Visual Studio program:
(2) Visual Studio program will send data to Arduino. Arduino received it, then feed it back to Visual Studio program
Overview steps:
1. Hardware connection
2. Arduino program
3. Visual Studio program
1. Hardware connection
2. Arduino program
Make a program for Arduino. At start up, Arduino will send a string (a sentence) in every 0.5sec. This will stop until User send data (string) to Arduino, then Arduino will send it back to User. In this case, User is Visual Studio program (in part 3)
The code can be download at here - Google share
Arduino program
String mySt;
char myChar = 0;
int i=0;
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
if (Serial.available() > 0) {
myChar = Serial.read();
mySt +=myChar; //receive string from Computer
}
if ((mySt.length() >0)&(!Serial.available())) {
Serial.print(mySt); //Print received string to Computer
mySt="";
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print("Arduino send: ");
Serial.println(i); //Print increasing value to Computer
i+=1;
delay(500);
}
}
char myChar = 0;
int i=0;
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
if (Serial.available() > 0) {
myChar = Serial.read();
mySt +=myChar; //receive string from Computer
}
if ((mySt.length() >0)&(!Serial.available())) {
Serial.print(mySt); //Print received string to Computer
mySt="";
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print("Arduino send: ");
Serial.println(i); //Print increasing value to Computer
i+=1;
delay(500);
}
}
3. Visual Studio porgram
Make a window application -> save it
Add button, textbox, label to the Form( taken from Toolbox in the left):
Click on button, textbox, label to see Properties on Toolbox in the right. Remember the name of each item for program in latter section.
Add component "serial Port" and "timer"
Also see properties to know the name each item. Remember to rename "serialPort1" -> "Portname" to COM-Port of Arduino (this case is COM4)
The whole code of Visual Studio program can be downloaded at here - Google share
(1) Double click to Form1 -> input following code.
Meaning: open (Arduino) COM-Port
start timer1
Form1 code
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
serialPort1->Open();
timer1->Start();
}
serialPort1->Open();
timer1->Start();
}
(2) Double click to Button1 -> input following code
Meaning: send string from textBox1 to (Arduino) COM-Port
Send data code
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
serialPort1->WriteLine(textBox1->Text);
}
serialPort1->WriteLine(textBox1->Text);
}
(3) Double click to timer1 -> input following code
Meaning: every time timer1 is tick (it will tick every 0.1s -> setting in "Interval" of Properties of timer1), label1 will update information.
timer code
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
label1->Text=mStr;
}
label1->Text=mStr;
}
(4) Click to serialPort1 -> see at Properties toolbox -> click at "Event" icon -> double click at "DataReceived"
Then, input following code.
Meaning: read COM-Port data at every time receiving -> save it to "mStr"
COM-Port code
private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
mStr=serialPort1->ReadLine();
}
mStr=serialPort1->ReadLine();
}
Auxiliary code:
(1) Meaning: when Form1 is close -> close COM-Port, and stop timer1
~Form1()
{
if (components)
{
delete components;
}
serialPort1->Close();
timer1->Stop();
}
{
if (components)
{
delete components;
}
serialPort1->Close();
timer1->Stop();
}
(2) Meaning: make global variable "mStr"
private:
/// <summary>
/// Required designer variable.
String^ mStr;
/// </summary>
/// <summary>
/// Required designer variable.
String^ mStr;
/// </summary>
After all, click icon "Local Window Debugger" to build and run program
If every thing run smoothly, a window form will appear (note: Arduino should connected to Computer through COM4 with program as in Step2)
At result (1): Form1 will show result from Arduino
At result (2): Form1 will sent data to Arduino, then receive it after Arduino sending.
yes! This is wonderful. I try'd this with a blanc universal app, but this seems to work differently.
ReplyDeleteI was thinking if I use a universal app, I could use both my computer and my tablet to connect with arduino. Do you know how to do this
Sorry. I have never try this. To communicate with tablet, you can use ESP12E which has wifi connection
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThank you very much ur the best
ReplyDeletei hope to see more projects done by you
you teach better than anyone
Thanks, your comment encourage me to do more.
DeletePlease share my blog if you think it's helpful other. I will try to do more project
good job !!! do you know how to interface with labview?
ReplyDeleteno, i didn't work it with Labview
Deletesir
Deletewhy Local Window Debugger not appear?
why appeared?
ReplyDeleteunable to start program.
Debug\windowsformsaplication3.exe.
help me sir.
Did you install Visual Studio 2012 into your computer? the program windowsformsaplication3.exe need those environment to run
DeleteI am glad that I found this. Thank you giving tutorial. I have a question to ask. Is it possible to send parameters from arduino via esp8266 and let visual studio receive it via internet? Btw the project that I do is based IoT.
ReplyDeletesorry, i didn't try this before.
DeleteThank you for this wonderful project. I have a question, is it possible to extract the data points of the generated graph in .csv file? Thanks!
ReplyDeletesure, i will make this kind project. Please wait, i will post it here.
Delete
ReplyDeletehello and tried to reproduce the project made by you but I have problems with the visual study without error of sale
Hi. Please learn to make application with Visual Studio step by step. Then make application like this. Error is case by case, debug is also a skill needed for programmer
DeleteWow, this is fascinating reading. I am glad I found this and got to read it. Great job on this content. I liked it a lot. Thanks for the great and unique info. https://kissenglishcenter.com/cau-giao-tiep-tieng-anh-thong-dung/
ReplyDeleteGreat Article
ReplyDeleteInternet of Things Final Year Project
Final Year Project Centers in Chennai
excuse me, whare i get the visual studio software?
ReplyDeleteplease search on Google. Normally, this software should be purchased
DeleteHi, looks to be a needed example for many of use using Arduino's. THANKS.
ReplyDeleteVisual Studio Community 2019 is a free version of Visual Studio, but the Com code you
describe to be entered in SerialPort1> DataReceived is Flagged as an Error "private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {
mStr=serialPort1->ReadLine();
}
ERROR REPORT from VS
Property value is not valid, then this is the Details.
'private: System::Void serialPort1_DataReceived(System::Object^ sender, System::IO::Ports::SerialDataReceivedEventArgs^ e) {' is not a valid identifier.
Any help available on this
Visual Studio 2019 Error
ReplyDeleteSeverity Code Description Project File Line Suppression State
Error LNK1104 cannot open file 'MSVCURTD.LIB' WindowsFormsApplication3