首页 > 解决方案 > 按住按钮以继续串行发送变量

问题描述

我正在尝试做一个与 arduino 通信的应用程序,它需要来自串行的输入来移动伺服。我做了一些东西,但我需要继续单击按钮以将变量发送到串行,但我需要它像按钮一样工作,所以如果我按住它,它会继续向串行发送变量。

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;

namespace test
{
    public partial class Form1 : Form
    {
        
        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.Open();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            serialPort1.Close();
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            serialPort1.Write("z");
        }

        private void button4_MouseDown(object sender, MouseEventArgs e)
        {
            serialPort1.Write("c");
        }

        private void button1_MoseDown(object sender, MouseEventArgs e)
        {
            serialPort1.Write("a");
        }
                
        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            serialPort1.Write("d");
        }

       
    }
}

这是arduino

#include<Servo.h> // include server library
Servo ser;
Servo ser1;// create servo object to control a servo
int poser = 0; // initial position of server
int val; // initial value of input

void setup() {
  Serial.begin(9600); // Serial comm begin at 9600bps
  ser.attach(9);// server is connected at pin 9
  
}

void loop() {
  if (Serial.available()) // if serial value is available 
  {
    val = Serial.read();// then read the serial value
    if (val == 'd') //if value input is equals to d
    {
      poser += 1; //than position of servo motor increases by 1 ( anti clockwise)
      ser.write(poser);// the servo will move according to position 
      delay(10);//delay for the servo to get to the position
     }
    if (val == 'a') //if value input is equals to a
    {
      poser -= 1; //than position of servo motor decreases by 1 (clockwise)
      ser.write(poser);// the servo will move according to position 
      delay(10);//delay for the servo to get to the position
    }
     if (val == 'c') //if value input is equals to d
    {
      poser += 10; //than position of servo motor increases by 1 ( anti clockwise)
      ser.write(poser);// the servo will move according to position 
      delay(10);//delay for the servo to get to the position
  }
  if (val == 'z') //if value input is equals to a
    {
      poser -= 10; //than position of servo motor decreases by 1 (clockwise)
      ser.write(poser);// the servo will move according to position 
      delay(10);//delay for the servo to get to the position
    }
}
}

标签: c#loopsbutton

解决方案


我建议使用带有取消令牌的任务。从如何:取消任务及其子项

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

namespace test
{
    public partial class Form1 : Form
    {
      CancellationTokenSource taskATokenSource = new CancellationTokenSource();

      private void button1_MoseDown(object sender, MouseEventArgs e)
      {
        Task.Run(TaskA(taskATokenSource.Token), taskATokenSource.Token);
      }

      private void button1_MoseUp(object sender, MouseEventArgs e)
      {
        taskATokenSource.Cancel();
      }

      private Action TaskA(CancellationToken ct)
      {
        //For example only
        //Needs slowed down or some other way to prevent input buffer from overflowing.
        while (!ct.IsCancellationRequested)
        {
            serialPort1.Write("a");
        }

        return null;
      }

推荐阅读