I want to make a serial plotter with Visual Basic that plots received data from an Arduino nano, In arduino i would like to read data from Analog pin A0 then plot it in VB app. I made an arduino project and a VB app but it has problems so I'd be happy if you correct me. Note that the timer interval in vb must be changeable for this case, so i would like the serial port to work all the time but vb reads it whenever i want. on this code below the problem is when i change timer interval and when it's not equal with arduino delay, the data is not correct (mostly null data) and when it's not any delay in arduino code the data is completely false.
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");
}
}