直接从.net应用程序与USB端口通信并控制当前流量(打开和关闭)

问题描述 投票:-3回答:1

我想直接从.net应用程序与端口进行通信并控制当前的流程...这样我就可以直接打开或关闭步进电机或c#程序中的任何东西。基本上我想使用串口等USB端口......我也想知道如何使用arduino完成这项工作。

c# .net usb
1个回答
0
投票

我想你可以使用SerialPortfrom System.IO.Ports。至于arduino,使用Serial静态类来写/读端口。

这是一个小草图,说明如何继续(使用Windows窗体应用程序)。

.NET(txtCom as TextBox和btnSend as Button

Imports System.IO.Ports
Public Class SerialCom
    Dim WithEvents SP As New SerialPort("COM5", 9600)
    Private Sub SerialCom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SP.Open()
    End Sub

    Private Sub SP_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SP.DataReceived
        Dim dataR As String = SP.ReadExisting
        Me.Invoke(New updateText(AddressOf updateText_s), dataR)
    End Sub

    Public Delegate Sub updateText(ByVal line As String)
    Sub updateText_s(ByVal line As String)
        txtCom.AppendText(line)
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        SP.Write("Welcome")
    End Sub
End Class

Arduino的

void setup() {
  Serial.begin(9600);
  Serial.setTimeout(200); // Wait 200ms max. when Serial.readString() is called
}

void loop() {
  Serial.println("CONNECTION");
  delay(1000);
  Serial.println("+OK");
  delay(1000);
  String rec;
  rec=Serial.readString();
  Serial.println("Received:'"+rec+"'"); // Send back what we received
}

注意:要检查COM端口ID,您可以在Windows上{CTRL} + {BEAK},然后打开设备管理器并在PORT子树下查找Arduino。在启动.NET应用程序之前,不要忘记连接您的arduino。

Arduino上的串行通信:https://www.arduino.cc/reference/en/language/functions/communication/serial/

© www.soinside.com 2019 - 2024. All rights reserved.