View unanswered posts | View active topics It is currently Thu Apr 24, 2014 10:57 pm






Reply to topic  [ 2 posts ] 
[C# & RobotC] Communicating by bluetooth always returns 240 
Author Message
Rookie

Joined: Mon Dec 02, 2013 9:37 am
Posts: 2
Post [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);
}


Thu Jan 30, 2014 9:03 am
Profile
Novice
User avatar

Joined: Sat Aug 31, 2013 9:15 am
Posts: 71
Post 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.

_________________
Burning Lights Programming
FTC Team 6100 Chariots of Fire - Programmer (2012-2013)
FTC Team 7468 Blue Chariots of Fire - Programmer (2013-2014)
Check out our team website at http://cof7468.weebly.com/.


Thu Jan 30, 2014 10:47 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.