无线串行通信

问题描述 投票:0回答:1

我创建了这个 Arduino 草图,它通过控制台库与我的电脑进行通信,并通过我的网络使用串行监视器。本质上,当我输入数字 1 时,我的 Arduino 步进电机会移动 30 步等。但是,必须在串行监视器中输入很不方便。我希望任何人都知道如何创建可执行的 Windows 窗体或可以通过网络连接到 Arduino Yun 控制台的东西。

#include <Stepper.h>
#include <Console.h>

char incomingByte;      
Stepper stepper(64,8,9,10,11);
int stepCount = 1;

void setup() {

  Bridge.begin();  
  Console.begin(); 
  while (!Console);
  stepper.setSpeed(60);

}

void loop() {
  if (Console.available() > 0) 
  {    

incomingByte = Console.read();

    if (incomingByte == '1') 
    {
    stepCount += 30;
     if (stepCount > 0 && stepCount < 4096)
     {
     stepper.step(30);
     Console.println(stepCount);
     }
    else{stepCount = stepCount - 30;}
    }

    if (incomingByte == '2') 
    {
    stepCount = stepCount - 30;
     if (stepCount > 0 && stepCount < 4096)
     {
     stepper.step(-30);
     Console.println(stepCount);
     }
    else{stepCount += 30;}
    }

    if (incomingByte == '3') 
    {
    stepCount += 200;
     if (stepCount > 0 && stepCount < 4096)
     {
     stepper.step(200);
     Console.println(stepCount);
     }
    else{stepCount = stepCount - 200;}
    }

    if (incomingByte == '4') 
    {
    stepCount = stepCount - 200;
     if (stepCount > 0 && stepCount < 4096)
     {
     stepper.step(-200);
     Console.println(stepCount);
     }
    else{stepCount += 200;}
    }
  }
}

这是我希望能够工作的 Windows 窗体应用程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;


namespace Remote_Focuser
{
    public partial class Form1 : Form
    {
        private SerialPort myPort;
        public Form1()
        {
            Console.Read();
            InitializeComponent();
            Init();
        }

        private void Fwd_30_Button_Click(object sender, EventArgs e)
        {
            myPort.WriteLine("1");
        } 

        private void Backward_30_Button_Click(object sender, EventArgs e)
        {
            myPort.WriteLine("2");
        }

        private void Forward_200_Button_Click(object sender, EventArgs e)
        {
            myPort.WriteLine("3");
        }

        private void Backward_200_Button_Click(object sender, EventArgs e)
        {
            myPort.WriteLine("4");
        }
        private void Init()
        {
            myPort = new SerialPort();
            myPort.BaudRate = 9600;
            myPort.PortName = "10.1.1.211";

            myPort.Open();
        }
    }
}

提前谢谢大家,是的,我可能犯了一个很大的错误,我还不太熟练......:)

c# winforms console serial-port arduino-yun
1个回答
0
投票

你可以使用arduino WebSerial库

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