0
votes

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");
  }
}
1
What specific issue are you having, though? You haven't asked a question or stated what problems you are experiencing...Idle_Mind
@Idle_Mind i corrected the queationSina Ronaghi

1 Answers

0
votes

that's how you make a serial plotter :) Description : When the timer starts the vb app sends '*' to the arduino by COM port as you can see it happens in a timer_tick event so you can change the timer interval for your own use

then the arduino receives the '*' character and enters into SerialEvent() in serialEvent() it reads analog port and returns it to VB app

then the VB app receives it and runs ReceiveSerialData() function and so on...
`

VB CODE :

`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 CODE :

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++;
  }*/