Page 3 of 3 FirstFirst 123
Results 81 to 102 of 102

Thread: [RESOLVED] SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS2008)

  1. #81
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Quote Originally Posted by LiamC View Post
    In the case were we would only use one serialport.write to avoid 2 iterations of serialdata received event, how would you parse a string like this one:

    Code:
    response = 20.000{CR}{LF}0.000{CR}{LF}
    knowing that the number of useful characters (positions) can change the next time a response is triggered by a user click on the "verify position" button ?
    Food for thought
    Code:
            'test message
            Dim response As String = "20.0" & vbCrLf & "10.123" & vbCrLf
            'I picked 255 because it is not normally an ASCII value seen
            response = response.Replace(vbCrLf, Chr(255)) 'change the two character CRLF to a single char
            Dim parts() As String = response.Split(New Char() {Chr(255)}, StringSplitOptions.RemoveEmptyEntries)
            For Each p As String In parts
                Debug.WriteLine(p)
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #82

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Hi !

    Testing this as soon as I can on the controler. I really like the thinking behind the split/parse action !

    I hope I can be as good "a sponge" in the technical domain as usual to absorb all the knowledge and tricks you explained here along the topic !

  3. #83

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    OK, it worked fine and for the first message (verify the state of the motion) it spared a trigger to the serialport data event.

  4. #84

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Hi Everybody,

    I'm close to the final UI that I wanted with nearly everything functionning, so a big thank you to both persons participating and helping here !

    I will put a screen capture of the UI with translated text (FR to ENG) to give an overview of where it is at now

    Cheers,

  5. #85
    Addicted Member
    Join Date
    Nov 2011
    Posts
    223

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Well done, writing an interface for 1 or 2 servos is quite an achievement. I think the code provided by dbasnett is flexible enough to be adapted for further applications using the Newport controller. Your project has certainly held my interest and I am glad you have succeeded.

  6. #86
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Quote Originally Posted by Mc_VB View Post
    Well done, writing an interface for 1 or 2 servos is quite an achievement...I am glad you have succeeded.
    Agreed!!! Congrats. Remember to mark this resolved.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #87

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Quote Originally Posted by dbasnett View Post
    Agreed!!! Congrats. Remember to mark this resolved.
    Thank you dbasnett and Mc_VB !

    I've been asked to tweak some things and re arrange the interface a bit, so still working on it !

    I'm facing another situation I'd like to put behind me :

    Let's say there is a sequence of bytes (array) with only twice the double char "vbCrLf". How to handle the ENDOFARRAY with GetChars(byteArray,0, ENDOFARRAY) in the conversion between bytes and string of chars ?
    I'd like to be sure I'm not letting more info than necessary in the converted string as I'm checking other things with it. Thus, I'd like to cut the conversion at the second vbCrLf index of the byte array.

    From my point of view, I was thinking of something that would look for the vbCrLf and then choose the position of Lf with the greater index of the two.

  8. #88
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Quote Originally Posted by LiamC View Post
    ...From my point of view, I was thinking of something that would look for the vbCrLf and then choose the position of Lf with the greater index of the two.
    Take a look at this code which is the code from the backgroundworker slightly modified to find the second LF. Note that I added some debug statements.

    Code:
            Dim idxLF As Integer = dataByts.IndexOf(LF)  'get the index of the first LF
    
            If idxLF >= 0 AndAlso idxLF + 1 < dataByts.Count Then
                Debug.WriteLine(idxLF)
                'there is one LF and there might be another
                idxLF = dataByts.IndexOf(LF, idxLF + 1)  'get the index of the second LF
                Debug.WriteLine(idxLF)
                If idxLF >= 0 Then
                    'do we have a second LF ?
                    'If yes, get the message from controller
                    Dim s As String
    
                    Threading.Monitor.Enter(dataLock)
                    s = System.Text.Encoding.GetEncoding(28591).GetChars(dataByts.ToArray, 0, idxLF - 1)
                    dataByts.RemoveRange(0, idxLF + 1) 'remove the message from the buffer
                    Threading.Monitor.Exit(dataLock)
                    'etc
                End If
            End If
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #89

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    OK, thank you !

    Didn't think that idxLf+1 "inside" the indexOf() was going to be the answer, will test that.

  10. #90

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Quote Originally Posted by dbasnett View Post
    Take a look at this code which is the code from the backgroundworker slightly modified to find the second LF. Note that I added some debug statements.

    Code:
            Dim idxLF As Integer = dataByts.IndexOf(LF)  'get the index of the first LF
    
            If idxLF >= 0 AndAlso idxLF + 1 < dataByts.Count Then
                Debug.WriteLine(idxLF)
                'there is one LF and there might be another
                idxLF = dataByts.IndexOf(LF, idxLF + 1)  'get the index of the second LF
                Debug.WriteLine(idxLF)
                If idxLF >= 0 Then
                    'do we have a second LF ?
                    'If yes, get the message from controller
                    Dim s As String
    
                    Threading.Monitor.Enter(dataLock)
                    s = System.Text.Encoding.GetEncoding(28591).GetChars(dataByts.ToArray, 0, idxLF - 1)
                    dataByts.RemoveRange(0, idxLF + 1) 'remove the message from the buffer
                    Threading.Monitor.Exit(dataLock)
                    'etc
                End If
            End If
    I don't get why there is a if index>=0 inside an IF statement that already has "IF index>=0" in it, because the index can only get larger with the line:

    Code:
     idxLF = dataByts.IndexOf(LF, idxLF + 1)  'get the index of the second LF
    I'm asking that because I have an issue :

    With the SECOND debug.writeline(idxLF) I sometimes get a value for the index = to “-1” which of course causes the program to stop as it isn’t >=0. This occurs when I click rapidly on my “VERIFY POSITION” button after the move is over (it's the button that triggers the response with coordinates in it). Whereas when I wait like 5sec, it doesn’t happen and I get the expected value (like 15 or 16, as it’s the index of the second vbCrLf of the return string).

  11. #91
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Quote Originally Posted by LiamC View Post
    I don't get why there is a if index>=0 inside an IF statement that already has "IF index>=0" in it, because the index can only get larger with the line:

    Code:
     idxLF = dataByts.IndexOf(LF, idxLF + 1)  'get the index of the second LF
    I'm asking that because I have an issue :

    With the SECOND debug.writeline(idxLF) I sometimes get a value for the index = to “-1” which of course causes the program to stop as it isn’t >=0. This occurs when I click rapidly on my “VERIFY POSITION” button after the move is over (it's the button that triggers the response with coordinates in it). Whereas when I wait like 5sec, it doesn’t happen and I get the expected value (like 15 or 16, as it’s the index of the second vbCrLf of the return string).
    The code is written this way because you wanted two LF's (two responses) before proceeding. There is nothing in the code to cause the program to stop that I can see. This assumes that you are testing what I posted.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #92

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Quote Originally Posted by dbasnett View Post
    The code is written this way because you wanted two LF's (two responses) before proceeding. There is nothing in the code to cause the program to stop that I can see. This assumes that you are testing what I posted.
    Yes, that's exactly how it works.

    The only worry is an index sometimes equal to -1 and I don't know where it comes from. Is it a default value when it cannot find a LF ?
    If the only char is indeed a vbCrLf, the index would be 1 and will thus proceed the string, is it correct ?

  13. #93
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    http://msdn.microsoft.com/en-us/library/e4w08k17.aspx

    for your answer. Does the code provided supply a string with two responses?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  14. #94

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Quote Originally Posted by dbasnett View Post
    http://msdn.microsoft.com/en-us/library/e4w08k17.aspx

    for your answer. Does the code provided supply a string with two responses?
    Thanks for the link, I sometimes check on MSDN but did not have a look this time, my bad as the answer is quite clear: the "-1" as a result of IndexOf(LF) does come from the fact the second LF wasn't found in one response. I think I have to look back to the changes I made to your code (I think it was around the part where there was the WaitOne() in an Else condition/loop: waiting for enough data). I may have made a mistake with a tweak. This error does not occur often, that's why it's bugging me...It may come from a string length change or something like that, because I can go through 20 steps of the loops before the "-1" is returned.

  15. #95
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Remember that you will receive the data you expect in pieces normally. This goes back to what we discussed near the beginning of this thread.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  16. #96

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Hi !

    I'm still within the subject of this thread but on slightly different things, so I'm posting this here:

    I have a form (form1.vb) with 2 buttons, the first button (let's say it's called btnMOVE) runs on UI thread and is doing several small operations. The second button (btnIMAGE) runs on the "worker" thread (GRABthread) which starts when I click on this button.

    In the GRAB thread, a longer , heavier operation is performed. I managed to update the status of controls (buttons/textbox) with this kind of code (here it can pass a button from enabled state to disabled and also from disabled to enabled):

    Code:
    Delegate Sub ChangeStatusBtnDel (ByVal btn As Button, ByVal Bool3 As Boolean)

    Code:
    Public sub ChangeStatusOfBtn (ByVal btn As Button, ByVal Bool3 As Boolean)
    
    If btn.InvokeRequired Then
    
    Dim BTNDel As New ChangeStatusOfBtnDel (AddressOf ChangeStatusOfBtn)
    
    Me.invoke (BTNDel, New Object() {btn, Bool3})
    
    Else
    
    btn.Enabled=Bool3
    
    End If
    
    End Sub
    And then I call the method with the 2 arguments (nameof button, state I want it to be (True/False) ).

    Now here's my issue at the moment:

    I have another form (progressdialog.vb) containing a progressbar in a indeterminate mode :

    Code:
    progresbar1.style= Progressbar1.marquee
    This is then supposed to be shown as a dialogbox (showdialog() ) during the worker thread running (GRABthread) in order to prevent a user to click on the form while the 2nd thread is running and to display information about its "occupied state" (and show the user it is running and that nothing has crashed or stopped)

    As it's done from another thread, I need to close this form (and the progressbar) when the GRABthread finishes. I haven't succeeded in doing so. I tried this to no avail:

    Code:
    Progresdialog.Invoke(New Action(AddressOf Progressdialog.close))
    The error is (translation from FR to ENGLISH) : "impossible to call invoke or beginInvoke on a control as long as the handle of the window has not been created".

    Whereas this worked:

    Code:
    Me.Invoke(New MethodInvoker (AddressOf Me.close))
    And closed all the opened forms.

    What is required to have a close action on a specific form (shown as dialogbox) from another thread ?
    Last edited by LiamC; Oct 18th, 2013 at 02:54 AM.

  17. #97

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Ok, my bad...

    I created the form ProgressDialog.vb and did not have that line in the code !!!

    Code:
    Dim ProgForm as New ProgressDialog
    Of course, now this:

    Code:
    ProgForm.Invoke ( New MethodInvoker(AddressOf ProgForm.Close))
    is working....

  18. #98

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Hi again

    I have another question:

    If I have to restart a task by clicking the same button again (and several times) how to manage the thread.start part ? To take the above example, a button starts a task, at some point we move to another thread to perform another task. but I need to perform this task several times, so let's says I need the operator to click again on the same button. So the sequence finds the thread.start() line again, dnd the error thrown is that it cannot (RE)start a thread that has stopped.

  19. #99
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    We are off topic and a new thread should be started in the forum. You can reference this thread if needed. Take a look at the following code(create a new project with one button):
    Code:
        Dim somethd As Threading.Thread
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Button1.Enabled = False
            If IsNothing(somethd) OrElse Not somethd.IsAlive Then
                somethd = New Threading.Thread(AddressOf something)
                somethd.IsBackground = True
                somethd.Start()
            Else
                Stop 'should not happen
            End If
        End Sub
    
        Dim anotherthrd As Threading.Thread
        Private Sub something()
            Debug.WriteLine("1")
            'simulate some long running
            Threading.Thread.Sleep(2000) '2 seconds
            'end simulation
            anotherthrd = New Threading.Thread(AddressOf another)
            anotherthrd.IsBackground = True
            anotherthrd.Start()
            anotherthrd.Join() 'wait for other thread to end
            Debug.WriteLine("4")
            somethingended()
        End Sub
    
        Private Sub another()
            Debug.WriteLine("2")
            'simulate some long running
            Threading.Thread.Sleep(2000) '2 seconds
            'end simulation
            Debug.WriteLine("3")
        End Sub
    
        Delegate Sub somethingendeddel()
        Private Sub somethingended()
            Debug.WriteLine("5")
            If Me.InvokeRequired Then
                Me.Invoke(New somethingendeddel(AddressOf somethingended))
            Else
                Button1.Enabled = True
            End If
        End Sub
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  20. #100

    Thread Starter
    Addicted Member
    Join Date
    Sep 2013
    Posts
    144

    Re: SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS2010 and VS200

    Quote Originally Posted by dbasnett View Post
    We are off topic and a new thread should be started in the forum. You can reference this thread if needed. Take a look at the following code(create a new project with one button):
    Code:
        Dim somethd As Threading.Thread
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Button1.Enabled = False
            If IsNothing(somethd) OrElse Not somethd.IsAlive Then
                somethd = New Threading.Thread(AddressOf something)
                somethd.IsBackground = True
                somethd.Start()
            Else
                Stop 'should not happen
            End If
        End Sub
    
        Dim anotherthrd As Threading.Thread
        Private Sub something()
            Debug.WriteLine("1")
            'simulate some long running
            Threading.Thread.Sleep(2000) '2 seconds
            'end simulation
            anotherthrd = New Threading.Thread(AddressOf another)
            anotherthrd.IsBackground = True
            anotherthrd.Start()
            anotherthrd.Join() 'wait for other thread to end
            Debug.WriteLine("4")
            somethingended()
        End Sub
    
        Private Sub another()
            Debug.WriteLine("2")
            'simulate some long running
            Threading.Thread.Sleep(2000) '2 seconds
            'end simulation
            Debug.WriteLine("3")
        End Sub
    
        Delegate Sub somethingendeddel()
        Private Sub somethingended()
            Debug.WriteLine("5")
            If Me.InvokeRequired Then
                Me.Invoke(New somethingendeddel(AddressOf somethingended))
            Else
                Button1.Enabled = True
            End If
        End Sub
    Thank you, this helped especially this " If IsNothing(somethd) OrElse Not somethd.IsAlive Then" !

  21. #101
    New Member
    Join Date
    Feb 2015
    Posts
    4

    Re: [RESOLVED] SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS201

    Great topic, could post final code for study LiamC ?

    Thanks

  22. #102
    Junior Member
    Join Date
    Mar 2016
    Posts
    16

    Re: [RESOLVED] SerialPort VB.NET : Write/buffer/timing and Data Received Event (VS201

    dbasnett,

    First of all I would like to thank you. I have been programming for a few years now and I feel I have learned more from reading your posts on this thread regarding serial communications than I have in all my years of programming. I am new to this forum so forgive me if this post would be better served in a new thread, but I am using your base code that you provide for this example and I am running into a few minor problems that I was hoping you could help me with. I will start by just explaining the problems and if needed I can post my code, but first I will provide some details on my setup.

    I am communicating with approximately 12 different devices over RS485. These devices are from 4 different manufacturers, so they each have slightly different timings in terms of how long it takes them to respond to a specific message. The way I would like this program to work would be to constantly send commands to these devices and once a response is received, send the next command in line. I would like this to happen indefinitely until I manually end the communications. This constant communication would run normally for approximately 7 days at a time before it would be ended, and its very important that if any interruption in communication occurs, it resume immediately.

    1. The first problem is similar to a problem that i believe LiamC had at some point. I can communicate no problem with any one of these devices at a time using a single command. When I try to add in multiple commands, sometimes the communication just stops, and I believe this is due to timing on the devices response, because I can get rid of this issue by placing a system.threading.thread.sleep(20) command before I call the cmdDone.Set. I also believe this is the case because on another type of device this issue doesn't exist, even if I send 20 or 30 commands at a time, but this devices response time is much quicker than the device I am having issues with. I can live with using this delay, however, the way I understand the code is that this timing should not matter, because I am sending a command, waiting for a response, dealing with that response, and then sending the next command. I also can't figure out an efficient way for these communications to resume once they cease up.

    2. The second problem I am having is related to how to handle to responses of the data. This is easy when you just have one set of data coming in and you can place all of the data into a textbook or a richtextbox, but how would you suggest placing the data into specific controls, for example the result of the first command needs to be in textbook 1, and the result of the second command needs to be in textbook 2 and so on...I am handling this fine now using a variable that is related to the send command so based on which command I receive it handles the data accordingly and so far no issues, however, I don't believe this is the best way to handle this, because I am assuming the commands will always be sent in the exact order which makes me a little nervous when the communications are interrupted.

    Again,

    Thanks dbasnett. I have learned a lot from you and hope to continue to do so.

Page 3 of 3 FirstFirst 123

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width