2019年12月19日 星期四

VB2010與Arduino Com port Test

VB2010與Arduino  Com port Test 












===================Arduino ===========================
String datos;

// Setup
void setup()
{
  // set baud rate
  Serial.begin(9600);
}

// Main loop
void loop()
{
  datos = "HelloWord_123";
  delay(100);
  char charBuf[50];
  datos.toCharArray(charBuf, 50);
  delay(100);
  Serial.write(charBuf);
  delay(100);
  Serial.println(""); // to jump lines
  delay(100);
}

===================VB2010 ===========================

Imports System.IO
Imports System.IO.Ports

Public Class Form1

    Dim sports As String() = System.IO.Ports.SerialPort.GetPortNames
    Delegate Sub meumetodo(ByVal [text] As String)
    Dim subrotinaSTR As New meumetodo(AddressOf tratamentoSTR)

    'choose  a Serial Port

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim sports As String() = System.IO.Ports.SerialPort.GetPortNames


        If sports Is Nothing Then
            MsgBox("Can't find Serial Ports in this computer.")
        End If

        For i = 0 To UBound(sports)
            ComboBox1.Items.Add(sports(i))
            ComboBox1.SelectedIndex = 0
        Next

    End Sub



    ' to connect with Arduino

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try

            SerialPort1.PortName = ComboBox1.SelectedItem
            SerialPort1.BaudRate = 9600
            SerialPort1.DataBits = 8
            SerialPort1.Parity = IO.Ports.Parity.None
            SerialPort1.StopBits = IO.Ports.StopBits.One

            SerialPort1.Open()
            MsgBox("Connected.")


        Catch ex As Exception
            MsgBox("Can't connect.")
        End Try
    End Sub


    ' get data received from Arduino

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

        Dim string1 As String = SerialPort1.ReadExisting()
        Invoke(subrotinaSTR, string1)

    End Sub

    ' Display data received from Arduino


    Sub tratamentoSTR(ByVal meustring As String)
        TextBox1.Text = meustring


    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class




沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...