【发布时间】:2018-04-08 05:17:43
【问题描述】:
我想用 Visual Basic 制作一个串行绘图仪,它可以绘制从 Arduino nano 接收到的数据, 在 arduino 中,我想从模拟引脚 A0 读取数据,然后在 VB 应用程序中绘制它。 我做了一个 arduino 项目和一个 VB 应用程序,但它有问题,所以如果你纠正我,我会很高兴。 请注意,对于这种情况,vb 中的计时器间隔必须是可变的,所以我希望串行端口一直工作,但 vb 随时读取它。 在下面的这段代码中,问题是当我更改计时器间隔并且它与 arduino 延迟不相等时,数据不正确(主要是空数据)并且当 arduino 代码没有任何延迟时,数据完全错误。
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");
}
}
【问题讨论】:
-
您遇到了什么具体问题?您尚未提出问题或说明您遇到了什么问题...
-
@Idle_Mind 我纠正了这个问题
标签: vb.net timer arduino serial-port delay