Bluetooth connections and mutiple threads

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

Moderators: 99jonathan, roger, imaqine

Bluetooth connections and mutiple threads

Postby freeall » Fri Oct 05, 2007 6:30 am

Hi.

Another thread a bit like this, viewtopic.php?t=439.

We wanted to do a multithreaded instance of several bluetooth connections. First off, we're not even sure that lejOS accepts more than one bluetooth connection, but so far we're getting static.

How this code (should) work:
1. When main class (class Factory) initializes create a new thread (class ListenComm) that waits for any bluetooth connection.
2. When connection is made, create a new thread (class CommThread), that takes a BTConnection and then binds this thread to that BTConnection (i.e, the one that just connected)
3. In that new thread, whenever the client sends anything to the CommThread, then put that in to a QUEUE that out main class (class Factory) then reads.

But something does not work, because we create the connection and all the new thread fine. The thing is just that the chars sent from the client does not match what we're seing as output on the screen.

We have tried before with just a simple connection test and there we easily managed to send chars, but in this example it doesn't work.

Does anybody know anything or have any ideas to why this might be? Is it because we have multiple threads that sort of doesn't work well with the bluetooth connection? We had a suspicion that we might run into problems when connecting more devices, but the problem occurs with just one device connecting.

Code: Select all
import lejos.nxt.*;
import lejos.nxt.comm.*;
import java.util.*;
import java.io.*;

public class Factory {
    public static void main(String[] args) {
        Factory f = new Factory();
        f.start();
    }

    private final Motor CONVEYER_BELT = Motor.A;
    private final Motor TOWER = Motor.B;
    private Queue BUFFER;

   public Factory() {
        BUFFER = new Queue();
        Thread listenForBTConns = new ListenComm();
        listenForBTConns.setDaemon(true);
        listenForBTConns.start();
    }

    public void start() {
        int counter = 0;
        while (true) {
            counter++;
            LCD.drawInt(counter, 0, 7);
            LCD.refresh();

//         if (!BUFFER.isEmpty()) {
//             LCD.drawString((String)BUFFER.pop(), 0, 0);
//             LCD.drawString("bleh", 0, 1);
//             LCD.refresh();
//        } else {

//             }
            try {
                Thread.sleep(1000);
            }
            catch(Exception e) { }
        }
    }
   
    private class CommThread extends Thread {
        private DataInputStream in;
   
        public CommThread(BTConnection conn) throws Exception {
            super();
            in = conn.openDataInputStream();
        }

        public void run() {
            String chr_buff = "";
            while (true) {
                try {
                    LCD.drawString("read 1 ", 0, 2);
                    LCD.refresh();
                    char c = in.readChar();
                    LCD.drawString("read  2 [" + c + "]", 0, 2);
                    LCD.refresh();
                    Thread.sleep(500);
                    if (c == '\n') {
//                         BUFFER.push(chr_buff);
                        LCD.drawString(chr_buff, 0, 3);
                        LCD.refresh();
                        chr_buff = "";
                    } else {
                        chr_buff += c;
                    }
                }
                catch (Exception e) {
                    LCD.drawString("Exception 1", 0, 4);
                    LCD.refresh();
                }
            }
        }
    }

    private class ListenComm extends Thread {
        public void run() {
            while (true) {
                LCD.drawString("listen 1 ", 0, 1);
                LCD.refresh();
                BTConnection conn = Bluetooth.waitForConnection();
                LCD.drawString("listen  2", 0, 1);
                LCD.refresh();
                try {
                    Thread.sleep(1000);
                    Thread thread = new CommThread(conn);
                    thread.setDaemon(true);
                    thread.start();
                }
                catch (Exception e) {
                    LCD.drawString("Exception 2", 0, 5);
                    LCD.refresh();
                }
            }
        }
    }
}
freeall
New User
 
Posts: 6
Joined: Fri Oct 05, 2007 6:13 am

doubt about setDaemon method

Postby esmetaman » Sun Oct 07, 2007 12:37 pm

Hi,

Why did you use:




Code: Select all
thread.setDaemon(true);


thanks
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby freeall » Mon Oct 08, 2007 8:41 pm

Hi.

Thanks for your reply.

We used that because then the program exits when the main thread exits. If this new thread is not a daemon, then the program does not exit, until this thread also exits. This was at least our intention :)

But I think there might be a bug somewhere in the bluetooth code. Just can't seem to find where it might be.
freeall
New User
 
Posts: 6
Joined: Fri Oct 05, 2007 6:13 am

Postby esmetaman » Wed Oct 10, 2007 10:32 pm

Try to use code idea in NXT comm to create a set of classes to send and receive data.

I used LCDUI library to manage actions (Send/receive)

can you help me?
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
https://github.com/jabrena/livingrobots
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 311
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain

Postby lawrie » Fri Oct 12, 2007 7:00 pm

Hi freeall,

The problem is that the BC4 chip in the NXT can either be in command mode or data mode. While it is waiting for a connection it is in command mode. After is has connection, it is in data mode.

After you have connected, your ListenComm thread starts waiting for a new connection. This will put the BC4 chip back into command mode, and you will receive no data.

You need to somehow make ListenComm wait and not listen for a new connection until CommThread has processed all the data and closed the connection.
lawrie
leJOS Team Member
 
Posts: 934
Joined: Mon Feb 05, 2007 1:27 pm

Postby freeall » Fri Nov 02, 2007 9:47 am

Thanks for your help, lawrie, and sorry for not replying.

It really did the trick. Now we do this thing where we first make all BT devices connect to this master device and after that we have the master device sent a START signal out to all of them.

So first in command mode until the master device knows that everyone has connected, and then the switch to data mode.

For what it's worth we actually haven't tried with more than one connection yet, hehe.... but our code "should" be able to handle it, if Lejos doesn't mind several BT connections. We'll find out later.

Thanks again.
freeall
New User
 
Posts: 6
Joined: Fri Oct 05, 2007 6:13 am


Return to NXJ Software

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

more stuff