i am a student. i am using vb6 to get an agv running. using mscomm to speak to a main micro which then addresses motor boards. at present i am just testing 2 motors to see if the program will work. in my foward command button the code is as follows:

1 mscomm.output = "MOT1SPF1000"

2 for icount = 1 to 10
3 icount = icount +1
4 next icount

5 mscomm.output = "MOT2SPF1000"

(the string basically tells motor 1 to turn forward at speed 1000)

the motors work fine when addressed indivually
but since i added lines 2-5 only 1 motor spins and it doesnt stop

please could someone advise me on how to correct the program

thank you

10 Years
Discussion Span
Last Post by MikeStump

U r incrementing the value twice. Once in the For (which will automatically increment by 1) and once inside the for loop. So ur icount values are 1,3,5,7... . Since it never reaches 10, it goes into an infinite loop.


for icount = 1 to 10
Votes + Comments
Good Work Here Bud...

I assume you're using that loop as some way to make the computer wait a moment betwen sending commands. Yes, you are incrementing twice but that's not your problem.

The problem is that the loop isn't doing anything. Counting to 10 (even 100 or 1000) is fast, very fast. RS232 is slow, very (VERY) slow. Most of the first packet of data is still in the output buffer when you chuck the second packet in right behind it so, from the motor's perspective, there's no gap between the commands which confuses the motor controller.

Use the system timer to force processing to wait a moment between commands like this:-

dim t as single

' send first command
MSComm.Output = "MOT1SPF1000"

Do While MSComm.OutBufferCount > 0
   ' wait here till all the first command was sent

' now lets wait a little longer

t = Timer
Do While Timer < t + 0.5
    ' This will make processing loop here for half a second

' Now send the second command .
MSComm.Output = "MOT2SPF1000"

Look at some sample code at the <<plug removed>>

Edited by Nick Evan: Removed plug

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.