is it possible to send binary data or hex data from arduino to VB.net?...
for example..i want to send 00010(in bin) or 02h in hex to vb.net..i read somewhere that its just connecting the serial port to vb.net by i dont know where to start..what would be the code if i want to send data to vb.net?..
for (int i = 0; i < 100; i++) {
Serial.print (i, BYTE);
delay(100);
}
Then write some code on the PC to read the value and do something with it.
The following should give you a start.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Long
Do
i = SerialPort1.ReadByte()
TextBox1.Text = TextBox1.Text + "<" + Str(i) + ">"
TextBox1.Refresh()
Loop While i <> 100
End Sub
Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
If SerialPort1.IsOpen() Then
SerialPort1.Close()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = "Com37"
SerialPort1.Open()
End Sub
End Class
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() { //This will print bytes from 0-100
for (int i = 0; i < 100; i++) {
Serial.print (i, BYTE);
delay(1000);
}
}
while arduino is sending data to the computer..A vb.net software will be listening to what that port is sending (in my case COM5)
here are the codes for vb.net
Imports System.Threading
Imports System.IO
Imports System.Text
Imports System.IO.Ports
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Long
Do
i = SerialPort1.ReadByte
RichTextBox1.Text = RichTextBox1.Text + "<" + Str(i) + ">"
RichTextBox1.Refresh()
TextBox1.Text = "Hellow World"
Loop While i <> 100
End Sub
Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
If SerialPort1.IsOpen() Then
SerialPort1.Close()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = "Com5"
SerialPort1.Open()
End Sub
End Class
a serialport object is added to the form,with variable name "SerialPort1"..when the button1 is clicked..it will fetch the data that com5 is sending..(0-100 bytes based on the code above)..i dont really know how to convert that byte to string..
heres another problem i want to resolve..for example..i want to send "5" to com5(Serial.print (5, BIN) , when you open the serial monitor from arduino..it will output "101" which is the equivalent of 5 in Binary..but when fetch to the vb.net software..VB's output are 49,48,49....(49 is the ASCII code for 1 and 48 for 0)..how do i change that output from the software to BIN also..not 49,48,49 but instead 101..
The reason your form "Freeze" is because the UI (User Interface) is in the same thread as your code.
When your software is busy reading the serialport, the UI will "freeze", and because this is a loop in the code, it will just hang there untill you close it.
The solution for that is to put the loop code inside a new thread.
That being said, thank you for the code. Playing around with Ping))) Ultrasonic and VB now.