Here’s a very simple demo using two IQ2 brains. crossover cable (swap scl/sda) with gnd is needed, disconnect power and the other signals.
One sends (well, technically they both send in this demo) the string “Hello” the other is receiving it.
Here is the C++ code, in practice for reliable communication you need to invent a suitable protocol handling partial reception of data, error checking etc.
C++ demo code
/*----------------------------------------------------------------------------*/
/* */
/* Module: main.cpp */
/* Author: james */
/* Created: 12/9/2022, 2:42:19 PM */
/* Description: IQ2 project */
/* */
/*----------------------------------------------------------------------------*/
#include <fcntl.h>
#include <unistd.h>
#include "vex.h"
using namespace vex;
// A global instance of vex::brain used for printing to the IQ2 brain screen
vex::brain Brain;
void
transmitTask() {
int fd = 0;
int nTotalTx = 0;
fd = open("/dev/port1", O_RDWR );
if( fd < 0 ) {
printf("port error\n");
return;
}
while(true) {
int nWritten = write( fd, "Hello", 5);
nTotalTx += nWritten;
Brain.Screen.printAt( 5, 30, "tx: %8d", nTotalTx);
this_thread::sleep_for(50);
}
}
void
receiveTask() {
int fd = 0;
static char buf[128];
int nTotalRx = 0;
fd = open("/dev/port7", O_RDWR );
if( fd < 0 ) {
printf("port error\n");
return;
}
// set input non-blocking
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
while(true) {
// non blocking read
int nRead = read( fd, buf, sizeof(buf) );
// see if we have any characters
if( nRead > 0 ) {
// print to stdout in hex
for(int i=0;i<nRead;i++) {
printf("%02X ", buf[i]);
}
printf("\n" );
nTotalRx += nRead;
Brain.Screen.printAt( 5, 50, "rx: %8d", nTotalRx);
}
this_thread::sleep_for(10);
}
}
int main() {
thread ttx(transmitTask);
thread trx(receiveTask);
printf("started\n");
while(1) {
// Allow other tasks to run
this_thread::sleep_for(10);
}
}
Python code would be very similar.
