serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
Go Back  Xtreme Visual Basic Talk > > > serial port freezing in VB.NET when use it with Arduino


Reply
 
Thread Tools Display Modes
  #1  
Old 02-11-2015, 03:33 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Angry serial port freezing in VB.NET when use it with Arduino


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

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 only 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

Code:
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

Code:
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

http://www.gulfup.com/?euQbry

Last edited by haider; 02-11-2015 at 03:44 AM.
Reply With Quote
  #2  
Old 02-11-2015, 08:31 PM
passel's Avatar
passelserial port freezing in VB.NET when use it with Arduino passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

I would put all the code in the timer tick function, except for the Serial port read, in a sub (for example called parseSerial).
Code:
    Sub parseSerial(aaa as String) 'pass the line from the serial port into this sub
        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)


        Label1.Text = aaa
        'SerialPort.Close()

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

        For i = 0 To UBound(aryTextFile)

'Why an empty loop here???

        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
Then, I would read a line from the serial port in the SerialPort DataReceived handler, which will occur on a separate thread, and not hang up the GUI.
You'll have to Invoke back to the GUI thread to run the sub above to access your controls.
Code:
Private Sub DataReceivedHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
    Dim sp As SerialPort = CType(sender, SerialPort)
 
    Dim s As String = sp.ReadLine().ToString
    Me.Invoke(Sub() parseSerial(s))
End Sub
Didn't test it, but I think it is coded correctly and should work.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 02-12-2015, 12:23 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

thanks passel for reply

the code is work fine

i remember when i join this forum first time and make my first thread about making program to read file and story it in binary array to send it via serial port bit by bit
in that time you help me and post many time

by the way i remove the timer and the program still work very well
can you tell me whats happen
we just put the code in function and call it
why the program is not hanging like when we use timer before

can you explain that i want to learn more about vb.net

thank you again to solve my problem

my regards
Reply With Quote
  #4  
Old 02-12-2015, 06:27 AM
passel's Avatar
passelserial port freezing in VB.NET when use it with Arduino passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

Serial Port communications is relatively slow, so when you do a ReadLine, the thread is stopped at that point waiting for the complete line to arrive.
If you are doing that read in the GUI thread, then the GUI (your form) is stalled for that period so can't respond to user input, or refresh the display if needed, etc.
The Timer Event handler is processed on the GUI thread (which is why you can update controls in the Timer Event).
The Timer interval was probably set to a short interval to be responsive to the serial port, which means that as soon as you finished doing the ReadLine, and processed the data, the next timer event was already waiting, as you exited the timer event sub, so you immediately got another timer event, came back in and sat at the next ReadLine.
The end result, you were fairly constantly spending all your GUI thread time sitting on the ReadLine, and you couldn't do anything else (or very little else) with the GUI.

The DataReceived event handler, on the other hand, is not processed by the GUI thread. The serialport object creates a new background thread and raises the event on that thread.
Now you will still be doing a ReadLine in that event handler, and waiting for the the whole line to get there as before, but since it is being done in another process thread, that thread is stalled, but your GUI thread is still running, responding to buttons, etc...

Since your GUI thread is active, and can be accessing controls and making modifications, other threads, like the one your DataReceived event handler is running in, can't also modify the controls in parallel, or you can have issues, so VB will by default not allow you to do that. You will get an exception if you called the parseSerial sub directly from the event handler. You can try that to see what the error looks like, just comment out the Me.Invoke line, and do the parseSerial(s) on a line directly.

Invoke expects a delegate and will schedule a method to be called through that Delegate. A Delegate is essentially a description of the interface to a method and the address of the method, and by Invoking the Delegate, you call the method associated with the Delegate indirectly. You can modify a Delegate to "point" to different methods at different times.
In this case, the delegate is used to allow the method to be called later on the GUI thread, so the method has access to the GUI controls and and other objects.

You could create the Delegate and associate it with the sub as a separate thing done at initialization, and if you look up VB.Net delegates I'm sure you'll see plenty of examples of that.
But the method used here is a shortcut method which I like to use because of the simplicity, of just doing an Invoke using the form object, and having it generate the Delegate anonymously (unnamed delegate) in line with the (Sub() .....) syntax. Very convenient for this common type of need.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 02-12-2015 at 06:36 AM.
Reply With Quote
  #5  
Old 02-14-2015, 01:36 PM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

thank you very much this details
Reply With Quote
  #6  
Old 02-18-2015, 04:44 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

hi Passel

i have problem but this time with cancel the program

when i want to close the program the GUI freezing again and i don't know why
if i use events like FormClosed to close Serial port thought is behind this problem
nothing happen till with do that

this is my code

Code:
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
   
    Sub parseSerial(aaa As String) 'pass the line from the serial port into this sub
        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)


        Label1.Text = aaa
        'SerialPort.Close()

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

        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
    Private Sub DataReceivedHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
        Dim sp As SerialPort = CType(sender, SerialPort)

        Dim s As String = sp.ReadLine().ToString
        Me.Invoke(Sub() parseSerial(s))
    End Sub

    Private Sub frmMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        SerialPort.Close()

    End Sub
End Class

Last edited by haider; 02-18-2015 at 04:54 AM.
Reply With Quote
  #7  
Old 02-18-2015, 08:24 AM
passel's Avatar
passelserial port freezing in VB.NET when use it with Arduino passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

I'm not sure, but it is probably the ReadLine or the Invoke in the DataReceived handler, that is keeping things from closing.

You'll probably need a boolean that you set when closing, and check for that in the DataReceived handler so that you can skip any object processing if you're in the process of exiting.

Instead of using the FormClosed event, use the FormClosing event so you can cancel the close the first time, and hopefully the DataReceived handler will issue a second close when it processes its last line.
If the serial port is not active, then you may need to click the close a second time yourself, which will close the form because the leaving flag will be set True the second time the FormClosing event occurs.
Code:
'in the forms declarations area at the top of the file
  Private leaving As Boolean

  Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If Not leaving Then 'if we haven't set the leaving flag
      leaving = True    '  set it
      e.Cancel = True   '  Cancel then current closing to allow background tasks to exit
    Else
      SerialPort1.Close()   'we're on our way out, close the port
    End If
  End Sub

  Private Sub Serialport1DataReceiveHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    Dim sp As SerialPort = CType(sender, SerialPort)
    Dim s As String
    If Not leaving Then
      s = sp.ReadLine().ToString
    End If
    If Not leaving Then
      Me.Invoke(Sub() parseSerial(s))
    End If
    If leaving Then
      Me.BeginInvoke(Sub() Close())    'reissue a close command to the form
    End If
  End Sub
You might want to try commenting out the Else in the FormClosing event, so that the SerialPort is closed on the first closing event. I don't know if that will abort a ReadLine, if one is in progress, or prevent another DataReceived event if we are not already in one, in which case you won't get a second closing event from there.

You could enable a timer, that has a Close() in it, so if you don't receive a second close from the DataReceived event within a short period, say 200ms, you have the timer as a backup which will issue the second Close. Of course, you can always get the second close as already mentioned, by the user hitting the close button again if the form doesn't close the first time.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #8  
Old 02-18-2015, 01:50 PM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

this code doen't work
but it's not very important i compared to this problem

when i want to send serial of string from arduino like
aaa "<13,17,9,13,2320,4480,2212,1,0,-26214> " String

i got an error


An exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll but was not handled in user code

Additional information: Conversion from string "<13" to type 'Integer' is not valid.


i try to rename A1,A2.... to Byte or String but the problem still the same

in the past time i just make simple program in arduino to send number generated by loop from 1 to 10 so that's why i don't realise the problem in that time
Reply With Quote
  #9  
Old 02-19-2015, 01:31 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

i found the solve

yesterday after i post i was Vigil till more than midnight searching about the problem

but the electric shutdown suddenly in my city because of sudden heavy rain

i wake up late and continue searching again

and find this code

Code:
A1 = Convert.ToInt32(Convert.ToDecimal(aryTextFile(0)))
and use it it work done

by the way can i ask another question while this thread open

i have chart graph in the program

when i plot line and it reach the limit of chart it stop plotting and i want it to look continue plotting
i search in chart option to find the solution but i didn't anything
Reply With Quote
  #10  
Old 02-20-2015, 09:31 PM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

hi parsel
i solve most of my problem and the code work well

but i have an warning

with this code
Code:
    Private Sub DataReceivedHandler(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort.DataReceived


        Dim sp As SerialPort = CType(sender, SerialPort)
        Dim s As String
        If Not leaving Then
            s = sp.ReadLine().ToString
        End If
        If Not leaving Then
            Me.Invoke(Sub() parseSerial(s))
        End If
        If leaving Then
            Me.BeginInvoke(Sub() Close())    'reissue a close command to the form
        End If


        'Dim sp As SerialPort = CType(sender, SerialPort)
        'Dim s As String = sp.ReadLine().ToString
        'Me.Invoke(Sub() parseSerial(s))

    End Sub


its compile with no problem and work well

but i want to ensure there will be no problem in the future

the warning is

"Warning 1 Variable 's' is used before it has been assigned a value. A null reference exception could result at runtime."


also while program running if i disconnect arduino cable and trying to exit
the exit button will not close the program and this message show to me

http://s4.postimg.org/nv9z4kx19/vb_error.jpg

Last edited by haider; 02-20-2015 at 09:40 PM.
Reply With Quote
  #11  
Old 02-21-2015, 02:08 AM
passel's Avatar
passelserial port freezing in VB.NET when use it with Arduino passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

To get rid of the warning about s, just initialize s to something, usually nothing, when you declare it.

Dim s as String = Nothing

As for the exception when previously disconnecting the arduino cable, since you're on your way out, and really don't care (the port was probably already closed or disconnected), just wrap it in a general try catch exception handler.
Code:
  Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If Not leaving Then 'if we haven't set the leaving flag
      leaving = True    '  set it
      e.Cancel = True   '  Cancel then current closing to allow background tasks to exit
    Else
      Try
        serialport1.Close()   'we're on our way out, close the port
      Catch
      End Try
    End If
  End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #12  
Old 02-21-2015, 07:49 AM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

that's done

why i cant but this

Code:
SerialPort.PortName = "COM12"
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()
in try and catch in Private Sub DataReceivedHandler

in stead of putting it to work one time with GUI in form

in id did not open or i plug off the cable then i cant go back to another form to re connection or do another thing with try and catch

Last edited by haider; 02-21-2015 at 03:28 PM.
Reply With Quote
  #13  
Old 02-21-2015, 09:59 PM
passel's Avatar
passelserial port freezing in VB.NET when use it with Arduino passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

I don't know what you code looks like now, but if you're not leaving the program, but just want to reopen the port, I would just have those lines in a sub that I can call when needed.
You could call the sub from the Form_Load sub.
You could call it from a button to open the port whenever you want.

I don't know that I would call it from another event handler if I got an exception.
If I got an exception in the DataReceived event, I would probably set a global flag variable indicating the port needed to be reopened.
I would have a "watchdog" timer that monitored the flag, and if it sees it, would clear it and call the sub to open the port again.

I'm not sure if that is the problem you're dealing with, it is hard to understand.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #14  
Old 02-23-2015, 03:03 PM
haider haider is offline
Newcomer
 
Join Date: Apr 2014
Posts: 24
Default

i solve that thank you very much passel

my regards
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
 
serial port freezing in VB.NET when use it with Arduino
serial port freezing in VB.NET when use it with Arduino
 
-->