Ok. More about communication between Vex IQ and Windows:
Communication is done via USB cable, which connected as usually. Vex IQ driver, which is installed anyway if you use RobotC or Robot Mesh Studio, creates a virtual COM port. Its actually, quite simle to use it from Delphi, because COM port can be opened just like any file in WinAPI. I’ll not describe all details here, you can just re-use CommPort.pas unit from the project. I’ve found it actually, in Internet, and modified slightly. As well, I’ve found and added code, which detects VEX IQ port number; sources show how to connect to VEX IQ port:
procedure TRobot.Connect;
var
nfo: TPortInfo;
begin
if not FPort.SearchPort('VEX', nfo) then
raise ERobotError.Create('No connected VEX IQ found');
FPort.Connect(nfo.Port);
end;
The main problem is that Com port cannot be opened by several application at the same time, so, its required to disconnect from Robot Mesh Studio first to make the port available. So, I use “Build program and download only” mode inside Robot Mesh Studio. It will automatically diconnects from COM port after two-three repeated click to this button. And, as well, your application should also disconnect from COM port, before Robot Mesh Studio will be able to connect to it. So, its not very comfortable
Next, there no “byte-code” or something similar published. Robot Mesh Python just provides Serial class, which can be used read/write bytes at the robot side. So, you can send/receive any bytes between Vex IQ and PC, and its your program task of how to interpret them.
Here its important to note that USB connection we use is generally designed for program downloading and debugging, speaking another words - to interoperating with IDE. This implies two things:
- Not all bytes, send from computer side, can be read with Serial class at robot side. Most data will not be considered as user-defined message. So, (thanks Anthony from Robot Mesh) data, which should be received via Serial class at robot side, should be prefixed with some predefined bytes:
To send data from the PC to the VEXIQ Brain to be read using Serial.read(), you need to prefix the data buffer with the following bytes:
0xc9, 0x36, 0xb8, 0x47, 0x7a, , data,….
e.g. the following would send two bytes: 0xaa, 0xbb:
0xc9, 0x36, 0xb8, 0x47, 0x7a, 2, 0xaa, 0xbb
This is done in TRobot.SendCmd in my sources.
- VEX IQ can send some debuggind data from the brain to PC at unpredictable times moments. And there no ways to distinguish this data from the data, which is explicitly sent to PC from your program using Serial class. So, robot program side should use some longer prefixes while sending data, and PC receiving side should search for such prefixes in in-coming data stream. There no other way.
My program is used htree commands:
01 - Execute setup procedure.
02 - Move to (without drawing, e.g. with raised pen)
03 - Draw to
Two last commands require two angles (for two motors) to be transfered as arguments. I encode every angly as two bytes and so, these commands are five bytes long. At the robot side I use the following code:
if sp.bytes_to_read() > 0:
cmd = sp.read_byte()
for read commands, and:
arg = sp.read_bytes(4)
for read argument angles.
In opposite direction I send only one “ready” notification from robot to PC, which mean that the current command has been executed, and the brain is ready to read next command:
READY_STR = '\x0AVP_\x01\x0A'
sp.write(READY_STR, False)
There no reason, why I choose READY_STR as shows above, you can choose any other string instead. However, as I noted, VEX IQ use “\x0A” char as a debuggins messages terminator char. So, probably, its a good idea to use it also, since it will minimize the chance, that a debugging message will contain READY_STR char sequence.
At PC side this notification is readed in TRobot.WaitForCmd method.