【问题标题】:Using data from arduino in winforms在 winforms 中使用来自 arduino 的数据
【发布时间】:2016-05-30 15:59:56
【问题描述】:
using System;
using System.Drawing;
using System.IO.Ports;

using System.Windows.Forms;

namespace ek_zıplama
{
public partial class Form1 : Form
{
    public enum Directions
    {
        right,
        left,
        up,

    }

    private Directions car_direction;

    public SerialPort myPort;


    int G = 15;
    int force;


    bool jump;

   public string DATA;
    public Form1()
    {
        InitializeComponent();

        myPort = new SerialPort();
        myPort.BaudRate = 9600;
        myPort.PortName = "COM6";
        myPort.Open();

        jump = false;
    }


    private void Form1_Load(object sender, EventArgs e)
    {


        moves();

    }

    private void timer1_Tick(object sender, EventArgs e)
    {


        if (jump)
        {
            car.Top -= force;
            force -= 1;
        }



       //using block to stay in same position when car is stopped
        if (car.Left + car.Width - 1 > block.Left && car.Left + car.Width + 5 < block.Left + block.Width + car.Width
            && car.Top + car.Height >= block.Top && car.Top < block.Top)
        {
            car.Top = ekran.Height - block.Height - car.Height;
            force = 0;
            jump = false;
        }


    }




    private void moves()
    {

        if (label2.Text == "10111" && car_direction != Directions.right)
        {
            car.Location = new Point(car.Location.X + 130, car.Location.Y);
            car_direction = Directions.right;
        }

        if (label2.Text == "01111" && car_direction != Directions.left)
        {
            car.Location = new Point(car.Location.X - 130, car.Location.Y);
            car_direction = Directions.left;
        }

        if (!jump && label2.Text == "11011")
        {
            jump = true;
            force = G;
        }

    }

    private void timer3_Tick(object sender, EventArgs e)
    {

        DATA = myPort.ReadExisting();

        label2.Text = DATA;


    }
  }
 }

我在移动功能中搞砸了。 我试图创建一个像 *如果 DATA 是 01111 那么我的车左转 *如果 DATA 是 10111 那么我的车右转 *如果 DATA 是 11011 那么我的车会跳起来

在我改变之前,汽车是用键盘控制的。一切都一样,但“移动”功能。它正在工作。什么是“移动”功能是:

   private void Form1_KeyDown(object sender, KeyEventArgs e)
{

    if (e.KeyCode == Keys.Right&& car_direction != Directions.right)
    {
        car.Location = new Point(car.Location.X + 130, car.Location.Y);
        car_direction = Directions.right;
    }

    if (e.KeyCode == Keys.Left && car_direction != Directions.left)
    {
        car.Location = new Point(car.Location.X - 130, car.Location.Y);
        car_direction = Directions.left;
    }

    if (!jump && e.KeyCode == Keys.Up)
    {
        jump = true;
        force = G;
    }

}

希望你能得到我:)

【问题讨论】:

  • 按键控制听起来不错。你为什么改变它,你想达到什么目的?

标签: c# winforms visual-studio arduino


【解决方案1】:

我很确定如果您只在 Form_Load 事件中调用它一次,它就不会起作用。由于您可能希望您的car 在每次标签中的数据发生更改时移动,您应该只引用您创建的函数。这意味着,您应该将moves(); 放入private void timer3_Tick(object sender, EventArgs e)

private void timer3_Tick(object sender, EventArgs e)
{

    DATA = myPort.ReadExisting();

    label2.Text = DATA;

    moves();
}

这样,每次计时器计时,你的车就会根据标签中的新数据移动。

【讨论】:

  • 抱歉没用。但非常感谢您的关注 :)
  • 我试图向您询问有关您在 cmets 中的代码的更多信息,因为我不确定一切如何运作。也许你想调试一下......为什么你甚至需要设置标签文本,因为你已经把那个 DATA 字符串全球化了。无论如何,您确定它会返回您正在寻找的正确代码吗? if 语句是否得到满足?
  • 因为首先我只是想确定我是否从 arduino 收到数据。我认为我的问题是将我的数据转换为某些东西但仍然不确定。
  • 应该是。仔细查看您接收数据的方式及其格式。
  • 它是字符串。所以我应该转换成 Int16,Int32 还是什么?谢谢
【解决方案2】:

当我用 Arduino 做某事时,我使用了这样的东西:

private void timer_Tick(object sender, EventArgs e)
{
    if (serialPort != null && serialPort.IsOpen && serialPort.BytesToRead > 0)
    {
        data_as_string += serialPort.ReadLine();
    }
}

我不确定DATA 是如何发送的,但您可以使用Serial.write("your line here"); 从Arduino 发送单行代码。从这里接收和解析数据非常简单,因为您可以读取0111110111 等行并使用任何逻辑来操纵汽车。我希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多