This code does not work. Usually the remote NXT will crash (ticking), sometimes after it has sent a message, sometimes before. The master NXT doesn't crash, and will sometimes show 1 or 2 messages received. I really have no idea where to go from here, is there a tutorial somewhere that shows how to implement something like this?
Both are running NXC enh FW 1.31.
message.nxc (shared code)
Code: Select all
#ifndef MESSAGE
#define MESSAGE
#include "BTlib.nxc"
struct MessageCommand {
       int Command;
};
struct MessageStatus {
       int Status;
       float Azimuth;
       float Elevation;
};
int SendCommand(byte connection, byte mailbox, MessageCommand& command1) {
    string sDataOut;
    sDataOut = FlattenVar(command1);
    BTSendMessage(connection,mailbox,sDataOut);
    return 0;
}
int ReceiveCommand(byte connection, byte mailbox, MessageCommand& command1) {
     string sDataIn;
     sDataIn = BTReceiveMessage(connection, mailbox, TRUE);
     if (strlen(sDataIn) > 0) {
        UnflattenVar(sDataIn, command1);
        return 0;
     }
     return -1;
}
int SendStatus(byte connection, byte mailbox, MessageStatus& status1) {
    string sDataOut;
    sDataOut = FlattenVar(status1);
    BTSendMessage(connection,mailbox,sDataOut);
    return 0;
}
int ReceiveStatus(byte connection, byte mailbox, MessageStatus& status1) {
     string sDataIn;
     sDataIn = BTReceiveMessage(connection, mailbox, TRUE);
     if (strlen(sDataIn) > 0) {
        UnflattenVar(sDataIn, status1);
        return 0;
     }
     return -1;
}
#endif
Code: Select all
#include "NXCDefs.h"
#include "message.nxc"
#define BT_CONN 0 //line where slave is connected
#define MAILBOX 0
string Connected = "N" ;
task HandleRemote() {
     int recv = 0;
     MessageCommand command1;
     MessageStatus status1;
     while (true) {
     while (BTCommCheck(BT_CONN)) {
           Connected = "Y" ;
           if (ReceiveCommand(BT_CONN, MAILBOX, command1) == 0) {
              recv++;
              NumOut(50,LCD_LINE7,recv);
              status1.Status = recv;
              status1.Azimuth = recv * 0.9;
              status1.Elevation = recv * 1.1;
              SendStatus(BT_CONN, MAILBOX, status1);
           }
           Wait(100);
     }
     Connected = "N" ;
     Wait(100);
     }
}
task main() {
     Precedes(HandleRemote);
     TextOut(50,LCD_LINE1,"start" );
}
Code: Select all
#include "NXCDefs.h"
#include "message.nxc"
#define BT_CONN 1
#define MAILBOX 0
int Status1 = 1;
float Azimuth = 0;
float Elevation = 0;
string Connected = "N" ;
task PollMaster() {
     int sent = 0;
     MessageCommand command1;
     command1.Command = 0;
     while (true) {
     TextOut(50,LCD_LINE2,"a" );
     while (BTCommCheck(BT_CONN)) {
           Connected = "Y" ;
           SendCommand(BT_CONN, MAILBOX, command1);
           sent++;
           NumOut(40,LCD_LINE7,sent);
           Wait(1500);
     }
     Connected = "N" ;
     Wait(1000);
     }
}
task HandleMaster() {
     int recv = 0;
     MessageStatus status1;
     while (true) {
     while (BTCommCheck(BT_CONN)) {
           Connected = "Y" ;
           ReceiveStatus(BT_CONN, MAILBOX, status1);
           recv++;
           NumOut(50,LCD_LINE7,recv);
           Status1 = status1.Status;
           Azimuth = status1.Azimuth;
           Elevation = status1.Elevation;
           Wait(50);
     }
     Connected = "N" ;
     Wait(1000);
     }
}
task main() {
     Precedes(PollMaster, HandleMaster);
     TextOut(50,LCD_LINE1,"start" );
}