View unanswered posts | View active topics It is currently Wed Sep 17, 2014 7:41 pm






Reply to topic  [ 9 posts ] 
Nxt bluetooth issue 
Author Message
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post 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);


   }
}


Sun Sep 30, 2012 11:29 pm
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 576
Post 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.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our Wiki and our Forums.

I just met you,
And this is crazy,
But here's my code now,
So fix it, maybe?
~ Carly Rae Jepsen parody


Tue Oct 09, 2012 12:40 pm
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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.

_________________
Mike aka Spiked3
http://www.spiked3.com


Tue Oct 09, 2012 2:12 pm
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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?

_________________
Mike aka Spiked3
http://www.spiked3.com


Tue Oct 09, 2012 2:21 pm
Profile
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post 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 354 times
Sun Oct 21, 2012 3:17 am
Profile
Rookie

Joined: Wed Jul 10, 2013 1:18 pm
Posts: 8
Post 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?


Thu Jul 18, 2013 9:00 am
Profile
Expert
User avatar

Joined: Fri Nov 02, 2012 12:07 am
Posts: 164
Location: California, USA
Post Re: Nxt bluetooth issue
Do you mean like a call, Computer to NXT and a Response, NXT to Computer?

_________________
string Robored = "Awesome"
~~Neil Balch~~


Fri Aug 02, 2013 11:44 pm
Profile WWW
Rookie

Joined: Wed Jul 10, 2013 1:18 pm
Posts: 8
Post 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


Sun Aug 04, 2013 7:30 pm
Profile
Expert
User avatar

Joined: Fri Nov 02, 2012 12:07 am
Posts: 164
Location: California, USA
Post Re: Nxt bluetooth issue
In that case, Here. http://www.robotc.net/wiki/Debug_Stream

:P :P

_________________
string Robored = "Awesome"
~~Neil Balch~~


Mon Aug 05, 2013 12:12 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 9 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.