Zoomkat's home page

Basic source code for Mini-SSC web control

New 9/30/04: You can run this code using qbasic if desired instead of having to compile it. Go here to see a similar web control setup using qbasic running a .bas file to control the parallel port. If you are using windows XP (which does not support the below code), then go here and here to see a different (and simpler) method of writing bytes to the com port.

Below is the basic source code for my main program for controlling the mini-ssc servo controller via the web. The program looks at the length of the string being supplied by the QUERY_STRING or the command line, and breaks the string into various parts and performs operations based on the line length. The string components are seperated by an & which only servers as a visual seperator between components. This some times confuses windows scripting applications, so a * or - could be substituted with no problem. The first section uses the cons: (console) function to write the status: 204 code to STDOUT where the webserver picks it up and acts upon it, sending the code to the brouser. Don't use this if a batch file will be used to send the status: 204 to STDOUT. The program then looks for a command line for input. If no command line input is detected, then the program looks for input from the QUERY_STRING environmental variable set by the web server. If testing with qbasic, remember it doesn't support the command line function. Quickbasic and firstbasic can use the command line. The beep at the very end is for testing with qbasic so I can tell if the program was terminated due to some problem. Check the readme files contained in the zip files containing the compiled files for more examples.

Example strings with a 6 character length for moving a single servo:

127.0.0.1/cgi/ZK1PT1.exe?00&000 servo 00 to position 000
127.0.0.1/cgi/ZK1PT1.exe?03&027 Servo 03 to position 027
127.0.0.1/cgi/ZK1PT1.exe?13&216 servo 13 to position 216

Example string with a 17 character length.

PSETB: 00&000&000&00&000
(servo#)&(servo poisiton)&(time delay)&(servo#)&(servo position)
This control string is 17 characters long. It has a time delay between moving the servos. It can be used to move 2 servos with a time delay between the two, or it can be used to move the same servo to a position, have a time delay, then move the servo to another position.

The basic program:

OPEN "CONS:" FOR OUTPUT AS #1 'rem if using bat file for 204
PRINT #1, "Status: 204"
PRINT #1, ""
PRINT #1, ""
CLOSE #1

a$ = COMMAND$ 'rem this section when testing with qbasic
IF a$ = "" THEN GOTO WEB
IF LEN(a$) = 6 THEN GOTO PT
IF LEN(a$) = 7 THEN GOTO PT1
IF LEN(a$) = 10 THEN GOTO SW1
IF LEN(a$) = 11 THEN GOTO SW2
IF LEN(a$) = 13 THEN GOTO PSETA
IF LEN(a$) = 15 THEN GOTO PSETA1
IF LEN(a$) = 17 THEN GOTO PSETB
IF LEN(a$) = 19 THEN GOTO PSETB1
IF LEN(a$) = 20 THEN GOTO PSETC
IF LEN(a$) = 23 THEN GOTO PSETC1
IF LEN(a$) = 27 THEN GOTO PSETD
IF LEN(a$) = 28 THEN GOTO SW3
IF LEN(a$) = 31 THEN GOTO PSETD1
BEEP
END

WEB:
'a$ = "00&000" 'test string for use with qbasic
a$ = ENVIRON$("QUERY_STRING") 'web server string
IF LEN(a$) = 6 THEN GOTO PT
IF LEN(a$) = 7 THEN GOTO PT1
IF LEN(a$) = 10 THEN GOTO SW1
IF LEN(a$) = 11 THEN GOTO SW2
IF LEN(a$) = 13 THEN GOTO PSETA
IF LEN(a$) = 15 THEN GOTO PSETA1
IF LEN(a$) = 17 THEN GOTO PSETB
IF LEN(a$) = 19 THEN GOTO PSETB1
IF LEN(a$) = 20 THEN GOTO PSETC
IF LEN(a$) = 23 THEN GOTO PSETC1
IF LEN(a$) = 27 THEN GOTO PSETD
IF LEN(a$) = 28 THEN GOTO SW3
IF LEN(a$) = 31 THEN GOTO PSETD1
BEEP
END

PT: '00&000, 6
servo$ = MID$(a$, 1, 2)
deg$ = MID$(a$, 4, 3)
servo = VAL(servo$)
deg = VAL(deg$)
IF servo > 99 THEN GOTO FINI
IF servo < 0 THEN GOTO FINI
IF deg > 254 THEN GOTO FINI
IF deg < 0 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo); CHR$(deg);
CLOSE #2
END

PT1: '000&000, 7
servo$ = MID$(a$, 1, 3)
deg$ = MID$(a$, 5, 3)
servo = VAL(servo$)
deg = VAL(deg$)
IF servo > 254 THEN GOTO FINI
IF servo < 0 THEN GOTO FINI
IF deg > 254 THEN GOTO FINI
IF deg < 0 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo); CHR$(deg);
CLOSE #2
END

PSETB: '00&000&000&00&000, 17
servo1$ = MID$(a$, 1, 2)
deg1$ = MID$(a$, 4, 3)
servo1 = VAL(servo1$)
deg1 = VAL(deg1$)
servo2$ = MID$(a$, 12, 2)
deg2$ = MID$(a$, 15, 3)
servo2 = VAL(servo2$)
deg2 = VAL(deg2$)
sec$ = MID$(a$, 8, 3)
sec! = VAL(sec$)
IF servo1 > 99 THEN GOTO FINI
IF servo1 < 0 THEN GOTO FINI
IF deg1 > 254 THEN GOTO FINI
IF deg1 < 0 THEN GOTO FINI
IF servo2 > 99 THEN GOTO FINI
IF servo2 < 0 THEN GOTO FINI
IF deg2 > 254 THEN GOTO FINI
IF deg2 < 0 THEN GOTO FINI
IF sec! < 0 THEN GOTO FINI
IF sec! > 999 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo1); CHR$(deg1);
now! = TIMER
DO
LOOP UNTIL TIMER > (now! + sec!)
PRINT #2, CHR$(255); CHR$(servo2); CHR$(deg2);
CLOSE #2
END

PSETB1: '000&000&000&000&000, 19
servo1$ = MID$(a$, 1, 3)
deg1$ = MID$(a$, 5, 3)
servo1 = VAL(servo1$)
deg1 = VAL(deg1$)
servo2$ = MID$(a$, 13, 3)
deg2$ = MID$(a$, 17, 3)
servo2 = VAL(servo2$)
deg2 = VAL(deg2$)
sec$ = MID$(a$, 9, 3)
sec! = VAL(sec$)
IF servo1 > 254 THEN GOTO FINI
IF servo1 < 0 THEN GOTO FINI
IF deg1 > 254 THEN GOTO FINI
IF deg1 < 0 THEN GOTO FINI
IF servo2 > 254 THEN GOTO FINI
IF servo2 < 0 THEN GOTO FINI
IF deg2 > 254 THEN GOTO FINI
IF deg2 < 0 THEN GOTO FINI
IF sec! < 0 THEN GOTO FINI
IF sec! > 999 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo1); CHR$(deg1);
now! = TIMER
DO
LOOP UNTIL TIMER > (now! + sec!)
PRINT #2, CHR$(255); CHR$(servo2); CHR$(deg2);
CLOSE #2
END

PSETA: '00&000&00&000, 13
servo1$ = MID$(a$, 1, 2)
deg1$ = MID$(a$, 4, 3)
servo1 = VAL(servo1$)
deg1 = VAL(deg1$)
servo2$ = MID$(a$, 8, 2)
deg2$ = MID$(a$, 11, 3)
servo2 = VAL(servo2$)
deg2 = VAL(deg2$)
IF servo1 > 99 THEN GOTO FINI
IF servo1 < 0 THEN GOTO FINI
IF deg1 > 254 THEN GOTO FINI
IF deg1 < 0 THEN GOTO FINI
IF servo2 > 99 THEN GOTO FINI
IF servo2 < 0 THEN GOTO FINI
IF deg2 > 254 THEN GOTO FINI
IF deg2 < 0 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo1); CHR$(deg1);
PRINT #2, CHR$(255); CHR$(servo2); CHR$(deg2);
CLOSE #2
END

PSETA1: '000&000&000&000, 15
servo1$ = MID$(a$, 1, 3)
deg1$ = MID$(a$, 5, 3)
servo1 = VAL(servo1$)
deg1 = VAL(deg1$)
servo2$ = MID$(a$, 9, 3)
deg2$ = MID$(a$, 13, 3)
servo2 = VAL(servo2$)
deg2 = VAL(deg2$)
IF servo1 > 254 THEN GOTO FINI
IF servo1 < 0 THEN GOTO FINI
IF deg1 > 254 THEN GOTO FINI
IF deg1 < 0 THEN GOTO FINI
IF servo2 > 254 THEN GOTO FINI
IF servo2 < 0 THEN GOTO FINI
IF deg2 > 254 THEN GOTO FINI
IF deg2 < 0 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo1); CHR$(deg1);
PRINT #2, CHR$(255); CHR$(servo2); CHR$(deg2);
CLOSE #2
END

SW1: '00&000&000, 10
servo$ = MID$(a$, 1, 2)
deg$ = MID$(a$, 4, 3)
sec$ = MID$(a$, 8, 3)
servo = VAL(servo$)
deg = VAL(deg$)
sec! = VAL(sec$)
IF servo > 99 THEN GOTO FINI
IF servo < 0 THEN GOTO FINI
IF deg > 254 THEN GOTO FINI
IF deg < 0 THEN GOTO FINI
IF sec! < 0 THEN GOTO FINI
IF sec! > 999 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo); CHR$(deg);
now! = TIMER
DO
LOOP UNTIL TIMER > (now! + sec!)
PRINT #2, CHR$(255); CHR$(servo); CHR$(127);
CLOSE #2
END

SW2: '000&000&000, 11
servo$ = MID$(a$, 1, 3)
deg$ = MID$(a$, 5, 3)
sec$ = MID$(a$, 9, 3)
servo = VAL(servo$)
deg = VAL(deg$)
sec! = VAL(sec$)
IF servo > 254 THEN GOTO FINI
IF servo < 0 THEN GOTO FINI
IF deg > 254 THEN GOTO FINI
IF deg < 0 THEN GOTO FINI
IF sec! < 0 THEN GOTO FINI
IF sec! > 999 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo); CHR$(deg);
now! = TIMER
DO
LOOP UNTIL TIMER > (now! + sec!)
PRINT #2, CHR$(255); CHR$(servo); CHR$(127);
CLOSE #2
END

SW3: '00&000&000&00&000&000&00&000, 28
servo1$ = MID$(a$, 1, 2)
deg1$ = MID$(a$, 4, 3)
sec1$ = MID$(a$, 8, 3)
servo1 = VAL(servo1$)
deg1 = VAL(deg1$)
sec1! = VAL(sec1$)
servo2$ = MID$(a$, 12, 2)
deg2$ = MID$(a$, 15, 3)
sec2$ = MID$(a$, 19, 3)
servo2 = VAL(servo2$)
deg2 = VAL(deg2$)
sec2! = VAL(sec2$)
servo3$ = MID$(a$, 23, 2)
deg3$ = MID$(a$, 26, 3)
servo3 = VAL(servo3$)
deg3 = VAL(deg3$)
IF servo1 > 99 THEN GOTO FINI
IF servo1 < 0 THEN GOTO FINI
IF deg1 > 254 THEN GOTO FINI
IF deg1 < 0 THEN GOTO FINI
IF sec1! < 0 THEN GOTO FINI
IF sec1! > 999 THEN GOTO FINI
IF servo2 > 99 THEN GOTO FINI
IF servo2 < 0 THEN GOTO FINI
IF deg2 > 254 THEN GOTO FINI
IF deg2 < 0 THEN GOTO FINI
IF sec2! < 0 THEN GOTO FINI
IF sec2! > 999 THEN GOTO FINI
IF servo3 > 99 THEN GOTO FINI
IF servo3 < 0 THEN GOTO FINI
IF deg3 > 254 THEN GOTO FINI
IF deg3 < 0 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo1); CHR$(deg1);
now! = TIMER
DO
LOOP UNTIL TIMER > (now! + sec1!)
PRINT #2, CHR$(255); CHR$(servo2); CHR$(deg2);
now! = TIMER
DO
LOOP UNTIL TIMER > (now! + sec2!)
PRINT #2, CHR$(255); CHR$(servo3); CHR$(deg3);
CLOSE #2
END

PSETC: '00&000&00&000&00&000, 20
servo1$ = MID$(a$, 1, 2)
deg1$ = MID$(a$, 4, 3)
servo1 = VAL(servo1$)
deg1 = VAL(deg1$)
servo2$ = MID$(a$, 8, 2)
deg2$ = MID$(a$, 11, 3)
servo2 = VAL(servo2$)
deg2 = VAL(deg2$)
servo3$ = MID$(a$, 15, 2)
deg3$ = MID$(a$, 18, 3)
servo3 = VAL(servo3$)
deg3 = VAL(deg3$)
IF servo1 > 99 THEN GOTO FINI
IF servo1 < 0 THEN GOTO FINI
IF deg1 > 254 THEN GOTO FINI
IF deg1 < 0 THEN GOTO FINI
IF servo2 > 99 THEN GOTO FINI
IF servo2 < 0 THEN GOTO FINI
IF deg2 > 254 THEN GOTO FINI
IF deg2 < 0 THEN GOTO FINI
IF servo3 > 99 THEN GOTO FINI
IF servo3 < 0 THEN GOTO FINI
IF deg3 > 254 THEN GOTO FINI
IF deg3 < 0 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo1); CHR$(deg1);
PRINT #2, CHR$(255); CHR$(servo2); CHR$(deg2);
PRINT #2, CHR$(255); CHR$(servo3); CHR$(deg3);
CLOSE #2
END

PSETC1: '000&000&000&000&000&000, 23
servo1$ = MID$(a$, 1, 3)
deg1$ = MID$(a$, 5, 3)
servo1 = VAL(servo1$)
deg1 = VAL(deg1$)
servo2$ = MID$(a$, 9, 3)
deg2$ = MID$(a$, 13, 3)
servo2 = VAL(servo2$)
deg2 = VAL(deg2$)
servo3$ = MID$(a$, 17, 3)
deg3$ = MID$(a$, 21, 3)
servo3 = VAL(servo3$)
deg3 = VAL(deg3$)
IF servo1 > 254 THEN GOTO FINI
IF servo1 < 0 THEN GOTO FINI
IF deg1 > 254 THEN GOTO FINI
IF deg1 < 0 THEN GOTO FINI
IF servo2 > 254 THEN GOTO FINI
IF servo2 < 0 THEN GOTO FINI
IF deg2 > 254 THEN GOTO FINI
IF deg2 < 0 THEN GOTO FINI
IF servo3 > 254 THEN GOTO FINI
IF servo3 < 0 THEN GOTO FINI
IF deg3 > 254 THEN GOTO FINI
IF deg3 < 0 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo1); CHR$(deg1);
PRINT #2, CHR$(255); CHR$(servo2); CHR$(deg2);
PRINT #2, CHR$(255); CHR$(servo3); CHR$(deg3);
CLOSE #2
END

PSETD: '00&000&00&000&00&000&00&000, 27
servo1$ = MID$(a$, 1, 2)
deg1$ = MID$(a$, 4, 3)
servo1 = VAL(servo1$)
deg1 = VAL(deg1$)
servo2$ = MID$(a$, 8, 2)
deg2$ = MID$(a$, 11, 3)
servo2 = VAL(servo2$)
deg2 = VAL(deg2$)
servo3$ = MID$(a$, 15, 2)
deg3$ = MID$(a$, 18, 3)
servo3 = VAL(servo3$)
deg3 = VAL(deg3$)
servo4$ = MID$(a$, 22, 2)
deg4$ = MID$(a$, 25, 3)
servo4 = VAL(servo4$)
deg4 = VAL(deg4$)
IF servo1 > 99 THEN GOTO FINI
IF servo1 < 0 THEN GOTO FINI
IF deg1 > 254 THEN GOTO FINI
IF deg1 < 0 THEN GOTO FINI
IF servo2 > 99 THEN GOTO FINI
IF servo2 < 0 THEN GOTO FINI
IF deg2 > 254 THEN GOTO FINI
IF deg2 < 0 THEN GOTO FINI
IF servo3 > 99 THEN GOTO FINI
IF servo3 < 0 THEN GOTO FINI
IF deg3 > 254 THEN GOTO FINI
IF deg3 < 0 THEN GOTO FINI
IF servo4 > 99 THEN GOTO FINI
IF servo4 < 0 THEN GOTO FINI
IF deg4 > 254 THEN GOTO FINI
IF deg4 < 0 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo1); CHR$(deg1);
PRINT #2, CHR$(255); CHR$(servo2); CHR$(deg2);
PRINT #2, CHR$(255); CHR$(servo3); CHR$(deg3);
PRINT #2, CHR$(255); CHR$(servo4); CHR$(deg4);
CLOSE #2
END

PSETD1: '000&000&000&000&000&000&000&000, 31
servo1$ = MID$(a$, 1, 3)
deg1$ = MID$(a$, 5, 3)
servo1 = VAL(servo1$)
deg1 = VAL(deg1$)
servo2$ = MID$(a$, 9, 3)
deg2$ = MID$(a$, 13, 3)
servo2 = VAL(servo2$)
deg2 = VAL(deg2$)
servo3$ = MID$(a$, 17, 3)
deg3$ = MID$(a$, 21, 3)
servo3 = VAL(servo3$)
deg3 = VAL(deg3$)
servo4$ = MID$(a$, 25, 3)
deg4$ = MID$(a$, 29, 3)
servo4 = VAL(servo4$)
deg4 = VAL(deg4$)
IF servo1 > 254 THEN GOTO FINI
IF servo1 < 0 THEN GOTO FINI
IF deg1 > 254 THEN GOTO FINI
IF deg1 < 0 THEN GOTO FINI
IF servo2 > 254 THEN GOTO FINI
IF servo2 < 0 THEN GOTO FINI
IF deg2 > 254 THEN GOTO FINI
IF deg2 < 0 THEN GOTO FINI
IF servo3 > 254 THEN GOTO FINI
IF servo3 < 0 THEN GOTO FINI
IF deg3 > 254 THEN GOTO FINI
IF deg3 < 0 THEN GOTO FINI
IF servo4 > 254 THEN GOTO FINI
IF servo4 < 0 THEN GOTO FINI
IF deg4 > 254 THEN GOTO FINI
IF deg4 < 0 THEN GOTO FINI
OPEN "com1:9600,N,8,1,CD0,CS0,DS0,OP0" FOR OUTPUT AS #2
PRINT #2, CHR$(255); CHR$(servo1); CHR$(deg1);
PRINT #2, CHR$(255); CHR$(servo2); CHR$(deg2);
PRINT #2, CHR$(255); CHR$(servo3); CHR$(deg3);
PRINT #2, CHR$(255); CHR$(servo4); CHR$(deg4);
CLOSE #2
END

FINI:
BEEP
END

Counter