mscomm program problem

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

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.

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
DoEvents
Loop
' now lets wait a little longer
t = Timer
Do While Timer < t + 0.5
' This will make processing loop here for half a second
DoEvents
Loop
' Now send the second command .
MSComm.Output = "MOT2SPF1000"