Java RXTX - Detecting the right device through serial communication - java

Let me go straight to the problem.
Let say, I have a Arduino Uno (COM4) and Arduino Mega (COM5), both is communicating with my OS through serial port. How can I detect that COM4 is really an Arduino Uno so that I don't mistakenly connecting my program to COM5 port? The process of connecting to the serial port is done in runtime by my program.
The code that I saw online here in another SO question and from my experiment with that code earlier, it only listed out all serial ports while it don't reveal out what is that device is (so the program don't know what is really that device, either can be Arduino Uno or Arduino Mega or something else).
So my question is, how can I achieve this purpose? Cross-platform is preferred but if such things can't be achieve, then please answer on how to do it inside Windows (as I currently developing this program for computer that's using Windows OS)

You can ask a device to identify itself right after connecting. For that you want to support device identification in the device's firmware.
Add something like following logic into loop method defenition of the arduino sketch:
int inputData = Serial->read();
if (inputData == IDENTIFICATION_REQUEST) {
// respond with information that identify the device
}
Then you can check which device the programm is connected to sending IDENTIFICATION_REQUEST.

Related

Using Java to send data to arduino sketch

I'm new into arduino programming and robotics, and I want to use Java to send data to the arduino sketch, to notify it when to use the robots motors. In particular, I have already written a working A* algorithm in Java and would really like to apply this to my arduino robot, so in my java code, when the best state is found, I want to then send data to arduino sketch to notify it what direction to go. Anyone have and ideas as to the best way? I've looked online but haven't anything that really answers this specific question.
Also, I realize I'll need to have the arduino connected via USB to use Java, but for now I'm fine with it
You are going to need JAVA USB API in order to send the data to the USB port where your Arduino is connected. From there you just read the input in your Arduino sketch and you are done.
You can also use a Java Serial Port library (like jssc).
Personally I use Processing (which uses Java) and it's quite easy to do Serial communication with the serial library it ships with.
Check out the SimpleWrite example (which includes example Processing-java code and Arduino code)

Combining Java + bluetooth + arduino

Today i came up with an idea to controle a arduino board or any other micro controller wirelessly using a java program. Im somewhat of an intermediate java programmer, meaning i can make basic applications. I know next to nothing about arduino, or the programming of microcontrollers in general, and i dont want to start to learn until i know i can link java with arduino, meaning running a program on the computer that, lets say, turn on a led. Ive done some research, and thats where i got the bluetooth from, but it seemed like the only videos were in spanish, and not much documentation about it, as far as i can see. I was just wondering how easy is it to link these two, is it possible, any links to tutorials of sorts, thanks. P.S. it doesn't have to be arduino, im just familier with that name, im just interested in controlling something physical through a java app, thanks.
Yes you can,
If you can program in java you will find arduino fairly easy but here is a arduino based tutorial with all the code your need:
http://playground.arduino.cc/Learning/Tutorial01#.UxClcvl_tFs
A majority of bluetooth modules will spit out serial data and this simply interacts with the RX/Tx (receive/transmit) inputs of the arduino. They are usually configured to standard board rates mainly 9600 to start.
All you have to do is spit a string out via bluetooth, an java based bluetooth example is below but you can also find bluetooth modules for PC that interact and install as a CDC (serial port).
Sending a string via bluetooth from a PC as client to a mobile as server.
Serial ports are a little easyier :)
http://www.java2s.com/Code/Java/Development-Class/COM-Port.htm
Cheers,
Chris

Linking an Arduino program in Java

I have written a program in Arduino to detect intrusion using an ultrasonic sensor. And when it detects intrusion, a camera must be triggered. I have written the triggering program in Java in the NetBeans IDE. But I don't know how to link the Arduino program with Java. I browsed many sites and came across the blog post Arduino and Java, but I got an error while running the program.
The error is at the line Preference.init() How can I fix this problem?
Your Java application running on some computer or board can communicate with your Arduino application via serial communication link. This can be RS232 for shorter distances (up to 30m if 9600bps is used), or RS485 for up to 1200m. You need touse some known protocol, or make your own. This link can also be wireless if needed. More complex would be to put bluetooth on both sides, if that is more convinient for some reason. Alternatively, if you have an ethernet shield for Arduino you can implement UDP or TCP messaging to notify Java application about Arduino events. Instead of sending an event only when it happens, you can also think about periodical polling from Java application.

Turn on the LED on Arduino with Java

I installed the Java serial RXTX libraries and got my Arduino set up right. I even have the sample code up and running, but I can't seem to understand how to access the different Arduino pins in order to turn a LED for example. How can I access the pins with the OutputStream or any other way?
Do I need to send something special to the stream in order to tell it about a specific pin?
Out of the box the Arduino doesn't provide any way to control the I/O ports from the serial link.
You will need to obtain or write a program which runs on the Arduino and which listens for commands from the serial port to control the I/O pins. Because the Arduino only has a small microcontroller on it, you probably cannot write this program in Java.
Here is an example program which runs on the Arduino and reads numbers spelt out in decimal ASCII characters over the serial line, then uses those to control an RGB LED. You should be able to use this example to understand how to approach the Arduino side of the problem, and your Java RXTX code can send the integers to control this once it's working.
http://arduino.cc/en/Tutorial/ReadASCIIString
I am not sure how readable my code is, or how easily you can install it, but I wrote a small project to talk to the Arduino via RXTX. You can see my code here:
https://github.com/p90puma/SerialToIR
It should give you some ideas.
These three files should give you everything you need:
RXTX Code
Interface
Arduino Sketch
No, the Arduino itself accesses its own pins. You use Java to send serial data to the Arduino, and it then decides which pin(s)/code to use.
So in your Arduino code you start with:
Serial.begin(9600);
...
...
And to make a serial connection from Java, you can use the SerialPortEventListener interface.
All of the information in the prior answers is correct. The Arduino does not by itself, talk to a computer running Java and do anything. You need to put code onto the Arduino that will talk to your computer with some kind of protocol.
It turns out that a standard set of code for this exists. It is called Firmata. See http://firmata.org/wiki/Main_Page for the Firmata home page. See http://playground.arduino.cc/Interfacing/Processing for a discussion of Firmata and Processing.
Processing is a PC/Mac/Linux programming environment based on Java. The Processing and Arduino projects are closely related.
Try the various examples. Put Processing and the Arduino IDE on your computer. Upload Firmata onto your Arduino and the use the Processing / Firmata examples to try everything out.

Detect current with USB and Java

To check if a 'switch' is open or closed and detecting that in Java, I have the following plan: I won't use the data pins, just the USB 5V current, and if the switch is closed there is a current, which I should detect in Java, and so it will be processed by my program.
Would there be a simple solution for this or do I need to find and try out a whole Java usb library for it, of which I would use just a tiny little bit?
Thanks in advance
This will not work in the way you describe it. Have you ever connected a gadget like USB lamp or USB fan? Then you would know that the Software/OS does not even know about them.
The USB spec says you can draw up to 100mA from a port without telling anyone about it, and 500mA when declared in the USB protocol. Most USB HDDs draw quite some more than the allowed 500mA maximum USB2 current.
To make your application work, you absolutely need a device which can talk over USB. This could be an USB=>RS232 adapter (which your application can talk to using RXTX) or a HID device like a USB Joystick. Joysticks can have buttons and switches.
You could try RXTX. This is a library written for serial communication with Java. http://users.frii.com/jarvi/rxtx/. You will have to use native libraries, and I don't know if it would be able to detect if there is a current or not on the USB.

Categories

Resources