我有一个arduino nano,它的行为就像电压表 它读取ADC(模拟端口A0)然后将数据从串口发送到计算机,我设计了一个可视化的基本程序来读取这些数据并将其绘制在图表中。 但不知何故,vb中的计时器和arduino中的延迟有时不会同步并返回false值。我能做什么 ? False Value's picture
VB侧码:
Private Sub connect_BTN_Click(sender As Object, e As EventArgs) Handles connect_BTN.Click
Try
If (connect_BTN.Text = "Connect") Then
If (comPORT <> "") Then
SerialPort1.Close()
SerialPort1.PortName = comPORT
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default 'very important!
SerialPort1.ReadTimeout = 10000
SerialPort1.Open()
connect_BTN.Text = "Dis-connect"
'Timer1.Enabled = True
'Timer_LBL.Text = "Timer: ON"
STATUS.BackColor = System.Drawing.Color.LightGreen
Else
MsgBox("Select a COM port first")
End If
Else
SerialPort1.Close()
connect_BTN.Text = "Connect"
Timer1.Enabled = False
Timer_LBL.Text = "Timer: OFF"
STATUS.BackColor = System.Drawing.Color.Red
End If
Catch exx As Exception
MsgBox(exx.Message)
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
receivedData = ReceiveSerialData()
RichTextBox1.Text += receivedData & vbCrLf
Dim meghdar As Integer = Convert.ToInt32(receivedData)
s.Points.AddY(meghdar)
End Sub
Function ReceiveSerialData() As String
Try
Dim Incoming As String
Try
Incoming = SerialPort1.ReadExisting()
If Incoming Is Nothing Then
Return 0
Else
Return Convert.ToInt32(Incoming)
End If
Catch ex As TimeoutException
Return "Error: Serial Port read timed out."
End Try
Catch ex As Exception
End Try
Arduino Code :
/*
SD card datalogger
The circuit:
* analog sensors on analog ins 0, 1, and 2
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//Serial.print("Initializing SD card...");
// see if the card is present and can be init-ialized:
if (!SD.begin(chipSelect)) {
//Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
// Serial.println("card initialized.");
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("ConePenetroMeter TEST results :");
dataFile.close();
// print to the serial port too:
//Serial.println("ConePenetroMeter TEST results :");
}
}
void loop() {
// make a string for assembling the data to log:
String dataString = "";
// read three sensors and append to the string:
int sensor = analogRead(A0);
dataString += String(sensor);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(sensor);
}
// if the file isn't open, pop up an error:
else {
//Serial.println("error opening datalog.txt");
}
delay(300);
}
arduino的延迟是300毫秒,vb.net中的定时器间隔也是300毫秒