/// <summary> /// Sends a string to the NXT, useful for your custom NXT-g program /// </summary> /// <param name="message">Message to send, kindly limit to 59 characters, you may risk it if you wont :)</param> /// <param name="inbox">Inbox to drop the message to NXT, default is inbox 1 on NXT</param> public void SendString(string message, NxtInbox inbox = NxtInbox.Inbox1) { // byte command derived from my nxtblue phone controller // check "Appendix 1-LEGO MINDSTORMS NXT Communication protocol" for reference (MESSAGEWRITE) int totalLen = 6 + message.Length + 1; // +6 for the header; +1 for the terminator char later byte[] cmd = new byte[totalLen]; int lsb, msb; // convert the length to lsb and msb lsb = (totalLen - 2) & 0x0F; // just get from command type, byte 2 msb = 0; cmd[0] = (byte)lsb; // byte 0 and 1 are for the message size cmd[1] = (byte)msb; cmd[2] = 0x80; // command type (this case it is direct command no response cmd[3] = 0x09; // command means "messagewrite" switch (inbox) { case NxtInbox.Inbox1: // inbox on NXT, in our case this is inbox 0 cmd[4] = (byte)0x00; break; default: // default to inbox 0 cmd[4] = (byte)0x00; break; } message += '\0'; // add the terminating \0 to the message cmd[5] = (byte)(message.Length); // length of message, plus terminator \0 Encoding.ASCII.GetBytes(message).CopyTo(cmd, 6); // add the message sendMessage(cmd); // send the message finally }
/// <summary> /// Sends a string to the NXT, useful for your custom NXT-g program /// </summary> /// <param name="message">Message to send, kindly limit to 59 characters, you may risk it if you wont :)</param> /// <param name="inbox">Inbox to drop the message to NXT, default is inbox 1 on NXT</param> public void SendString(string message, NxtInbox inbox = NxtInbox.Inbox1) { // byte command derived from my nxtblue phone controller // check "Appendix 1-LEGO MINDSTORMS NXT Communication protocol" for reference (MESSAGEWRITE) int totalLen = 6 + message.Length + 1; // +6 for the header; +1 for the terminator char later byte[] cmd = new byte[totalLen]; int lsb, msb; // convert the length to lsb and msb lsb = (totalLen-2) & 0x0F; // just get from command type, byte 2 msb = 0; cmd[0] = (byte)lsb; // byte 0 and 1 are for the message size cmd[1] = (byte)msb; cmd[2] = 0x80; // command type (this case it is direct command no response cmd[3] = 0x09; // command means "messagewrite" switch (inbox) { case NxtInbox.Inbox1: // inbox on NXT, in our case this is inbox 0 cmd[4] = (byte)0x00; break; default: // default to inbox 0 cmd[4] = (byte)0x00; break; } message += '\0'; // add the terminating \0 to the message cmd[5] = (byte)(message.Length); // length of message, plus terminator \0 Encoding.ASCII.GetBytes(message).CopyTo(cmd, 6); // add the message sendMessage(cmd); // send the message finally }