serial port freezing when use it with Arduino

Haider92

New member
Joined
Feb 11, 2015
Messages
1
Programming Experience
Beginner
hi everyone

im trying to make an Arduino project
send some sensors read from Arduino via serial port to PC (send as string)
then receive it and split the reading values :biggrin:

i send this string from arduino <1,2,3,4,5,6,7,8,9>

after i receive it via serial port in PC

i split it into
A1=1 , A2=2 , A3 = 3 ....etc
A1 might temp sensor or water level sensor


arduino is an microcontroler and it's work in infintie loop cycle to execute the instruction and its send the sensors reading every xx milliseconds or xx seconds

because of this the visual basic program also should read value from the serial frequently to update the values
to do that i use timer

buy when i compile the program the GUI freezing and i cant click on anything
also when i movie the cruiser the program work pnly by reading from serial port and update the A1 , A2 but GUI still freezing

this is the arduino code to generate random sensors value for test

VB.NET:
char dataPacket[64];

int MQ2 = 1; // 2 char
int MQ9 = 1; // 3 char
int AirQuality = 1; // 1 char
int HCHO = 1; // 2 char
int TMP = 1; // 2 char
int HUMB = 1; // 2 char
int TMPP = 1; // 4 char
int NOISE = 1; // 3 char
int SOIL = 1; // 3 char
int a=0;
int b=0;
void setup() 
{
  Serial.begin(9600);

}

void loop() {

 
  //sprintf(dataPacket, "X%d" ,gx);
    sprintf(dataPacket,"%d,%d,%d,%d,%d,%d,%d,%d,%d",MQ2, MQ9, AirQuality, HCHO, TMP, HUMB, TMPP, NOISE, SOIL);
      Serial.println(dataPacket);
      delay(500);
      
      if(a==0)
      {
       
   MQ2++; 
   MQ9++;
   AirQuality++;
   HCHO++;
   TMP++;
   HUMB++;
   TMPP++;
   NOISE++;
   SOIL++;
   b++;
  if(b==10){
   a=1;
   b=0;
  } 
  }
  
      
      if(a==1){
   MQ2--;
   MQ9--;
   AirQuality--;
   HCHO--;
   TMP--;
   HUMB--;
   TMPP--;
   NOISE--;
   SOIL--;
     b++;
     if(b==10){
   a=0;
   b=0;
      }
}
}


and this is the source code for vb.net

VB.NET:
Imports System.IO.Ports

Public Class frmMain
    Dim mq2max As Integer
    Dim mq2min As Integer
    Dim mq2aver As Integer
    Dim mq5max As Integer
    Dim mq5min As Integer
    Dim mq5aver As Integer
    Dim mq9max As Integer
    Dim mq9min As Integer
    Dim mq9aver As Integer
    Dim hchomax As Integer
    Dim hchomin As Integer
    Dim hchoaver As Integer
    Dim airtempmax As Integer
    Dim airtempmin As Integer
    Dim airtempaver As Integer
    Dim airhumimax As Integer
    Dim airhumimin As Integer
    Dim airhumiaver As Integer
    Dim liqtempmax As Integer
    Dim liqtempmin As Integer
    Dim liqtempaver As Integer
    Dim moismax As Integer
    Dim moismin As Integer
    Dim moisaver As Integer
     

    Dim a As Integer
    Dim A1 As Integer
    Dim A2 As Integer
    Dim A3 As Integer
    Dim A4 As Integer
    Dim A5 As Integer
    Dim A6 As Integer
    Dim A7 As Integer
    Dim A8 As Integer
    Dim A9 As Integer

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        SerialPort.PortName = "COM6"
        SerialPort.BaudRate = 9600
        SerialPort.DataBits = 8
        SerialPort.Parity = Parity.None
        SerialPort.StopBits = StopBits.One
        SerialPort.Handshake = Handshake.None
        SerialPort.Encoding = System.Text.Encoding.Default
        SerialPort.Open()
        SerialPort.DiscardInBuffer()
        SerialPort.DiscardOutBuffer()

    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
       
        If (A1 > mq2max) Then
            mq2max = A1
            Label21.Text = mq2max
        End If
        If (A1 < mq2min) Then
            Label19.Text = A1
        End If

        Chart1.ChartAreas(0).AxisX.Maximum = 100
        Chart1.ChartAreas(0).AxisY.Maximum = 10
        Chart1.ChartAreas(0).AxisX.Interval = 10
        Chart2.ChartAreas(0).AxisX.Maximum = 100
        Chart2.ChartAreas(0).AxisY.Maximum = 10
        Chart2.ChartAreas(0).AxisX.Interval = 10
        Chart3.ChartAreas(0).AxisX.Maximum = 100
        Chart3.ChartAreas(0).AxisY.Maximum = 10
        Chart3.ChartAreas(0).AxisX.Interval = 10
        Chart4.ChartAreas(0).AxisX.Maximum = 100
        Chart4.ChartAreas(0).AxisY.Maximum = 10
        Chart4.ChartAreas(0).AxisX.Interval = 10
        Chart5.ChartAreas(0).AxisX.Maximum = 100
        Chart5.ChartAreas(0).AxisY.Maximum = 10
        Chart5.ChartAreas(0).AxisX.Interval = 10
        Chart6.ChartAreas(0).AxisX.Maximum = 100
        Chart6.ChartAreas(0).AxisY.Maximum = 10
        Chart6.ChartAreas(0).AxisX.Interval = 10
        Chart7.ChartAreas(0).AxisX.Maximum = 100
        Chart7.ChartAreas(0).AxisY.Maximum = 10
        Chart7.ChartAreas(0).AxisX.Interval = 10
        Chart8.ChartAreas(0).AxisX.Maximum = 100
        Chart8.ChartAreas(0).AxisY.Maximum = 10
        Chart8.ChartAreas(0).AxisX.Interval = 10
        Chart1.Series(0).Points.AddY(A1)
        Chart2.Series(0).Points.AddY(A2)
        Chart3.Series(0).Points.AddY(A3)
        Chart4.Series(0).Points.AddY(A4)
        Chart5.Series(0).Points.AddY(A5)
        Chart6.Series(0).Points.AddY(A6)
        Chart7.Series(0).Points.AddY(A7)
        Chart8.Series(0).Points.AddY(A8)




        Dim aaa As String = SerialPort.ReadLine().ToString
        Label1.Text = aaa
        'SerialPort.Close()




        Dim i As Integer
        Dim aryTextFile() As String
        aryTextFile = aaa.Split(",")

        For i = 0 To UBound(aryTextFile)



        Next i
        A1 = aryTextFile(0)
        A2 = aryTextFile(1)
        A3 = aryTextFile(2)
        A4 = aryTextFile(3)
        A5 = aryTextFile(4)
        A6 = aryTextFile(5)
        A7 = aryTextFile(6)
        A8 = aryTextFile(7)
        A9 = aryTextFile(8)
        Label2.Text = A1
        Label3.Text = A2
        Label4.Text = A3
        Label5.Text = A4
        Label6.Text = A5
        Label7.Text = A6
        Label8.Text = A7
        Label9.Text = A8
        Label10.Text = A9


    End Sub

End Class


to download the complete source code this url below

serial port freezing in VB.NET
 
Last edited:
Back
Top