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

Nxt bluetooth issue
http://www.robotc.net/forums/viewtopic.php?f=1&t=4935
Page 1 of 1

Author:  brianchen11 [ Sun Sep 30, 2012 11:29 pm ]
Post subject:  Nxt bluetooth issue

I am trying to send messages to the nxt via bluetooth using c#. The nxt reads the data and then displays it on the screen.
However, I am confused on what is wrong with the program though I'm quite sure the issue is in the c# code, not RobotC.
It is highly likely that the bytes I'm sending is wrong. If so, what should they be?

The c# code is
Code:
using System;
using System.Data;
using System.IO.Ports;

namespace nxtBlueTooth
{
   static class main
   {   
      static void Main()
        {
         string hello = "ssssss";
         
         byte[] Command;         //Array of what you wannt to send to the NXT
         Command = new byte[hello.Length+7];
         Command [0] = (byte)(hello.Length + 5);
         Command [1] = 0;
         Command [2] = 80;
         Command [3] = 9;
         Command [4] = 0;
         Command [5] = (byte)(hello.Length + 1);   
         
         
         int ReplyLength = 10;
         string s1 = "";
         
         for(int i = 0; i < hello.Length; i++){
            Command[i + 6] = (byte)hello[i];
         }
         for(int i = 0; i < hello.Length+6; i++){
            Console.WriteLine(Command[i]);
         }
         Command [hello.Length+6] = 0;
         
         SerialPort BluetoothConnection= new SerialPort();
         BluetoothConnection.PortName = "COM4";
         // Replace this COM port by the appropriate one on your computer
         
         BluetoothConnection.Parity = Parity.None;
         BluetoothConnection.DataBits = 8;
         BluetoothConnection.StopBits = StopBits.One;      
         
         BluetoothConnection.Open();
         
         
         BluetoothConnection.Write(Command, 0, Command.Length);
         
         
         //for(int i=0;i<ReplyLength ;i++)          I am currently not getting replies from the NXT, all I want is to send
         //{
         //   s1+=BluetoothConnection.ReadByte();
         //   Console.Write(s1);
         //}
         BluetoothConnection.Close();
           
        }
   }
}


And the RobotC code is:
Code:
task main()
{
   string str1 = "";
   const int kMaxSizeOfMessage = 56;
   ubyte nReceiveBuffer[kMaxSizeOfMessage];



   while(true){
      if(nBTCurrentStreamIndex >= 0)  // if there is currently an open Bluetooth connection:
      {
        nxtDisplayTextLine(1,"Success");
      }
      else{
         nxtDisplayTextLine(1,"Fail");
      }
      TFileIOResult messageIn = cCmdMessageRead(nReceiveBuffer, kMaxSizeOfMessage, mailbox1);
      for(int i=0;i<kMaxSizeOfMessage;i++){
         str1 += nReceiveBuffer[i];
         if(nReceiveBuffer[i] != 0){
            nxtDisplayTextLine(6,"EWFG");
         }
      }

      nxtDisplayTextLine(3,"%s",str1);
      nxtDisplayTextLine(5,"%3d, %3d",cCmdBTCheckStatus(nBTCurrentStreamIndex));
      wait1Msec(10);


   }
}

Author:  JohnWatson [ Tue Oct 09, 2012 12:40 pm ]
Post subject:  Re: Nxt bluetooth issue

The ROBOTC side of the code seems to be in order (especially if the NXT is receiving the data and displaying it properly). For the C# code, I would suggest posting it in a C# specific forum (such as Microsoft's C Sharp answer database: http://social.msdn.microsoft.com/Forums ... sualcsharp) as I am not sure how fluent in C# anyone here may be.

Author:  Spiked3 [ Tue Oct 09, 2012 2:12 pm ]
Post subject:  Re: Nxt bluetooth issue

I'd have to code it up and see, which I have not done, but off the top of my head;

does

Code:
for(int i = 0; i < hello.Length; i++){
            Command[i + 6] = (byte)hello[i];


do what you think it does? try printing the hex value of (byte)hello[i] to see. I think you may need to use ASCIIEncoding.UTF8.GetBytes instead.

Also, I don't have the pdf doc in front of me, but is Command [2] = 80; supposed to be in hex instead?

Like I said, no code in front of me ATM, but those are 2 things I would verify first.

Author:  Spiked3 [ Tue Oct 09, 2012 2:21 pm ]
Post subject:  Re: Nxt bluetooth issue

On the robotC side, I'm not sure what this is trying to accomplish

Code:
 
     TFileIOResult messageIn = cCmdMessageRead(nReceiveBuffer, kMaxSizeOfMessage, mailbox1);
      for(int i=0;i<kMaxSizeOfMessage;i++){
         str1 += nReceiveBuffer[i];
         if(nReceiveBuffer[i] != 0){
            nxtDisplayTextLine(6,"EWFG");
         }
      }


Does it do anything now?

Author:  brianchen11 [ Sun Oct 21, 2012 3:17 am ]
Post subject:  Re: Nxt bluetooth issue

Sorry, I haven't done much robotics lately, and haven't checked the forum.
I have to admit, my original code was a bit dodgy.

I have figured how to do it in raw mode now.

Attached is a c# application (the code in a text file).
Not only can you send messages from a text box, you can draw in an area, and the program will send the coordinate info to the NXT. It will also send if you're erasing or drawing.

And the robotc code is:

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);
}


Attachments:
File comment: The code for the c# exe
c#applicationcode.txt [8.8 KiB]
Downloaded 533 times

Author:  Guillaume17 [ Thu Jul 18, 2013 9:00 am ]
Post subject:  Re: Nxt bluetooth issue

Hello,

I get to send a message to the NXT brick but can we send a message to the brick to the computer?

Author:  roboRed [ Fri Aug 02, 2013 11:44 pm ]
Post subject:  Re: Nxt bluetooth issue

Do you mean like a call, Computer to NXT and a Response, NXT to Computer?

Author:  Guillaume17 [ Sun Aug 04, 2013 7:30 pm ]
Post subject:  Re: Nxt bluetooth issue

roboRed wrote:
Do you mean like a call, Computer to NXT and a Response, NXT to Computer?


NXT to Computer and I am also interested to know the other

Author:  roboRed [ Mon Aug 05, 2013 12:12 am ]
Post subject:  Re: Nxt bluetooth issue

In that case, Here. http://www.robotc.net/wiki/Debug_Stream

:P :P

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