RN-42 Bluetooth mldule and Java
ValeT
Posts: 308
I am going to try to get the RN-42 - I still got to get the RN-42 Bluetooth module , but it's coming - to talk to my Windows PC over Bluetooth. I will be using one of Java's many Bluetooth libraries to communicate to the ActivityBoard using the built in Bluetooth module in my computer, but am new to this and would like some help.
Since this is my first time going to be using the RN-42 module, does anyone have any tips or pointers? I am new to using Java with Bluetooth as well, so if someone could give me a link to a great Java Bluetooth tutorial they have found in the past, that would be much appreciated!
Since this is my first time going to be using the RN-42 module, does anyone have any tips or pointers? I am new to using Java with Bluetooth as well, so if someone could give me a link to a great Java Bluetooth tutorial they have found in the past, that would be much appreciated!
Comments
Check out the forum at propellar powered, I think Jeff has done some stuff with Bluetooth.
Jim
If you are a 64 bit Windows user and you get libRxTx working I'd love to know what you did.
thanks. I will check it out and see what I can find out.
Martin_H,
I think I have miscommunicated my message. I would like to have the ActivityBoard as a standalone device and have Java use my computer's built in Bluetooth module. I would like to use Java to force pair with the ActivityBoard and send some simple characters such as 'A', 'B', etc.
Have either of you completed a project dealing with Bluetooth?
Yes I have. I used an eBay Bluetooth module with my BS2 and paired it with my PC's Bluetooth adapter. When I did that the Bluetooth module appeared as a COM port on my PC. I then opened the COM port with my PC program to read/wrote data to it. On the BS2 side I used two pins with the SERIN and SEROUT commands. The actual program was a remote control which used single letter commands to start/stop the drive wheels.
The pairing of a Bluetooth module and the PC is handled by the underlying OS, not Java. How that module is presented to the rest of the OS depends upon the capabilities of the Bluetooth device paired. Most of these Bluetooth modules used with microcontrollers are designed to appear as serial ports. That's why I mentioned libRxTx, Java, and a potential gotcha on 64 bit Windows.
Oh, ok. I see what you are saying.
Do you think then that it would be better to try to write C code to do this then?
I would not run into any problems because C supports this, right?
There would be no problems in C because that can call the host OS directly and use the Windows API's directly. Although you might have better luck getting Java to work than me.
BTW Here's a debugging tip. It will be a pain to debug your PC program and your Activity Board client at the same time. So do them separately. Use the Parallax serial terminal to open the COM port and send commands directly to the Activity Board. Once you get that working completely write the PC program which sends those commands. That way you can debug each part of the system in isolation.
Thanks for the tip! I will work on this tomorrow, and keep you all posted.
I am going to have to learn C++ then........Does anyone have any good tutorials/example programs I can learn from ?
Mr. Lawerence, I have tried to get that to work, but I am having a lot of trouble controlling the settings for the USB Rx_P30 pin. I have attached a piece of example code that I have been using in NetBeans 8.0. I can receive stuff from my ActivityBoard, but when I send stuff, it comes out as rectangles and circles. Do you know how to fix this? I suspect it is the settings that I am using on the ActivityBoard, but I don't know how to change that.
mindrobots, thanks for the link with the library. As you can see I am currently working with the other one, but I looked at the code.
Here is my Java code:
Here is my SimpleIDE code:
Some of the statements are debug statements. Just an FYI
a) I need to get the RN-42 Bluetooth module
b) I have done some research on how to use the RN-42, and it seems that it appears as a serial port on the computer.
Does help clear things up?
That's how I do my wireless serial protocol development as well. Basically get the serial protocol working, then make it wireless. That way you only debug one thing at a time.
Do you have any ideas on how to edit the protocols on the ActivityBoard?
First you need to work on one end at a time and get them working without errors and then combine them.
For testing you can make a loop back tester. RS-232 Loopback: Connect the TXD pin to the RXD pin
Then on the PC you can use a serial terminal program such as Putty for testing. Once you get that end working without errors you can start on the other end.
http://www.ni.com/white-paper/3450/en/
On you PC go to the device drivers >> serial Ports and check the serial port properties(ensure the baud rate is set high enough)
Also, in your PC code I didn't see any flow control. . I always add it even if it's not used.(just a good habit so you don't forget when it's needed)
try { serialPort.setFlowControlMode( // SerialPort.FLOWCONTROL_NONE);
For the activity board side >> I use SPIN and Asm for the Propeller so I'm not setup to test the C code . Jazzed can help you out if he pop's by.
An interactive language (e.g. Forth) simplifies this process because the built in read eval print loop (REPL) is the protocol your PC side program will invoke when the protocol is complete.
I am testing your theories and will post my results within the next day. BTW, the FlowControl statement didn't do anything when I added that into my code. I also changed the port Baud rates, but that didn't do anything.
Attached are pictures of my current results when I run the Java code in NetBeans and when I use Putty. Neither of them have changed.
Currently, when I run the program, I get random numbers back. I can't figure out why this is happening. Does anyone have any idea?
Would someone mind debugging my program and testing it? I have attached my current code below.
c is a char array and you are comparing a single character to the string "16". That will never be true. I'm also not sure about the definition of dprint, but you need to ensure it's expecting a character because that's what you are giving it.
Thanks, but it didn't fix the problem. Do you think anything could be wrong on the Java side of the code?
The sort of problems are way to difficult to debug if you try to do everything at once.
The problem that I am specifically dealing with is using the ActivityBoard to read in info from the Java program. Everything else works. I was having a problem with squares and circles, but since I got rid of the buffer variable, the Java program can read stuff in fine. I suspect the problem stems from the serial protocols the propeller is using, but I don't fully understand how the propeller communicates with the serial ports on the computer so I am having trouble narrowing down the problem.
Right now, when I communicate to the ActivityBoard and have it echo back whatever info it is receiving, I get random numbers. I know it is not a problem with the Java program, so it must be with the ActivityBoard. Since I also know that the ActivityBoard can send info without issue, I can assume that the problem occurs when the ActivityBoard is reading information from the serial connection.
If I could figure out how the ActivityBoard communicates with the SimpleIDE terminal, maybe I could figure out what is causing the problem, and hopefully come up with a solution. But if I could do this, I wouldn't need any help . Do you know if 1) this could be the problem and 2) if this could be the solution?
In one place in the code you call dprint with a null terminated string:
But later you call it with a single character:
Unless dprint is overloaded to take both data types that's not going to work. Given its name, my guess is that dprint expects a null terminated string and the dprint of c is going to print random junk until it bumps into a null.
I think in the second example you need a format specifier. I'm not sure if it will work, but try:
Tom
Sorry for the big delay! My school just started, so it is hard finding time to work on everything.
I have been trying to make some progress, but I can't figure out my problem . It appears that whatever I send to the ActivityBoard over USB from a Java program doesn't get "read in" by the program. When I try to make an echo loop with the ActivityBoard and the computer, it doesn't work. I can receive info from the ActivityBoard fine, and that part of my program/circuit works. The issue is when I send the character 'A', for example, and have the ActivityBoard echo what it receives, in this case an 'A', back to the computer, I get random numbers including 252, 128, and 16. Do these specific numbers matter; is there something that specifically causes these numbers to be returned?
I have tried a loop tester with connecting the Rx an Tx pins directly together over an FTDI chip, and it works fine.
Has anyone successfully communicated with the ActivityBoard over USB from a program other than the Parallax supported IDEs in Java ( well....at this point, I guess I am willing to go for any language )?
You can "communicate" with the Activity Board over USB with serial data. You can do that with any programming language. I've done it in Java, Python and I routinely just send commands over TTY to the Activity Board to test my basic code.
What operating system are you using? If you are on Linux try running "miniterm.py /dev/ttyUSB0 115200", and see if you can send/recieve data to the Activity Board. If that works, you probably are just having trouble with your string conversion.
Here is some test code I wrote a while back just to prove that I could talk over serial to an Activity Board from a Raspberry Pi:
It uses jSSC: https://code.google.com/p/java-simple-serial-connector/
It has been a few months since I threw that together, mostly from examples, so I don't remember all of the details.
I can communicate to the computer from the ActivityBoard fine, it is just when I attempt to send data to the ActivityBoard from my computer that my communication breaks down . I have tried to debug the issue, but the issue seems that the ActivityBoard doesn't receive any information when I send information from the computer to it.
Do you see what issue I am having?