无线串行通信

时间:2017-07-24 23:43:46

标签: c# winforms console serial-port arduino-yun

我创建了这个Arduino草图,它通过Console库与我的电脑通信,并通过我的网络使用串行监视器。基本上,当我输入数字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();
        }
    }
}

先谢谢大堆,是的,我可能犯了一个大错,我还不太精通...... :)

0 个答案:

没有答案