[Solved] Different Bluetooth Baud Rate

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

[Solved] Different Bluetooth Baud Rate

Postby arduino » Sat Apr 21, 2012 11:50 am

Hi everyone,

I have the plan to connect my Lego Mindstorms running lejos and my Arduino via a serial bluetooth connection.
However, the arduino supports a maximal baudrate of 38400 for stable communication, whereas the bluetooth baudrate in lejos is hard-coded to 460800.
I tried to rebuild the firmware with a new baudrate (changed in bt.c to 38400), but the firmware hungs up at startup every time. The same firmware with 460800 baudrate works fine.
Any idea what is wrong with this? Maybe I have to change more than BAUD_RATE in bt.c such that the firmware works?
Or maybe is there someone who has already rebuilt a firmware with baudrate smaller or equal to 38400 bps for bluetooth connections?
Any help is appreciated! Thanks

arduino
Last edited by arduino on Sun Apr 22, 2012 9:07 am, edited 1 time in total.
arduino
New User
 
Posts: 5
Joined: Sat Apr 21, 2012 11:45 am

Re: Different Bluetooth Baud Rate

Postby gloomyandy » Sat Apr 21, 2012 2:44 pm

I don't think you have any need to change the baudrate in the leJOS firmware. This is simply an internal rate used to talk between the Bluetooth module and the ARM chip. It has no impact (other than absolute speed), on the rate that the NXT can talk to other Bluetooth devices... If you change the baudrate then the NXT ARM chip will no longer be able to communicate with the Bluetooth module (which is why your modified firmware is not working)... If your device is not able to talk to the NXT I suspect that there is some other reason for the problem, not the baudrate being used...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3645
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Different Bluetooth Baud Rate

Postby arduino » Sat Apr 21, 2012 3:14 pm

Thanks for you answer. So, you actually want to tell me that there is no way to change the baudrate at all for external communication? But two Bluetooth devices cannot talk to each other if their baudrates differ, am I right?

If you think that my problem has a different reason, you have suggestion what that could be?

Thanks, arduino
arduino
New User
 
Posts: 5
Joined: Sat Apr 21, 2012 11:45 am

Re: Different Bluetooth Baud Rate

Postby gloomyandy » Sat Apr 21, 2012 3:41 pm

There is nothing to stop two Bluetooth devices from talking to each other with different baud rates. The baud rate really does not mean anything at the Bluetooth level.

What happens when you try to connect the two devices? Have you paired them? Have you tried using your BLuetooth serial device to talk to any other system like a PC to check that it is working ok? When you connect the devices which one are you initiating the connection from?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3645
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Different Bluetooth Baud Rate

Postby arduino » Sat Apr 21, 2012 4:03 pm

Okay, that is good to know, I didn't know this before, that the baud rates have nothing to do at the bluetooth level.
So, I shortly describe what I did:
* Lego Mindstorms is Master in Bluetooth communication, I use some DataIn/OutStream Object for communication
* Arduino is Bluetooth slave, I use a serial terminal for communication (baud 38400)
* I paired them succesfully and I could also transmit some characters between them (both directions)
So now to the problems: Several characters are not transmitted correctly, so they are basically garbage. Some characters are transmitted correctly, but only for arduino baud rate of 38400 bps (any other rate produced total garbage).
I basically thought, that there is some error correction at the bluetooth level, but maybe I am wrong with this.

So the main questions still open to me are the following:
* Does anybody know why only baud rate 38400 works on the arduino site as baud rate for the serial console that is established via bluetooth?
* Is there any error correction at the bluetooth level? If not, is it basically okay that 10 % of all transmitted characters are garbage?
If there is no error correction built in: any suggestions how to efficiently implement a stable error correcting layer upon the bluetooth layer, such that my arduino-to-NXT communication becomes stable?

Thank you all a lot for your fast answers. I learned already a lot.

arduino

P.S.: Yes the arduino works fine for arduino-PC communication.
arduino
New User
 
Posts: 5
Joined: Sat Apr 21, 2012 11:45 am

Re: Different Bluetooth Baud Rate

Postby gloomyandy » Sat Apr 21, 2012 4:13 pm

Hi,
The Bluetooth level contains error correction so it is unlikely that the problem is at that level. What sort of data are you trying to send from the NXT. Post your NXT code and the details of what you see at the other end.

Do you have a link for the Arduino Bluetooth device?

So have you used the same Bluetooth module to talk with a Bluetooth device on a PC? If so did you have to set the baudrate to 38400 on the Aduino? What program did you use to talk to on the PC?

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3645
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Different Bluetooth Baud Rate

Postby arduino » Sat Apr 21, 2012 5:58 pm

Hi,

I try to simply transmit any bits/bytes that correspond to measured sensor data on the arduino. So what I write basically on the arduino site are 3 characters (C language), thus 3 * 8 bits.
My mindstorms code looks like this: (shortened a little bit)
Code: Select all
public class BTTransmitCmd {
   public static void main(String[] args) throws Exception {
      String name = "SeeedBTSlave";
      LCD.drawString("Connecting...", 0, 0);
      LCD.refresh();
      RemoteDevice btrd = Bluetooth.getKnownDevice(name);

      if (btrd == null) {
         LCD.clear();
         LCD.drawString("No such device", 0, 0);
         LCD.refresh();
         Thread.sleep(2000);
         System.exit(1);
      }
      BTConnection btc = Bluetooth.connect(btrd);
      if (btc == null) {
         LCD.clear();
         LCD.drawString("Connect fail", 0, 0);
         LCD.refresh();
         Thread.sleep(2000);
         System.exit(1);
      }
      LCD.clear();
      LCD.drawString("Connected", 0, 0);
      LCD.refresh();
      
      DataInputStream dis = btc.openDataInputStream();
      DataOutputStream dos = btc.openDataOutputStream();
            
      Thread.sleep(500);
      
      // Let Arduino write 2 characters; those will get lost (XXX : this is also a strange artifact)
      dos.write('I');
      dos.write('I');
      dos.flush();
      Thread.sleep(1000);
      LCD.clear();
      LCD.drawString("Boot up done", 0, 0);
      LCD.refresh();
      
      State state = State.IDLE;
      int fail = 0;
      while(!interrupted) {
         // Send current State ...
         dos.write(state.get(0));
         dos.write(state.get(1));
         dos.flush();
         
         // Ask for a sensorvalue
         Sensor se = Sensor.REMOTE;
         dos.write('S');
         dos.write(se.getId());
         dos.flush();
         
         long timeout = System.currentTimeMillis() + 450;
         int cnt = 0;
         int number = 0;
         while (cnt < 3 && System.currentTimeMillis() < timeout) {
            if (dis.available() > 0) {
               cnt++;
               number = number*10 + ((int) (((char) dis.read()) - '0'));
            }
         }
         if (cnt != 3) fail++;
         LCD.drawString("Value..."+number,0,1);
         LCD.drawString("Fail..."+fail,0,2);
         LCD.refresh();
         
         if (number == RemoteAnswer.FORWARD.getId()) {
            forward();
         } else if (number == RemoteAnswer.BACKWARD.getId()) {
            backward();
         } else if (number == RemoteAnswer.IDLE.getId()) {
            idle();
         } else if (number == RemoteAnswer.LEFT.getId()) {
            left();
         } else if (number == RemoteAnswer.RIGHT.getId()) {
            right();
         }
      }

      try {
         LCD.drawString("Closing...    ", 0, 0);
         LCD.refresh();
         dis.close();
         dos.close();
         btc.close();
      } catch (IOException ioe) {
         LCD.drawString("Close Exception", 0, 0);
         LCD.refresh();
      }
   }

The line marked with XXX shows also a strange artifact: The first to characters sent from arduino will be lost, while all characters from the NXT reach the arduino.
The output I get for 38400 baud on arduino site is
���BTSTATE:3

CONNECT:OK

+BTSTATE:4
III write two dummy chars
���DS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)
ID����IDS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)


So the question marks should not be there, but something of the obvious pattern (IDS1).

Using any other feasible baud rate delivers a result like this
4���4 �������P��P��P��P�!�P�@B���������������������4


I use an arduino mega with the 2560 processor und use the following shield:http://www.seeedstudio.com/wiki/index.php?title=Bluetooth_Shield

When Is use the same module for PC-Arduino communication everything works. I use screen as serial console by invoking
Code: Select all
screen /dev/ttyUSB0 38400


[edit] I just rechecked the things: for arduino-PC connection also only 38400 baud rate works which was not apparent to me before. So maybe the problem is really with the arduino code.
Nevertheless, for 38400 everything works out fine, so it also should work perfectly with the NXT, shouldn't it? [/edit]

So, I am actually quite confused about this behaviour.

arduino
arduino
New User
 
Posts: 5
Joined: Sat Apr 21, 2012 11:45 am

Re: Different Bluetooth Baud Rate

Postby gloomyandy » Sat Apr 21, 2012 6:31 pm

Well the first thing you need to do is to open the connection from the NXT in RAW mode. If you do not then by default leJOS will use packet mode which is a Lego compatible mode that adds additional bytes to frame each packet (each "packet" will have a two byte length header). Check the details for the connect call to see how to do this...
http://lejos.sourceforge.net/nxt/nxj/ap ... ang.String, int)
The "lost" characters will be because the NXT is expecting these header bytes.

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3645
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Different Bluetooth Baud Rate

Postby arduino » Sun Apr 22, 2012 9:05 am

Good morning,

Well the first thing you need to do is to open the connection from the NXT in RAW mode.

Good to know. Indeed this solves the 2 byte off-error at the beginning of the communication.
However, it does not solve the problem with the question marks during communication.

But, it was a fault on the arduino site: I learned that the mega 2560 has several hardware serial communication pins, so instead of using the SoftwareSerial (http://arduino.cc/hu/Reference/SoftwareSerial) as suggested on the manufacturer's webpage, i used one of hardware serial ports, and, WOW. Everything works now with every feasible baud rate (I tested for 38400 and 9600).

So thanks your help finding the bug.

arduino
arduino
New User
 
Posts: 5
Joined: Sat Apr 21, 2012 11:45 am


Return to NXJ Software

Who is online

Users browsing this forum: Baidu [Spider] and 1 guest

more stuff