c#程序控制和arduino遥控小车
本文关键字:小车 arduino 程序控制 | 更新日期: 2023-09-27 18:15:38
所以我做了一个c#程序来控制我的RC汽车通过串行端口,当我按下按钮,使汽车向前移动,它继续前进,当我按下按钮。简而言之,我想用方向键控制按钮,当我按下键时,它应该向前走,当我放开它时,它应该停止。提前感谢你的帮助。你可能会觉得我是个新手。
程序图片:https://i.stack.imgur.com/yV8wa.jpg
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;
namespace CSharpEasySerial
{
public partial class frmSerial : Form
{
public static System.IO.Ports.SerialPort serialPort1;
private delegate void LineReceivedEvent(string line);
public frmSerial()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
try
{
System.ComponentModel.IContainer components = new System.ComponentModel.Container();
serialPort1 = new System.IO.Ports.SerialPort(components); // Creating the new object.
serialPort1.PortName = "COM" + numCom.Value.ToString(); // Setting what port number.
serialPort1.BaudRate = 9600; // Setting baudrate.
serialPort1.DtrEnable = true; // Enable the Data Terminal Ready
serialPort1.Open(); // Open the port for use.
btnConnect.Text = "Connected.";
btnConnect.Enabled = false;
numCom.Enabled = false;
serialPort1.Write(new byte[] { 0x08 }, 0, 1);
}
catch
{
MessageBox.Show("Not Found. Try Again","Error");
}
}
private void btnSend_Click(object sender, EventArgs e)
{
// Sends the text as a byte.
serialPort1.Write(new byte[] { Convert.ToByte(txtDatasend.Text) }, 0, 1);
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.Write(new byte[] { 0x00 }, 0, 1);
serialPort1.Write(new byte[] { 0x02 }, 0, 1);
}
您需要一个"Button_Unclick"事件来将停止命令发送到串行端口。
你没有说你的表单技术是什么…然而,在WPF中,这可能采取…private void button1_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// whatever command it is to stop... your the expert here...
serialPort1.Write(new byte[] { 0x08 }, 0, 1);
}
Update:所以对于键,这里是你需要在你的表单…是winforms吗?WPF吗?这是我测试它的winforms应用程序(因为这是我现在打开的):)
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += form_KeyDown;
this.KeyUp += form_KeyUp;
}
private void form_KeyUp(object sender, KeyEventArgs e)
{
Console.WriteLine(@"KeyUp:"+ e.KeyValue);
// user released the key so send whatever you need to stop...
}
private void form_KeyDown(object sender, KeyEventArgs e)
{
Console.WriteLine(@"KeyDown:"+ e.KeyValue);
// user pressed the key so send whatever you need to make it go..
}
更新2:这里是WPF的方法/设置
在你的window.xaml…
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
KeyDown="MainWindow_OnKeyDown"
KeyUp="MainWindow_OnKeyUp"
>
在你的例子中…
private void MainWindow_OnKeyDown(object sender, KeyEventArgs e)
{
}
private void MainWindow_OnKeyUp(object sender, KeyEventArgs e)
{
}
更新2:下面是检查键值的switch语句。
private void form_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
// do your up arrow thing here...
break;
case Keys.Down:
if (e.Control)
{
// get fancy if the control key + down arrow is pressed
// at the same time
}
else
{
// do the normal down arrow stuff
}
break;
}
}