Rcx code
From Wikipedia, the free encyclopedia
RCX is a brick from Lego Mindstorms. Feel free to go there if you don't know exaxly what it is.
In his standart firmware, the RCX receives byte code from the IR tower. This byte code, instead of being automattically compiled from a compiler, its actually more efficient, or even funnier, to send it your own.
NOTE: from here on, you will neeed advanced I/O knowdeledge from C or C++. NOTE2: This guide only says about transmitting data from USB IR-Tower that comes with RIS 2.
Contents |
[edit] Opening IR-Tower for entering data:
you can use CreateFile to open the tower, or using as a file. The sintaxis will be this:
{
- include <windows.h>
CreateFile ("\\\\.\\legotower1", // \\.\legotower1 is the direction of USB tower. GENERIC_READ | GENERIC_WRITE, //reading and writing 0, 0, OPEN_EXISTING, 0, 0) ; //other stuff
}
[edit] Writing data on th IR tower for sending commands to RCX
you can use the WriteFile command to write on the RCX
- include <windows.h>
... HANDLE hfh = CreateFile ("\\\\.\\legotower1", GENERIC_READ |GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0) ; BYTE data[] = {0x55, 0xff, 0x00, 0x10, ~0x10, 0x10, ~0x10} ; //data - wil be explained later int len = 7 ; //7 bytes to write DWORD bytew ;
WriteFile (hfh, data, len, &bytew, NULL) ; //sending the data ...
[edit] RCX byte code
To send a code to the RCX, it must follow some rules:
1º byte: 0x55 2º byte: 0xff 3º byte: 0x00
4º byte: 1º byte of code 5º byte: bi-inverted(~) 4º byte of byte code ... nº byte: n - 3 byte of byte code (n + 1)º byte: bi-inverted nº byte of byte code
(n + 2)º byte: 1 + ... + n (sum) (n + 3)º byte: bi-inverted sum
note that if you want to send the same code twice, you must turn on/off the 3rd bit
[edit] sample program
/* compile this program on a windows C or C++ compiler; bloodshed dev-cpp or visual c++ will work (C) 2006 windymager */ #include <windows.h> #include <stdio.h> #ifdef LINUX //for va_lists, witch change depenfding system #include <varargs.h> #else #include <stdarg.h> #endif int ntoggle = 0 ; //for 3rd bit checking int chgcode (BYTE *p, int n, va_list s) //make p the byte code or n bytes
{ p[0] = 0x55 ; p[1] = 0xff ; p[2] = 0x00 ; int i = 3 ; int u = 0 ; int sum = 0 ; while (u < n) { p[i] = va_arg (s, int) ; if (ntoggle && !u) p[i] |= 0x08 ; //for bit checking sum += p[i] ; p[i + 1] = ~p[i] ; u++ ; i += 2 ; } p[i] = sum ; p[++i] = ~sum ; return i ; //return length } int send (int n, ...) { HANDLE hfh = CreateFile ("\\\\.\\legotower1", GENERIC_READ | GENERIC_WRITE, 0, 0,OPEN_EXISTING, 0, 0) ; va_list s ; va_start (s, n) ; int i ; for (i = 0 ; i < n ; i++) { DWORD bytew ; BYTE p[50] ; int len = chgcode (p,