gpt4 book ai didi

c++ - VB.NET:未知的 KeyDown 事件问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:52:24 24 4
gpt4 key购买 nike

编辑:最终解决方案:

鉴于最终解决方案被埋没在评论中,我将其放在这里以供 future 的观众使用。

我的问题是我用来停止 Action 的信号包括“r”,还有停止命令(我认为我很聪明,结合了我的“左”命令和“停止”命令)。 C++ (arduino) 代码单独读取每个字符,因此完全停止信号,而不仅仅是我想要的信号。

感谢 dbasnett 指出这一点:)

好的,大家好,这是我对我的这个最新项目的最后一个问题。这个想法很简单,就是制作一个程序来通过 arduino 控制遥控车。有用!!! (只有一个小错误)。

简单地说,我使用方向键或 WASD 来控制程序。它通过串行端口向 arduino 发送信号,arduino 中有一些函数用于解释信号。您总共可以发送 9 个信号;一个用于每个方向,一个用于停止每个方向输入(例如,left 使汽车指向左轮,stop-left 将轮子指向正前方)。

问题是:当我同时前进(或后退)和左(或右)时,如果我改变方向(左/右),它会一起停止。我花了项目的大部分时间来解决这个问题,但我看不出我做错了什么。

所以,我的代码:

Imports System.IO.Ports
Imports System.IO
Imports System.Threading

Public Class Form1

Shared _continue As Boolean
Shared _serialPort As SerialPort
Shared lturn As Boolean
Shared rturn As Boolean
Shared keydelay As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SerialPort1.PortName = "com3" 'change com port to match your Arduino port
SerialPort1.BaudRate = 115200
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!

End Sub

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

Dim bHandled As Boolean = False

If SerialPort1.IsOpen Then
Else
SerialPort1.Open()
End If

Select Case e.KeyCode

Case Keys.Right

pbBgML.BackColor = Color.Transparent
pbBgMR.BackColor = Color.Black
SerialPort1.Write("d")

e.Handled = True

Case Keys.Left

pbBgMR.BackColor = Color.Transparent
pbBgML.BackColor = Color.Black
SerialPort1.Write("a")

e.Handled = True

Case Keys.Up

pbBgBC.BackColor = Color.Transparent
pbBgTC.BackColor = Color.Black
SerialPort1.Write("w")

e.Handled = True

Case Keys.Down

pbBgTC.BackColor = Color.Transparent
pbBgBC.BackColor = Color.Black
SerialPort1.Write("s")

e.Handled = True

Case Keys.Space

SerialPort1.Write("r")

e.Handled = True

End Select
End Sub

Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyUp

Select Case e.KeyCode

Case Keys.Right
pbBgMR.BackColor = Color.Transparent
SerialPort1.Write("dr")

Case Keys.Left
pbBgML.BackColor = Color.Transparent
SerialPort1.Write("ar")

Case Keys.Up
pbBgTC.BackColor = Color.Transparent
SerialPort1.Write("wr")

Case Keys.Down
pbBgBC.BackColor = Color.Transparent
SerialPort1.Write("sr")

End Select

End Sub

End Class

此外,如果需要,arduino 代码(C++):

    // Car Control v. 0.2

int reversePin = 9;
int forwardPin = 8;
int leftPin = 10;
int rightPin = 11;

byte byteRead;
int time;

void forward(int time){
digitalWrite(reversePin, HIGH);
Serial.println("This is forward...");
digitalWrite(forwardPin, LOW);
delay(time);
}

void reverse(int time){
digitalWrite(forwardPin, HIGH);
Serial.println("This is reverse...");
digitalWrite(reversePin, LOW);
delay(time);
}

void left(int time){
digitalWrite(rightPin, HIGH);
Serial.println("This is left...");
digitalWrite(leftPin, LOW);
delay(time);
}

void right(int time){
digitalWrite(leftPin, HIGH);
Serial.println("This is right...");
digitalWrite(rightPin, LOW);
delay(time);
}

void off(){
Serial.println("This is stop...");
digitalWrite(leftPin, HIGH);
digitalWrite(rightPin, HIGH);
digitalWrite(reversePin, HIGH);
digitalWrite(forwardPin, HIGH);
}

void setup() {
// initialize the digital pins as an output.
pinMode(rightPin, OUTPUT);
pinMode(leftPin, OUTPUT);
pinMode(forwardPin, OUTPUT);
pinMode(reversePin, OUTPUT);

Serial.begin(115200);
Serial.print("\n\nStart...\n");
}

void loop()
{
//Turn everything off...
if (Serial.available()) {
/* read the most recent byte */
byteRead = Serial.read();

switch(byteRead)
{
case 'w':
forward(5);
break;

case 'wr':
digitalWrite(forwardPin, HIGH);
break;

case 's':
reverse(5);
break;

case 'sr':
digitalWrite(reversePin, HIGH);
break;

case 'a':
left(5);
break;

case 'ar':
digitalWrite(leftPin, HIGH);
break;

case 'd':
right(5);
break;

case 'dr':
digitalWrite(rightPin, HIGH);
break;

case 'r':
off();
break;

}
}
}

对于大段文字,我深表歉意,但我根本不知道问题出在哪里。干杯伙计们。

最佳答案

我帮不上 C 代码,但 VB 代码可以使用单个字节来控制

<FlagsAttribute()> _
Enum ctrl As Byte
stp = 0 'stop is no bits set
frwd = 1 << 0 '1
back = 1 << 1 '2
left = 1 << 2 '4
rght = 1 << 3 '8
mask = 255
End Enum

Private _control(0) As ctrl

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.Right
_control(0) = _control(0) Or ctrl.rght
Case Keys.Left
_control(0) = _control(0) Or ctrl.left
Case Keys.Up
_control(0) = _control(0) Or ctrl.frwd
Case Keys.Down
_control(0) = _control(0) Or ctrl.back
End Select
'SerialPort1.Write(_control, 0, 1)
Debug.WriteLine(_control(0))
End Sub

Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case Keys.Right
_control(0) = _control(0) And (ctrl.mask Xor ctrl.rght)
Case Keys.Left
_control(0) = _control(0) And (ctrl.mask Xor ctrl.left)
Case Keys.Up
_control(0) = _control(0) And (ctrl.mask Xor ctrl.frwd)
Case Keys.Down
_control(0) = _control(0) And (ctrl.mask Xor ctrl.back)
End Select
'SerialPort1.Write(_control, 0, 1)
Debug.WriteLine(_control(0))
End Sub

关于c++ - VB.NET:未知的 KeyDown 事件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22958354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com