ROBOTC.net forums
http://www.robotc.net/forums/

[C# & RobotC] Communicating by bluetooth always returns 240
http://www.robotc.net/forums/viewtopic.php?f=1&t=7825
Page 1 of 1

Author:  absorr [ Thu Jan 30, 2014 9:03 am ]
Post subject:  [C# & RobotC] Communicating by bluetooth always returns 240

No matter what message I send to the NXT brick via Bluetooth, it always thinks that I am saying 240. It connects very well, it sends the message, but it always thinks it is 240. I have no idea which side is causing it. My code is bellow. Any thoughts would be helpful. Thanks!

Server Side (Console App - C#)
Code:
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleBluetoothMsg
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort BluetoothConnection = new SerialPort();
            Console.Write("Enter COM port number > COM");
            BluetoothConnection.PortName = "COM" + Console.ReadLine();
            // Replace this COM port by the appropriate one on your computer
            BluetoothConnection.Open();
            BluetoothConnection.ReadTimeout = 1500;
            Console.Write("Enter message > ");
            //string cmd = Console.ReadLine();
            //byte[] Command = Encoding.ASCII.GetBytes(cmd);
            //string head = Command.Length.ToString();
            //byte[] Head = Encoding.ASCII.GetBytes(head);
            //BluetoothConnection.Write(Head, 0, Head.Length);
            //BluetoothConnection.Write(Command, 0, Command.Length);
            SendMsg(Console.ReadLine(), false, 0, BluetoothConnection);
            BluetoothConnection.Close();
        }

        public static void SendMsg(string message, bool reply, int inbox, SerialPort BluetoothConnection)
        {
            byte[] command;
            int length = message.Length;
            command = new byte[length + 7];

            int i = 0;

            if (length + 6 > 55)
            {
                length = 49;
            }
            command[0] = (byte)(length + 5);  //command length
            command[1] = 0x00;
            if (reply)
            {
                command[2] = 0x00;
            }
            else
            {
                command[2] = 0x80;
            }
            command[3] = 0x09;
            if (inbox > 9)
            {
                inbox = 9;
            }
            command[4] = (byte)inbox;
            command[5] = (byte)(length + 1);
            while (i < length)
            {
                command[i + 6] = (byte)message[i];
                i++;
            }
            command[i + 6] = (byte)'\0';
            BluetoothConnection.Write(command, 0, length + 6);
        }   

    }
}


Client Side (NXT - C)
Code:
task main()
{
   string str1 = "";
   const int kMaxSizeOfMessage = 56;
   ubyte nReceiveBuffer[kMaxSizeOfMessage];

   setBluetoothRawDataMode();  // set Bluetooth to "raw mode"
   while (!bBTRawMode)         // while the Bluecore is still NOT in raw mode (bBTRawMode == false):
   {
     wait1Msec(1);               // wait for Bluecore to enter raw data mode
   }

   while(nNxtButtonPressed != 3){
      if(nBTCurrentStreamIndex >= 0)  // if there is currently an open Bluetooth connection:
      {
        nxtDisplayTextLine(1,"Success");
         nxtReadRawBluetooth(nReceiveBuffer, 6);
         int length = (int)nReceiveBuffer[0] - 1;
         nxtReadRawBluetooth(nReceiveBuffer, length);
         for(int i = 0; (byte)nReceiveBuffer[i] != 0; i++){
            str1 += (char)nReceiveBuffer[i];
         }
         nxtDisplayTextLine(2,"%s",str1);
         length = 0;
         str1 = "";
      }
      else{
         nxtDisplayTextLine(1,"Fail");
         nxtDisplayTextLine(5,"%3d, %3d",cCmdBTCheckStatus(nBTCurrentStreamIndex));
      }

      wait1Msec(20);

   }
   wait1Msec(10);
   btDisconnectAll();
   wait1Msec(10);
}

Author:  BurningLights [ Thu Jan 30, 2014 10:47 am ]
Post subject:  Re: [C# & RobotC] Communicating by bluetooth always returns

I believe the problem would be that you are not sending the correct bytes before your message data. I have done Bluetooth communication with an NXT with great success from a C# program. I would recommend that you download and read Lego's Bluetooth Developer Kit to get an idea of what the NXT is looking for when it receives Bluetooth messages.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/