VS 2013 unreliable serial port comunications-VBForums
Results 1 to 12 of 12

Thread: unreliable serial port comunications

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    7

    unreliable serial port comunications

    Im trying to send some information over a serial port to an ardiuno , I had the whole porbgram working perfectly but now it only works some times. I beleive the problem lies in the following code

    SerialPort1.Write("w")
    SerialPort1.Write(textbox1.Text)
    SerialPort1.Write("\n")

    Is this an acceptable way to write to serial , it seems to work but only sometimes, If i send the same nubers to the arduino with a serial monitor is responds as expected so it must be the way it is set here.
    Is there any other possibilities

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    8,947

    Re: unreliable serial port comunications

    Don't see a problem with the code. You might consider constructing the string and using WriteLine, like this
    Code:
            Dim s As String = "w" & TextBox1.Text
            SerialPort1.WriteLine(s)
    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
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin
    "It is not all that I know that is the problem, it is all I think I do that is."

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    7

    Re: unreliable serial port comunications

    If I add a 500 ms delay using a while loop after the serial writes it seems to work. but this doesn't seem like a real solution and like it might have issues later.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    8,947

    Re: unreliable serial port comunications

    Quote Originally Posted by simon100 View Post
    If I add a 500 ms delay using a while loop after the serial writes it seems to work. but this doesn't seem like a real solution and like it might have issues later.
    Make certain that you have the same setup at both ends of the port. This could be an issue with the Ardiuno end.

    After you do the write are you closing the port? Are you using flow control?
    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
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin
    "It is not all that I know that is the problem, it is all I think I do that is."

  5. #5
    Lively Member Goggy's Avatar
    Join Date
    Oct 2017
    Posts
    65

    Re: unreliable serial port comunications

    A serial port has a max baud rate and if i rember rite from my schooling, its a one way street... in the sence only one can speak on the line. So if you send something while the other is responding to you first message, the messages will all be lost.
    So i think it's a good idea to wait a little while after sending you second message. 500 ms feels a bit long , but none the less 100 ms or so seems to me to be normal....

    on the otherhand i might be talking rubbish.... seeing my scholing is a little while behind me...
    Utterly useless, but always willing to help

    As a finishing touch god created the dutch

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,106

    Re: unreliable serial port comunications

    I would be surprised if you should be doing
    SerialPort1.Write("\n")

    I would have though it should be along the lines of
    SerialPort1.Write(vbLf)

    I admit I didn't test to see if the Write function in .Net excepts C-style escape sequences, but I'm pretty sure it wasn't something you could do in earlier versions of VB.

  7. #7

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    7

    Re: unreliable serial port comunications

    Quote Originally Posted by dbasnett View Post
    Make certain that you have the same setup at both ends of the port. This could be an issue with the Ardiuno end.

    After you do the write are you closing the port? Are you using flow control?
    I'm not closing the port is this required ? and am not using any flow control, i was not aware of flow control i searched and from what i understand it is one end confirming it has finished receiving before the other transmits again ?

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    7

    Re: unreliable serial port comunications

    Quote Originally Posted by Goggy View Post
    A serial port has a max baud rate and if i rember rite from my schooling, its a one way street... in the sence only one can speak on the line. So if you send something while the other is responding to you first message, the messages will all be lost.
    So i think it's a good idea to wait a little while after sending you second message. 500 ms feels a bit long , but none the less 100 ms or so seems to me to be normal....

    on the otherhand i might be talking rubbish.... seeing my scholing is a little while behind me...
    i think the usb connection is a differential pair set up so only one could talk at a time but i was under the impression the usb controller at each end handled this . im only running at 9600 baud but even at that there should be heaps of time for what is happening.
    To be a bit more specific the arduino ( actually a teensy lc) sends a message every 100ms thats about 5 characters long , when the synch button is clicked in the vb program it then sends the message pictured at the top of the thread 5 times with different details. Its looking like i need to do some learning about usb vs the serial i would use between 2 arduino

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Pointless Forest 38.517,-92.023
    Posts
    8,947

    Re: unreliable serial port comunications

    Quote Originally Posted by simon100 View Post
    i think the usb connection is a differential pair set up so only one could talk at a time but i was under the impression the usb controller at each end handled this . im only running at 9600 baud but even at that there should be heaps of time for what is happening.
    To be a bit more specific the arduino ( actually a teensy lc) sends a message every 100ms thats about 5 characters long , when the synch button is clicked in the vb program it then sends the message pictured at the top of the thread 5 times with different details. Its looking like i need to do some learning about usb vs the serial i would use between 2 arduino
    There should not be an issue with simultaneous send / receive (full duplex) communication at a physical level. That does not mean that the application software is written to do that. Based on what you have described it shouldn't be an issue.
    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
    "They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety." Benjamin Franklin
    "It is not all that I know that is the problem, it is all I think I do that is."

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    4,106

    Re: unreliable serial port comunications

    Did you try changing your line termination method?
    Either use vbLf instead of ("/n") or use WriteLine for the last substring (or combine the substrings like dbasnett suggested in post #2).
    Code:
            SerialPort1.Write("w")
            SerialPort1.Write(TextBox2.Text)
            SerialPort1.Write(vbLf)
    '---Or---
            SerialPort1.Write("w")
            SerialPort1.WriteLine(TextBox2.Text)
     '---Or---
            Dim s As String
    
            s  = "w" & TextBox1.Text
            SerialPort1.WriteLine(s)
    
            s  = "w" & TextBox2.Text
            SerialPort1.WriteLine(s)
    
            'etc...
    I did verify that SerialWrite does not convert "/n" into a linefeed character, so at least that is one known thing that is wrong with your intentions.
    Last edited by passel; Dec 1st, 2017 at 05:20 PM.

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    7

    Re: unreliable serial port comunications

    Quote Originally Posted by passel View Post
    Did you try changing your line termination method?
    Either use vbLf instead of ("/n") or use WriteLine for the last substring (or combine the substrings like dbasnett suggested in post #2).
    Code:
            SerialPort1.Write("w")
            SerialPort1.Write(TextBox2.Text)
            SerialPort1.Write(vbLf)
    '---Or---
            SerialPort1.Write("w")
            SerialPort1.WriteLine(TextBox2.Text)
     '---Or---
            Dim s As String
    
            s  = "w" & TextBox1.Text
            SerialPort1.WriteLine(s)
    
            s  = "w" & TextBox2.Text
            SerialPort1.WriteLine(s)
    
            'etc...
    I did verify that SerialWrite does not convert "/n" into a linefeed character, so at least that is one known thing that is wrong with your intentions.
    ok thanks I will try and change that but it makes me wonder how it is working at all

  12. #12

    Thread Starter
    New Member
    Join Date
    Nov 2017
    Posts
    7

    Re: unreliable serial port comunications

    I tried vblf and it seems to work perfect now thanks for the solution, on the arduino end i was using "Serial.readStringUntil('\n');" to get the message but there was also a time out on this so i asume it was actually the time out that was making it all work.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.