我正在尝试将数据字节从 Arduino 发送到我的 Visual Basic 应用程序,但它不起作用。我已经尝试了几种解决方案,但我没有取回值,我已经发送到 arduino。
我用于 Visual Basic 应用程序的代码:
Private Sub DataReceivedHandler(sender As Object, e As SerialDataReceivedEventArgs)
Dim bytes As Integer = 6
Dim comBuffer As Byte() = New Byte(bytes - 1) {}
'read the data and store it
SerialPort1.Read(comBuffer, 0, bytes)
Dim aryReceived(10) As Integer
For i As Integer = 1 To (bytes - 1)
aryReceived(i) = comBuffer(i)
Y(i) = aryReceived(i)
Next
a = comBuffer(1)
b = comBuffer(2)
End Sub
Private Sub Verzenden()
Dim waardes() As Byte
waardes = {X(1), X(2), X(3), X(4), X(5), X(6)}
If SerialPort1.IsOpen = True Then
SerialPort1.Write(waardes, 0, waardes.Length)
End If
End Sub
Private Sub VerzendButton_Click(sender As Object, e As EventArgs) Handles VerzendButton.Click
X(1) = 10
X(2) = 11
X(3) = 12
X(4) = 13
X(5) = 14
X(6) = 15
Verzenden()
End Sub
和arduino代码:
int x[9];
void setup() {
Serial.begin(9600);
}
void loop() {
if(Serial.available() >= 6){
for(int i = 1; i < 7; i++){
x[i] = Serial.read();
}
for(int i = 1; i < 7; i++){
Serial.println(x[i]);
}
}
}
我使用以下值向 arduino 发送 6 个字节:10、11、12、13、14 和 15,但有时我必须在调试器中断之前发送两次字节。在 a & b 中返回的值与我发送的不同。发送字节的代码工作正常。
有人能帮我吗?