我有8个传感器我从模拟端口读取值并将它们存储在一个数组中。如何在Visual Basic中读取这些值?
我有一个定时器,从VB向Arduino发送'*',然后Arduino返回传感器值。我尝试通过发送'&'来接收Arduino中的数组数据确保我在数组中输入正确的值。
Arduino代码:
uint8_t i;
unsigned char c;
char data[6];
bool rd = false;
String dataString = "";
int sensor;
uint8_t b;
static const uint8_t analog_pins[] = {A0, A1, A2, A3, A4, A5, A6, A7};
int myVals[7];
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
void setup() {
Serial.begin(9600);
Serial.println(A0);
while (!Serial) {
;
}
if (!SD.begin(chipSelect)) {
return;
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("Gas Sesnor TEST results :");
dataFile.close();
}
}
void loop() {
}
void serialEvent() {
c = Serial.read();
if (c == '*') {
for (b = 0 ; b <= 7 ; b++) {
sensor = analogRead(analog_pins[b]);
myVals[b] = sensor;
Serial.println(sensor);
dataString = String(sensor) + "\n";
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
}
}
if (c == '&') {
for (b = 0; b <= 7; b++) {
Serial.println(myVals[b]);
}
}
}
到目前为止的VB代码:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
SerialPort1.WriteLine("*")
receivedData = ReceiveSerialData()
RichTextBox1.Text = receivedData
RichTextBox1.SelectionStart = RichTextBox1.TextLength
RichTextBox1.ScrollToCaret()
Function ReceiveSerialData() As Integer
Try
Dim Incoming As String
Try
Incoming = SerialPort1.ReadExisting()
If Incoming Is Nothing Then
Return 0
Else
Return Incoming
End If
Catch ex As TimeoutException
Return "Error: Serial Port read timed out."
End Try
Catch ex As Exception
End Try
End Function
它一直返回0.请帮助我。