Adding a 5ms delay in between sending bytes to a controller

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmdolcet69

    Adding a 5ms delay in between sending bytes to a controller

    How can i add a 5ms delay between bytes i send over to a controller?
    I have to send 6 bytes in total however in between each byte i need to
    have a 5ms delay.

  • RB

    #2
    Re: Adding a 5ms delay in between sending bytes to a controller

    cmdolcet69 wrote:
    How can i add a 5ms delay between bytes i send over to a controller?
    I have to send 6 bytes in total however in between each byte i need to
    have a 5ms delay.
    >
    I think you can just use Thread.Sleep (5).

    So you're code would look like:

    SendByteOne()
    Thread.Sleep (5)
    SendByteTwo()
    Thread.Sleep (5)
    SendByteThree()
    Thread.Sleep (5)

    etc...

    See


    Cheers,

    RB.

    Comment

    • cmdolcet69

      #3
      Re: Adding a 5ms delay in between sending bytes to a controller

      On Sep 20, 10:55 am, RB <owmdkbqziki... @mailinator.com wrote:
      cmdolcet69 wrote:
      How can i add a 5ms delay between bytes i send over to a controller?
      I have to send 6 bytes in total however in between each byte i need to
      have a 5ms delay.
      >
      I think you can just use Thread.Sleep (5).
      >
      So you're code would look like:
      >
      SendByteOne()
      Thread.Sleep (5)
      SendByteTwo()
      Thread.Sleep (5)
      SendByteThree()
      Thread.Sleep (5)
      >
      etc...
      >
      Seehttp://msdn2.microsoft .com/en-us/library/system.threadin g.thread.slee.. .
      >
      Cheers,
      >
      RB.
      RB, I was on the right path in thinking. I have been reading however
      that it may be sloppy coding.....anyw ays.... how can i write it were i
      use this delay with each byte i send across from a com.ouput command

      example: com.output= (byte(1) + thead.sleep(5) + byte(2)+.......

      ?????

      Comment

      • RB

        #4
        Re: Adding a 5ms delay in between sending bytes to a controller

        cmdolcet69 wrote:
        On Sep 20, 10:55 am, RB <owmdkbqziki... @mailinator.com wrote:
        >cmdolcet69 wrote:
        >>How can i add a 5ms delay between bytes i send over to a controller?
        >>I have to send 6 bytes in total however in between each byte i need to
        >>have a 5ms delay.
        >I think you can just use Thread.Sleep (5).
        >>
        >So you're code would look like:
        >>
        >SendByteOne( )
        >Thread.Sleep (5)
        >SendByteTwo( )
        >Thread.Sleep (5)
        >SendByteThree( )
        >Thread.Sleep (5)
        >>
        >etc...
        >>
        >Seehttp://msdn2.microsoft .com/en-us/library/system.threadin g.thread.slee.. .
        >>
        >Cheers,
        >>
        >RB.
        >
        RB, I was on the right path in thinking. I have been reading however
        that it may be sloppy coding.....anyw ays.... how can i write it were i
        use this delay with each byte i send across from a com.ouput command
        >
        example: com.output= (byte(1) + thead.sleep(5) + byte(2)+.......
        >
        ?????
        >

        You could use something similar to :

        Dim i as integer
        For i = 1 to 6
        com.output = byte(i)
        Thread.Sleep(5)
        Next i

        Be aware that this will cause AT LEAST a 5 ms delay per byte sending. It
        may be longer - would this cause an issue?

        Cheers,

        RB.

        Comment

        • cmdolcet69

          #5
          Re: Adding a 5ms delay in between sending bytes to a controller

          On Sep 20, 11:05 am, RB <owmdkbqziki... @mailinator.com wrote:
          cmdolcet69 wrote:
          On Sep 20, 10:55 am, RB <owmdkbqziki... @mailinator.com wrote:
          cmdolcet69 wrote:
          >How can i add a 5ms delay between bytes i send over to a controller?
          >I have to send 6 bytes in total however in between each byte i need to
          >have a 5ms delay.
          I think you can just use Thread.Sleep (5).
          >
          So you're code would look like:
          >
          SendByteOne()
          Thread.Sleep (5)
          SendByteTwo()
          Thread.Sleep (5)
          SendByteThree()
          Thread.Sleep (5)
          >
          etc...
          >
          Seehttp://msdn2.microsoft .com/en-us/library/system.threadin g.thread.slee.. .
          >
          Cheers,
          >
          RB.
          >
          RB, I was on the right path in thinking. I have been reading however
          that it may be sloppy coding.....anyw ays.... how can i write it were i
          use this delay with each byte i send across from a com.ouput command
          >
          example: com.output= (byte(1) + thead.sleep(5) + byte(2)+.......
          >
          ?????
          >
          You could use something similar to :
          >
          Dim i as integer
          For i = 1 to 6
          com.output = byte(i)
          Thread.Sleep(5)
          Next i
          >
          Be aware that this will cause AT LEAST a 5 ms delay per byte sending. It
          may be longer - would this cause an issue?
          >
          Cheers,
          >
          RB.- Hide quoted text -
          >
          - Show quoted text -
          RB how can i do the for loop with these bytes
          Dim Len As Byte = 6
          Dim Cmd As Byte = 7
          Dim loPass_byte As Byte = 28
          Dim hiPass_byte As Byte = 45
          Dim CRClo_byte As Byte = 48
          Dim CRChi_byte As Byte = 130

          Comment

          • rowe_newsgroups

            #6
            Re: Adding a 5ms delay in between sending bytes to a controller

            On Sep 20, 11:12 am, cmdolcet69 <colin_dolce... @hotmail.comwro te:
            On Sep 20, 11:05 am, RB <owmdkbqziki... @mailinator.com wrote:
            >
            >
            >
            cmdolcet69 wrote:
            On Sep 20, 10:55 am, RB <owmdkbqziki... @mailinator.com wrote:
            >cmdolcet69 wrote:
            >>How can i add a 5ms delay between bytes i send over to a controller?
            >>I have to send 6 bytes in total however in between each byte i need to
            >>have a 5ms delay.
            >I think you can just use Thread.Sleep (5).
            >
            >So you're code would look like:
            >
            >SendByteOne( )
            >Thread.Sleep (5)
            >SendByteTwo( )
            >Thread.Sleep (5)
            >SendByteThree( )
            >Thread.Sleep (5)
            >
            >etc...
            >
            >Seehttp://msdn2.microsoft .com/en-us/library/system.threadin g.thread.slee.. .
            >
            >Cheers,
            >
            >RB.
            >
            RB, I was on the right path in thinking. I have been reading however
            that it may be sloppy coding.....anyw ays.... how can i write it were i
            use this delay with each byte i send across from a com.ouput command
            >
            example: com.output= (byte(1) + thead.sleep(5) + byte(2)+.......
            >
            ?????
            >
            You could use something similar to :
            >
            Dim i as integer
            For i = 1 to 6
            com.output = byte(i)
            Thread.Sleep(5)
            Next i
            >
            Be aware that this will cause AT LEAST a 5 ms delay per byte sending. It
            may be longer - would this cause an issue?
            >
            Cheers,
            >
            RB.- Hide quoted text -
            >
            - Show quoted text -
            >
            RB how can i do the for loop with these bytes
            Dim Len As Byte = 6
            Dim Cmd As Byte = 7
            Dim loPass_byte As Byte = 28
            Dim hiPass_byte As Byte = 45
            Dim CRClo_byte As Byte = 48
            Dim CRChi_byte As Byte = 130
            Dump them into an array, collection, list, etc and then do the for
            each loop.

            Thanks,

            Seth Rowe

            Comment

            • Dick Grier

              #7
              Re: Adding a 5ms delay in between sending bytes to a controller

              Use an array of Type byte. E.g.,

              Dim Packet(5) As Byte
              Packet(0) = 6
              Packet(1) = 7
              Packet(2) = 28
              Packet(3) = 45
              Packet(4) = 48
              Packet(5) = 130

              Then use a loop. Arrays of type Byte are standard for this. For example,
              the SerialPort object Write method has an overload for a Byte array (if you
              wanted to send all "back-to-back, with no delay).

              Dick

              --
              Richard Grier, MVP
              Hard & Software
              Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
              Edition,
              ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
              2006.
              See www.hardandsoftware.net for details and contact information.


              Comment

              • cmdolcet69

                #8
                Re: Adding a 5ms delay in between sending bytes to a controller

                On Sep 20, 1:46 pm, "Dick Grier" <dick_grierNOSP AM@.msn.comwrot e:
                Use an array of Type byte. E.g.,
                >
                Dim Packet(5) As Byte
                Packet(0) = 6
                Packet(1) = 7
                Packet(2) = 28
                Packet(3) = 45
                Packet(4) = 48
                Packet(5) = 130
                >
                Then use a loop. Arrays of type Byte are standard for this. For example,
                the SerialPort object Write method has an overload for a Byte array (if you
                wanted to send all "back-to-back, with no delay).
                >
                Dick
                >
                --
                Richard Grier, MVP
                Hard & Software
                Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
                Edition,
                ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
                2006.
                Seewww.hardands oftware.netfor details and contact information.
                when i do the following loop and code for the initial loop it will
                break and give me an error saying "property value is not valid"

                Dim i As Integer
                Dim Packet(5) As Byte
                Packet(0) = 6
                Packet(1) = 7
                Packet(2) = 28
                Packet(3) = 45
                Packet(4) = 48
                Packet(5) = 130

                For i = 0 To 5
                comm1.Output = Packet(i)
                Thread.Sleep(5)
                Next i

                Comment

                • RB

                  #9
                  Re: Adding a 5ms delay in between sending bytes to a controller

                  cmdolcet69 wrote:
                  On Sep 20, 1:46 pm, "Dick Grier" <dick_grierNOSP AM@.msn.comwrot e:
                  >Use an array of Type byte. E.g.,
                  >>
                  >Dim Packet(5) As Byte
                  > Packet(0) = 6
                  > Packet(1) = 7
                  > Packet(2) = 28
                  > Packet(3) = 45
                  > Packet(4) = 48
                  > Packet(5) = 130
                  >>
                  >Then use a loop. Arrays of type Byte are standard for this. For example,
                  >the SerialPort object Write method has an overload for a Byte array (if you
                  >wanted to send all "back-to-back, with no delay).
                  >>
                  >Dick
                  >>
                  >--
                  >Richard Grier, MVP
                  >Hard & Software
                  >Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
                  >Edition,
                  >ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
                  >2006.
                  >Seewww.hardand software.netfor details and contact information.
                  when i do the following loop and code for the initial loop it will
                  break and give me an error saying "property value is not valid"
                  >
                  Dim i As Integer
                  Dim Packet(5) As Byte
                  Packet(0) = 6
                  Packet(1) = 7
                  Packet(2) = 28
                  Packet(3) = 45
                  Packet(4) = 48
                  Packet(5) = 130
                  >
                  For i = 0 To 5
                  comm1.Output = Packet(i)
                  Thread.Sleep(5)
                  Next i
                  >
                  You have 6 elements in your array, but you've only declared it as having
                  5 elements.

                  Dim Packet(6) As Byte 'THIS IS THE LINE OF CODE I CHANGED.
                  Packet(0) = 6
                  Packet(1) = 7
                  Packet(2) = 28
                  Packet(3) = 45
                  Packet(4) = 48
                  Packet(5) = 130

                  For i = 0 To 5
                  comm1.Output = Packet(i)
                  Thread.Sleep(5)
                  Next i

                  Comment

                  • Armin Zingler

                    #10
                    Re: Adding a 5ms delay in between sending bytes to a controller

                    "RB" <owmdkbqzikigpu @mailinator.com schrieb
                    when i do the following loop and code for the initial loop it will
                    break and give me an error saying "property value is not valid"

                    Dim i As Integer
                    Dim Packet(5) As Byte
                    Packet(0) = 6
                    Packet(1) = 7
                    Packet(2) = 28
                    Packet(3) = 45
                    Packet(4) = 48
                    Packet(5) = 130

                    For i = 0 To 5
                    comm1.Output = Packet(i)
                    Thread.Sleep(5)
                    Next i
                    >
                    You have 6 elements in your array, but you've only declared it as
                    having 5 elements.

                    No, his array has 6 elements, indexes 0, 1, 2, 3, 4, 5.
                    Dim Packet(6) As Byte 'THIS IS THE LINE OF CODE I CHANGED.
                    Packet(0) = 6
                    Packet(1) = 7
                    Packet(2) = 28
                    Packet(3) = 45
                    Packet(4) = 48
                    Packet(5) = 130
                    >
                    For i = 0 To 5
                    comm1.Output = Packet(i)
                    Thread.Sleep(5)
                    Next i

                    Your array can contain 7 items. You don't fill Packet(6).


                    Armin

                    Comment

                    • RB

                      #11
                      Re: Adding a 5ms delay in between sending bytes to a controller

                      Armin Zingler wrote:
                      "RB" <owmdkbqzikigpu @mailinator.com schrieb
                      when i do the following loop and code for the initial loop it will
                      break and give me an error saying "property value is not valid"
                      Dim i As Integer
                      Dim Packet(5) As Byte
                      Packet(0) = 6
                      Packet(1) = 7
                      Packet(2) = 28
                      Packet(3) = 45
                      Packet(4) = 48
                      Packet(5) = 130
                      For i = 0 To 5
                      comm1.Output = Packet(i)
                      Thread.Sleep(5)
                      Next i
                      >
                      >You have 6 elements in your array, but you've only declared it as
                      >having 5 elements.
                      >
                      >
                      No, his array has 6 elements, indexes 0, 1, 2, 3, 4, 5.
                      >
                      > Dim Packet(6) As Byte 'THIS IS THE LINE OF CODE I CHANGED.
                      > Packet(0) = 6
                      > Packet(1) = 7
                      > Packet(2) = 28
                      > Packet(3) = 45
                      > Packet(4) = 48
                      > Packet(5) = 130
                      >>
                      > For i = 0 To 5
                      > comm1.Output = Packet(i)
                      > Thread.Sleep(5)
                      > Next i
                      >
                      >
                      Your array can contain 7 items. You don't fill Packet(6).
                      >
                      >
                      Armin
                      Sorry, you're totally right. "Property value not valid" would be an
                      unusual error to receive from an array out of bounds anyway!!

                      I suppose the most likely line to be throwing the error is "comm1.outp ut
                      = Packet(i)" - but without knowing the details of the comm1 object, I
                      can't really help any further...

                      Cheers,

                      RB.

                      Comment

                      • Armin Zingler

                        #12
                        Re: Adding a 5ms delay in between sending bytes to a controller

                        "cmdolcet69 " <colin_dolcetti @hotmail.comsch rieb
                        when i do the following loop and code for the initial loop it will
                        break and give me an error saying "property value is not valid"
                        >
                        Dim i As Integer
                        Dim Packet(5) As Byte
                        Packet(0) = 6
                        Packet(1) = 7
                        Packet(2) = 28
                        Packet(3) = 45
                        Packet(4) = 48
                        Packet(5) = 130
                        >
                        For i = 0 To 5
                        comm1.Output = Packet(i)
                        Thread.Sleep(5)
                        Next i

                        Is comm1 the COM component for the serial port? If so, I guess you must
                        either assign a string or an array of bytes to the output property, but I'm
                        not sure because I can't test it here. Otherwise, using VB 2005, you should
                        consider using the System.IO.Ports .SerialPort class instead.


                        Armin

                        Comment

                        • cmdolcet69

                          #13
                          Re: Adding a 5ms delay in between sending bytes to a controller

                          On Sep 21, 5:32 am, RB <owmdkbqziki... @mailinator.com wrote:
                          Armin Zingler wrote:
                          "RB" <owmdkbqziki... @mailinator.com schrieb
                          when i do the following loop and code for the initial loop it will
                          break and give me an error saying "property value is not valid"
                          Dim i As Integer
                          Dim Packet(5) As Byte
                          Packet(0) = 6
                          Packet(1) = 7
                          Packet(2) = 28
                          Packet(3) = 45
                          Packet(4) = 48
                          Packet(5) = 130
                          For i = 0 To 5
                          comm1.Output = Packet(i)
                          Thread.Sleep(5)
                          Next i
                          >
                          You have 6 elements in your array, but you've only declared it as
                          having 5 elements.
                          >
                          No, his array has 6 elements, indexes 0, 1, 2, 3, 4, 5.
                          >
                          Dim Packet(6) As Byte 'THIS IS THE LINE OF CODE I CHANGED.
                          Packet(0) = 6
                          Packet(1) = 7
                          Packet(2) = 28
                          Packet(3) = 45
                          Packet(4) = 48
                          Packet(5) = 130
                          >
                          For i = 0 To 5
                          comm1.Output = Packet(i)
                          Thread.Sleep(5)
                          Next i
                          >
                          Your array can contain 7 items. You don't fill Packet(6).
                          >
                          Armin
                          >
                          Sorry, you're totally right. "Property value not valid" would be an
                          unusual error to receive from an array out of bounds anyway!!
                          >
                          I suppose the most likely line to be throwing the error is "comm1.outp ut
                          = Packet(i)" - but without knowing the details of the comm1 object, I
                          can't really help any further...
                          >
                          Cheers,
                          >
                          RB.- Hide quoted text -
                          >
                          - Show quoted text -
                          Public Sub SetupCOM(ByVal comPort As Integer)
                          'initialize the com if it has not been already
                          If IsNothing(comm1 ) Then
                          comm1 = New MSComm
                          End If
                          'set the properties of the com port
                          comm1.CommPort = comPort
                          comm1.Settings = "9600,n,8,1 "

                          'open the port and clear the buffer if it is not alread open
                          (which it better not be)
                          If Not comm1.PortOpen Then
                          comm1.PortOpen = True
                          comm1.InBufferC ount = 0
                          System.Threadin g.Thread.Curren tThread.Sleep(1 00)
                          End If
                          End Sub

                          Comment

                          • cmdolcet69

                            #14
                            Re: Adding a 5ms delay in between sending bytes to a controller

                            On Sep 21, 5:28 am, "Armin Zingler" <az.nos...@free net.dewrote:
                            "cmdolcet69 " <colin_dolce... @hotmail.comsch rieb
                            >
                            >
                            >
                            >
                            >
                            when i do the following loop and code for the initial loop it will
                            break and give me an error saying "property value is not valid"
                            >
                            Dim i As Integer
                            Dim Packet(5) As Byte
                            Packet(0) = 6
                            Packet(1) = 7
                            Packet(2) = 28
                            Packet(3) = 45
                            Packet(4) = 48
                            Packet(5) = 130
                            >
                            For i = 0 To 5
                            comm1.Output = Packet(i)
                            Thread.Sleep(5)
                            Next i
                            >
                            Is comm1 the COM component for the serial port? If so, I guess you must
                            either assign a string or an array of bytes to the output property, but I'm
                            not sure because I can't test it here. Otherwise, using VB 2005, you should
                            consider using the System.IO.Ports .SerialPort class instead.
                            >
                            Armin- Hide quoted text -
                            >
                            - Show quoted text -
                            Armin comm1 is the com component for the serial port. I can;t assign a
                            string or an array because i need to have a thread.sleep(5) so that i
                            give time for the micro to crunch the readings. All this code is in vb
                            2003 (1.1 Framework) because the program was orinigally developed in
                            that language.

                            Public Sub GetIndicator_In fo()

                            Dim Len As Byte = 6
                            Dim Cmd As Byte = 7
                            Dim loPass_byte As Byte = 28
                            Dim hiPass_byte As Byte = 45
                            Dim CRClo_byte As Byte = 48
                            Dim CRChi_byte As Byte = 130

                            comm1.InBufferC ount = 0
                            comm1.InputLen = 0
                            comm1.InputMode = InputModeConsta nts.comInputMod eBinary



                            Dim i As Integer
                            Dim Packet(6) As Byte
                            Packet(0) = 42
                            Packet(1) = 6
                            Packet(2) = 7
                            Packet(3) = 28
                            Packet(4) = 45
                            Packet(5) = 48
                            Packet(6) = 130

                            For i = 0 To 6
                            Thread.Sleep(5)
                            comm1.Output = Packet(i)
                            Next i
                            End Sub

                            in any event this code should work. Now if i pass a complete string of
                            byte like you say....
                            comm1.ouput= packet(0)& packet(1) & packet(2)&..... ...

                            is will accept that hwoever te micro cannot process this....

                            Comment

                            • Armin Zingler

                              #15
                              Re: Adding a 5ms delay in between sending bytes to a controller

                              "cmdolcet69 " <colin_dolcetti @hotmail.comsch rieb

                              Is comm1 the COM component for the serial port? If so, I guess you
                              must either assign a string or an array of bytes to the output
                              property, but I'm not sure because I can't test it here.
                              Otherwise, using VB 2005, you should consider using the
                              System.IO.Ports .SerialPort class instead.

                              Armin- Hide quoted text -

                              - Show quoted text -
                              >
                              Armin comm1 is the com component for the serial port. I can;t assign
                              a string or an array because i need to have a thread.sleep(5) so
                              that i give time for the micro to crunch the readings. All this code
                              is in vb 2003 (1.1 Framework) because the program was orinigally
                              developed in that language.
                              >
                              Public Sub GetIndicator_In fo()
                              >
                              Dim Len As Byte = 6
                              Dim Cmd As Byte = 7
                              Dim loPass_byte As Byte = 28
                              Dim hiPass_byte As Byte = 45
                              Dim CRClo_byte As Byte = 48
                              Dim CRChi_byte As Byte = 130
                              >
                              comm1.InBufferC ount = 0
                              comm1.InputLen = 0
                              comm1.InputMode = InputModeConsta nts.comInputMod eBinary
                              >
                              >
                              >
                              Dim i As Integer
                              Dim Packet(6) As Byte
                              Packet(0) = 42
                              Packet(1) = 6
                              Packet(2) = 7
                              Packet(3) = 28
                              Packet(4) = 45
                              Packet(5) = 48
                              Packet(6) = 130
                              >
                              For i = 0 To 6
                              Thread.Sleep(5)
                              comm1.Output = Packet(i)
                              Next i
                              End Sub
                              >
                              in any event this code should work. Now if i pass a complete string
                              of byte like you say....
                              comm1.ouput= packet(0)& packet(1) & packet(2)&..... ...
                              >
                              is will accept that hwoever te micro cannot process this....

                              Yes, because you don't know what you are doing here. If you concatenate the
                              values, you get "4267284548130" . These are 13 bytes. But you don't want to
                              send 13 bytes. The bytes are: 52, 50, 54, 55, 50, 56, .... Why? These are
                              the codes of the characters in the string. Because "0" has character code
                              48, "1" has code 49, and so on.

                              Maybe

                              comm1.Output = Chr(Packet(i))

                              works. I don't know. I have no serial ports enable in BIOS - and no device
                              attached.

                              Are you sure you have to write a 5ms delay? Doesn't the serial port have a
                              (small) out buffer? If it's full, doesn't the code wait til more data can be
                              sent? You are already setting the port speed (9600). Isn't it sufficient?


                              Armin

                              Comment

                              Working...