c#程序控制和arduino rc汽车

时间:2014-07-30 21:33:00

标签: c# serial-port

所以我制作了一个c#程序来通过一个串口来控制我的遥控车,当我按下按钮使汽车向前移动时,当我按下按钮时它继续前进。所以简而言之,我想用箭头键控制按钮,当我按下键时它应该前进然后当我放开它应该停止。在此先感谢您的帮助。你可能会说我是新手。

节目图片:   http://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);
    }

1 个答案:

答案 0 :(得分:0)

你需要一个" Button_Unclick" event将stop命令发送到串口。

你不能说你的表格技术是什么......但是在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);
}

更新:因此,这里的密钥是您在表单上所需要的...是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"
        >

在你的xaml.cs ......

private void MainWindow_OnKeyDown(object sender, KeyEventArgs e)
{
}

private void MainWindow_OnKeyUp(object sender, KeyEventArgs e)
{
}

更新2:以下是检查键值的开关语句..

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