c# - 按住按钮以继续串行发送变量
问题描述
我正在尝试做一个与 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;
}
推荐阅读
- python - 间歇性 TabError:缩进中制表符和空格的不一致使用(仅在 CircleCI 上)
- java - Java 的“扫描仪”方法与门面 GoF 设计模式
- excel - 当 A1 和 B1 匹配 A2 和 B2 时,尝试连接 C 列中的值
- jenkins - jenkins pipeline groovy 如何在 M 台机器上并行运行 N 个测试
- r - 有条件地替换数据框中的值
- c# - 为什么这个 ulong 除以 ulong 给我 0?
- javascript - 我们可以在没有 express 和 bodyParser 的情况下使用 nodemailer
- bigcommerce - BigCommerce 自动 moxieplayer 包裹在 tinymce
- javascript - 多选选项返回 React 中两个选择的结果
- javascript - DataTable如何定义多个初始化参数?