通过串行通信从arduino发送到vb.net应用程序

时间:2016-04-07 16:56:27

标签: vb.net arduino usb

我试图在vb.net中创建一个可以通过串行通信(USB)从arduino读取温度的应用程序。

我希望只有在用户点击按钮时才能读取温度。

我在下面发布了我的代码。当我点击按钮时,BtnManueel_Click'我首先向arduino发送命令。当arduino收到此命令时,它将读取温度结束发送给PC。当我使用串行监视器时,这非常有效。但是,当我使用下面的代码运行我的应用程序时,我无法获得vb.net中的值。

我做错了什么和/或为什么它不起作用?

我对vb.net和串口通信没有多少经验,我希望你能帮我解决这个问题。

提前致谢。

亲切的问候。

vb.net代码:

Imports System
Imports System.IO.Ports
Imports System.Windows.Forms

Public Class Form1
    Dim serielepoort As New SerialPort
    Dim celsius As Double

    Private Sub BtnVerbinden_Click(sender As Object, e As EventArgs) Handles BtnVerbinden.Click
        Try
            serielepoort.PortName = "COM8"
            serielepoort.BaudRate = 9600
            serielepoort.DataBits = 8
            serielepoort.Parity = Parity.None
            serielepoort.StopBits = StopBits.One
            serielepoort.Handshake = Handshake.None
            serielepoort.Encoding = System.Text.Encoding.Default
            serielepoort.ReadTimeout = 10000
            serielepoort.Open()

        Catch ex As Exception

            MessageBox.Show(ex.Message, "fout bij verbinden!", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub


    Private Sub BtnVerbindingVerbreken_Click(sender As Object, e As EventArgs) Handles BtnVerbindingVerbreken.Click

        serielepoort.Close()

    End Sub


    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed

        BtnVerbindingVerbreken_Click(sender, e)

    End Sub


    Private Sub BtnManueel_Click(sender As Object, e As EventArgs) Handles BtnManueel.Click

        If (serielepoort.IsOpen) Then
            serielepoort.Write("t")            
        End If

    End Sub


    Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)

        celsius = serielepoort.ReadLine()
        LblTempWaarde.Text = celsius

    End Sub

End Class

arduino中的代码:

nt pinLM335 = 0;


void setup() 
{
  Serial.begin(9600);

}

void loop() {
  if(Serial.available() >0)
  {
   String val = Serial.readStringUntil('\n');
   if (val == "t")
   { int waarde = analogRead(pinLM335);
   double spanning =(waarde / 1024.0)*5;
   double Kelvin = (spanning) * 1000 / 10;
   double celsius = Kelvin - 273.15;

   Serial.println(celsius);
  }
  }

}

1 个答案:

答案 0 :(得分:0)

试试这个,看看你在不断发送时是否收到任何数据?然后添加帖子。

Imports System
Imports System.IO.Ports
Class PortDataReceived
    Public Shared Sub Main()
        Dim mySerialPort As New SerialPort("COM1")

        mySerialPort.BaudRate = 9600
        mySerialPort.Parity = Parity.None
        mySerialPort.StopBits = StopBits.One
        mySerialPort.DataBits = 8
        mySerialPort.Handshake = Handshake.None
        mySerialPort.RtsEnable = True

        AddHandler mySerialPort.DataReceived, AddressOf DataReceivedHandler

        mySerialPort.Open()

        Console.WriteLine("Press any key to continue...")
        Console.WriteLine()
        Console.ReadKey()
        mySerialPort.Close()
    End Sub

    Private Shared Sub DataReceivedHandler(
                        sender As Object,
                        e As SerialDataReceivedEventArgs)
        Dim sp As SerialPort = CType(sender, SerialPort)
        Dim indata As String = sp.ReadExisting()
        Console.WriteLine("Data Received:")
        Console.Write(indata)
    End Sub
End Class