按住按钮继续串行发送变量



我正在尝试做一个与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
}
}
}

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

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;
}

最新更新