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

  • Thread starter Thread starter cmdolcet69
  • Start date Start date
C

cmdolcet69

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.threading.thread.slee...

Cheers,

RB.

RB, I was on the right path in thinking. I have been reading however
that it may be sloppy coding.....anyways.... 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)+.......

?????
 
cmdolcet69 said:
RB, I was on the right path in thinking. I have been reading however
that it may be sloppy coding.....anyways.... 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.
 
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
 
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
 
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.
 
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.hardandsoftware.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
 
cmdolcet69 said:
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
 
RB said:
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
 
cmdolcet69 said:
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
 
Armin said:
No, his array has 6 elements, indexes 0, 1, 2, 3, 4, 5.



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.output
= Packet(i)" - but without knowing the details of the comm1 object, I
can't really help any further...

Cheers,

RB.
 
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.output
= 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.InBufferCount = 0
System.Threading.Thread.CurrentThread.Sleep(100)
End If
End Sub
 
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_Info()

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.InBufferCount = 0
comm1.InputLen = 0
comm1.InputMode = InputModeConstants.comInputModeBinary



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....
 
cmdolcet69 said:
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_Info()

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.InBufferCount = 0
comm1.InputLen = 0
comm1.InputMode = InputModeConstants.comInputModeBinary



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

Yes, comm1 is the COM component for the serial port. Here is all the
code which is assocaited with this object.

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 = "57600,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.InBufferCount = 0
System.Threading.Thread.CurrentThread.Sleep(100)
End If
End Sub


Public Sub GetIndicator_Info()

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.InBufferCount = 0
comm1.InputLen = 0
comm1.InputMode = InputModeConstants.comInputModeText


Dim i As Integer
Dim Packet(6) As Byte
Packet(0) = 42 'Hex 2A
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
 
Hi,

I wrote my suggestion "at the keyboard," not in the IDE. You actually have
to debug the code to see where it goes wrong. The most likely thing is that
your loop is sending a byte that does not exist.

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.
 
Hi,

What is the Output method? The SerialPort object uses Write, not Output.
It looks like you are using MSComm? There is no good reason to do this.
Use the built-in SerialPort control.

--
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.
 
It looks like are attempting to use MSComm by late-binding. This really
isn't supported. If you want to use MSComm, add it to your toolbox and drop
it on the form. There are a few small syntax changes required.


--
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.
 
Just use the SerialPort control. Drop one on design surface.

With SerialPort1
.DTEnable = True
.RTSEnable = True
.BaudRate = 57600
.Parity = Parity.None
.DataBits = 8
.CommPort = PortName = "Com1"
End With

'then your code:

Dim i As Integer
Dim Packet(6) As Byte
Packet(0) = 42 'Hex 2A
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)
SerialPort1.Write(Packet(i), 0, 1)
Next i

BTW, this also is "at the keyboard," so you will have to actually debug it.

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.
 
Back
Top