Communicating to the Arduino in the .NET platform is pretty much straightforward: The Arduino uses a virtual serial port to allow programs to be written onto it, but we can also use this port to get and send data to and from the Arduino.
Materials:
- Arduino or Arduino Compatible Board (UNO, Mega, Etc.)
- Compatible USB Cable to Connect the Arduino to the Host Computer
- The Arduino Software (Available Here )
- VB.NET 2010 or 2012 (Available Here )
Step 1: Adding a Program to Send Data From the Arduino
Let's start by opening up the Arduino software, and copying/pasting in the following code:void setup() {
Serial.begin(9600);
}
//Sends the Number 1234 Over the Serial Port Once Every Second
void loop() {
Serial.write(1234)
delay(1000)
}
This program opens up a serial port, and sends the number string, 1234, over the port once every second in an infinite loop. The number '1234' has no special meaning, you could place any number there in place of it. Then plug in your Arduino board and upload the code to it. Leave it plugged in.
Step 2: Creating the .NET Program
Creating the .NET code in VB.NET is actually quite simple and easy to understand. Open a new VB.NET console program,
clear all of the code, and paste in the following:
Module
Module1
Sub
Main()
Dim
userRespond
Dim
fetchedData
Dim
writeString
Console.WriteLine(
"Type R to Read Serial Port."
)
userRespond = Console.ReadLine
If
userRespond =
"R"
Or
userRespond =
"r"
Then
Dim
port
As
New
System.IO.Ports.SerialPort
port.PortName =
"COM4"
port.Open()
port.BaudRate = 9600
fetchedData = port.ReadByte
Console.WriteLine(fetchedData)
Console.ReadLine()
Else
Console.WriteLine(
"Not a Recognized Command."
)
End
If
End
Sub
End
Module
This code should be fairly simple. It opens a new console window, then it asks the user if they want to get the data from the port, next (if they said yes), it opens up a new COM port and gets the data, finally it writes it to the console. You may need to change COM4 to the correct port for your Arduino. It lists what port Arduino is on in the Arduino software bottom right corner. You can not write a new program to the Arduino when this software is running, for it gives an error that says that the COM port is busy.
Step 3: Testing
To
use this program, be sure your Arduino is plugged in, and it is running
the code from step 1. Now, run the program from above and type in R. It
may take a second (the Arduino only sends it
once per second), but the number 1234 will come up! If you change the
number 1234 in the Arduino code to "hello" and upload it and run the
.NET program, it will only give you the decimal value for a lower-case
h. This is because this program can only read
one byte at a time. See if you can change it up to read the whole word
and convert it to unicode! Post your inventions!
1 komentar:
Prediction Correct Score Today Match 우리카지노 마틴 우리카지노 마틴 카지노 카지노 427Horse Racing Each Way Betting Strategy - ThauberBet
Posting Komentar