The same one that the Serial Monitor uses and that the Arduino outputs - String.
i've changed the data type into string but still no data displayed in textbox. i tried with another way to get the sensor value. i send the "c" character to arduino and arduino print the sensor value.below is the code
int trigPin = 8;
int echoPin = 9;
void setup(void)
{
Serial.begin(19200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop(void)
{
if(Serial.available()){
char val = Serial.read();
if(val != -1)
{
switch(val)
{
case 'c':
ultrasonicSensor(); //Print the Sensor value
break;
}
}
}
}
void ultrasonicSensor()
{
int Duration, Distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
Duration = pulseIn(echoPin, HIGH);
Distance = (Duration/2) / 29.1;
Serial.println(Distance);
delay(500);
}
in visual basic, im using the timer to send the "c" character.below is the code
Imports System
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Threading
Public Class Form1
Dim myPort As Array
Dim Distance As Integer
Delegate Sub SetTextCallBack(ByVal [text] As String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
PortComboBox.Items.AddRange(myPort)
BaudComboBox.Items.Add(9600)
BaudComboBox.Items.Add(19200)
BaudComboBox.Items.Add(38400)
BaudComboBox.Items.Add(57600)
BaudComboBox.Items.Add(115200)
ConnectButton.Enabled = True
DisconnectButton.Enabled = False
End Sub
Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
SerialPort1.PortName = PortComboBox.Text
SerialPort1.BaudRate = BaudComboBox.Text
SerialPort1.Open()
lblMessage.Text = PortComboBox.Text & " Connected."
ConnectButton.Enabled = False
DisconnectButton.Enabled = True
End Sub
Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
SerialPort1.Close()
lblMessage.Text = SerialPort1.PortName & " Disconnected."
DisconnectButton.Enabled = False
ConnectButton.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
SerialPort1.Write("c")
System.Threading.Thread.Sleep(250)
Dim i As String = SerialPort1.ReadExisting()
ListBoxSensor.Text = i
Me.Refresh()
Catch ex As Exception
End Try
End Sub
End Class
on timer, i set the interval to 1000. but still not working. whats wrong actually?
thanks for your reply.