as I have 3 slaves, I only have 3 mailboxes for each slave on the master. I use them for 1 outbox and 2 inboxes for each slave (on the slaves vice versa):
Code: Select all
// master: connection channels and mailboxes to the slaves.
#define BT_CONN_1 1 // Slave 1
#define OUTBOX_1 1 // out: bool or cmd_string
#define INBOX_11 2 // sensors + variables
#define INBOX_12 3 // ack (requested)
#define BT_CONN_2 2 // Slave 2
#define OUTBOX_2 4 // out: bool or cmd_string
#define INBOX_21 5 // sensors + variables
#define INBOX_22 6 // ack (requested)
#define BT_CONN_3 3 // Slave 3
#define OUTBOX_3 7 // out: bool or cmd_string
#define INBOX_31 8 // sensors + variables
#define INBOX_32 9 // ack (requested)
the 2 different outboxes are used to return
1) the requested value and
2) an acknoldge byte.
Wouldn't cause the fact problems, that both a bool and a string come in at a slave via the same inbox and require different handlings?
Code: Select all
// send and receive tasks on each slave
#define BT_CONN 0
#define INBOX 1 // requests and exec_commands
#define OUTBOX_1 2 // sensor + variable values to send back
#define OUTBOX_2 3 // acknowledge to send back
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
task BTReceive() {
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
string in;
int ack, len;
char bResult;
while(true) {
in= " ";
JHSReceiveRemoteString(BT_CONN, INBOX, true, in, bResult); // INBOX: incoming cmd string containing exec_cmd
ack=checksum(in);
JHSSendResponseNumber(BT_CONN, OUTBOX_2, ack); // check / acknowledge
Wait(1);
__cmd=in;
start ExecCmd;
Wait(1);
}
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
task BTMBxWrite() {
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
string sn, sv;
string fmtstr, buf, out;
int i, dec;
char bResult;
while(true) {
out="";
JHSReceiveRemoteBool( BT_CONN, INBOX, true, bResult); // INBOX: incoming request to send values back
for(i=0; i<11; i++) { // Sensorports: 0...3 plus virtual ports
GetSensorValues(i);
dec=SensorValueDecimals[i];
sn=NumToStr(dec); // value decimals
fmtstr=StrCat("%",NumToStr(dec),"d");
sv=FormatNum(fmtstr,SensorCurrArr);
out = StrCat(out, sn, sv);
} // for port i=...
if (StrLen(out)>58) strncpy(out,out,58); // 58 = max msg length
JHSSendResponseString(BT_CONN, OUTBOX_1, out);
//TextOut(0, 0, out);
Wait(1);
} // while(true)
}