我想用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");
}
}
答案 0 :(得分:0)
这就是你如何制作一个连续绘图仪:) 说明: 当计时器启动时,vb app通过COM端口将'*'发送到arduino 正如您所看到的那样,它发生在timer_tick事件中,因此您可以更改自己使用的计时器间隔
然后arduino收到'*'字符并进入SerialEvent() 在serialEvent()中,它读取模拟端口并将其返回给VB app
然后VB应用程序接收它并运行ReceiveSerialData()函数等等...
`
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 = "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
row += 1
RichTextBox1.Text += receivedData
RichTextBox1.SelectionStart = RichTextBox1.TextLength
RichTextBox1.ScrollToCaret()
s.Points.AddY(receivedData)
End Sub
Function ReceiveSerialData() As Integer
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
End Function
ARDUINO代码:
uint8_t i;
unsigned char c;
char data[6];
bool rd=false;
String dataString = "";
int sensor;
#include <SPI.h>
#include <SD.h>
const int chipSelect = 4;
void setup() {
Serial.begin(9600);
while (!Serial){;}
if (!SD.begin(chipSelect)) {
return;
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println("ConePenetroMeter TEST results :");
dataFile.close();
}
}
void serialEvent()
{
c = Serial.read();
if (c == '*')
{
sensor = analogRead(A7);
Serial.println(sensor);
dataString = String(sensor) + "\n";
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
//memset(&data[0],'\0',6);
//i = 0;
}}
/*else
{
data[i] = c;
i++;
}*/