Send Hex Data To Serial Port Java
' Released: March 2006. Checking if this processor honours the WP bit even in mode. SELinux: Initializing. PS2 I also posted this in the lounge area but I suspect that was the wrong place (sorry).Yours Mike Robinson # dmesg egrep -i '(audio sound snd linux processor ac97 alsa tv)' Linux version 2.6.7 (root@Knoppix) (gcc-Version 3.3.4 (Debian 1:3.3.4-3)) #2 SMP Wed Jul 28 04:25:36 CEST 2004 Processor #0 15:3 APIC version 20 Kernel command line: auto BOOTIMAGE=Linux ro root=301 ramdisksize=100000 init=/etc/init lang=us apm=power-off nomce pnpbios=off alsa lang=us wheelmouse Detected 2395.371 MHz processor. SELinux: Starting in permissive mode Total of 1 processors activated (4751.36 BogoMIPS).
This is more of a follow-up to my previous post about serial programming in Java and how to install the RXTX libraries. This post also assumes that Java is already properly set up with RXTX.Generally, communication with serial ports involves these steps (in no particular order):. Searching for serial ports.
Connecting to the serial port. Starting the input output streams. Adding an event listener to listen for incoming data. Disconnecting from the serial port.
Sending Data. Receiving DataI wrote an example program that includes all of those steps in it and are each in their own separate method within the class, but first I will go through my hardware set up.
Java Hex Number
Hardware SetupMy current hardware setup is as follows:. PC connected to an XBee. Arduino connected to an XBeeUser input is given from the PC through the a Java GUI that contains code for serial communication, which is the code presented here.The Arduino is responsible for reading this data. This set up is pretty much using my computer as a remote control for whatever device is on the Arduino end. It could be a motor control, on-off switch, etc.
Existing CodeThe purpose of this post is to discuss serial programming in Java, and not GUI’s. However, I did create a GUI for testing purposes. See the Code Downloads section for the actual files.Above is the picture of the GUI complete with the buttons that I use to interact with the program. I also added key bindings which I can use to control the throttle.When the program is first started, none of the GUI elements will work except for the combo box and the connect button. Once a successful connection is made the controls are enabled. This is done through the use of the setConnected(true) and the toggleControls methods shown in the example code that follows. ImportsThe imports i used for this program were as follows.
Import gnu.io.;import java.awt.Color;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.HashMap;import java.util.TooManyListenersException;Depending on the Java IDE it might already know to tell you to use these imports except for the first one. That first import is specific to RXTX, and all its library methods/classes are in there. Class DeclarationThe code here reads: public class Communicator implements SerialPortEventListenerI named my class Communicator, but the name is really up to the programmer. The name pretty much reflects its intended use.The class should also implement the SerialPortEventListener class.
This is a class in RXTX and is required in order to receive incoming data.On some IDE’s this may generate a public void method called serialEvent. This method will be defined later. Class Variables and ConstantsBelow are the variables and constants that I defined in my class. What the variables are for is in the comments but a more detailed explanation will follow. Hi, thanks for your comment. I don’t think I’ve noticed anything of that sort happening, but it could be entirely possible and it sounds like something that happens consistently.
I did have some problems before with the numbers not updating correctly and I thought I had fixed it, but I could be wrong. It could be that something went awry during the serial communication. As far as I know (I am trying to remember since I wrote this quite a while ago), the code explicitly says to update the right most throttle when the button to accelerate the right side is pressed, so it is really strange to see the opposite happen. I would like to take a look and try it for myself, but I will be a little bit busy for the next little while, so I don’t know if I will be able to do that. If you do find a fix for it, please let me know! Thanks for your compliment!If I were to do something like that, I wouldn’t know how to do it as far as coding specifics go (I’ve never done anything like that before), but I’m relatively certain that there are libraries available to handle communication through the internet, but I’m not sure as to how you’d go from a remote computer to the robot.
Maybe one way is to go from a client computer —- server (with the serial comm stuff) — robot, and then that way you could handle the net code separately from the serial code. I’m really not sure as to how feasible this method is or whether there are better alternative methods available.
I just thought of it, but never tried it. Man thanks a lot for this code!! I used to communicate with my precision GPS Trimble Pathfinder Pro XH, I modified the code because I just want to listen the NMEA output of the GPS. So I erased the part of write and the part of throtle and all that. But now I have to do something very important, I don’t know how: I need that the data I see in the Textarea of the program saved it in a MySQL database, but I don’t know how, because I’m not completely sure what is the variable that has the data, I think is logText. I’m new in Java I don’t have a lot of experience so I need your help. Your code is working great but now I need to save that data in a database.
This is the data I received and see in the TextArea (is a NMEA frame): $GPGGA,0741.836692,N,53,W,1,06,1.3,54.11,M,-6.15,M,.54Thanks for your attention. Hello, I am doing a embedded system project where i need to use the data sent by an external device wirelessly to interface my PC keyboard and mouse (my project is a game keypad design ). So,basically,any key digital key i press on the game pad will reach my computer wirelessly ( i use serial communication). My major doubts are as follows: 1. Sent data (in bits) reaches the COM port, how do i use them as input and operate my keyboard accordingly? 2.” Robot class ” in JAVA is the best option. But how do i get serial port data as input and program in it?
I read your post and i found it very interesting. I want to continuously read the incoming COM port data and emulate my keyboard and mouse function using robot class accordingly to my wish please help me with your suggestions.
Send Hex Data To Serial Port Java Key
I am using Zigbee cc2530 Texas instruments, i connected it with a serial cable to my PC and display measured data on GUI using Java, when i run your code the combo box of the ports is disabled so i think it do not see any of the ports, i try to trace the communicator class by adding some print statements i found out that it do not enter the while loop of thewhile (ports.hasMoreElements)I don’t know what’s the problem, and am a beginner with this serial programmingso pls i need help as soon as possibleThnx in advance. Hello, I like what you have done here.
I have a question. I am looking to do the same sort of read and write to and from the ports, but my project is different. At work, I have a tool connected to my laptop. I am only looking to simply listen to the ports for certain “objects’ or ‘strings’ that I define and flag them when they become available. Can you give a suggestion on what I may need to do as far as modifying your codes?
Secondly, I don’t think I should use Xbee and arduino, should I?I am now excited to work on this project because of your website. I will be doing this on windows by the way.Thanks for your help. Hi Karin,It sounds like the same thing as what I’ve done except that you don’t have to send any commands – all you’re doing is waiting for specific strings/objects.
So when the Java side receives data from the hardware, you just check to see what you’ve got.You certainly have lots of options for communicating with hardware. I used an XBee and Arduino because I specifically wanted to learn a little bit more about those when I dove into the electrical design side of things. As far as other alternatives, I don’t know much (electrical design is not my field), but I’m sure Google can give you some nice alternatives.
Very fast response from you. Am thinking, It doesn’t seem like I will be needing another hardware.
Is there a need for a hardware?My set up is set up like this in my project.1.I have a hardware, let’s call it ‘B’ connected to my computer ‘A’ and another hardware ‘C’. With this hardware ‘B’, data is constantly streaming from ‘C’ and logged by my computer and I can query ‘C’ or write to it using the GUI on my computer. So, I am already logging the data just fine as it is. The problem is that the data collected takes a lot to be manually analyzed and pain in the neck.So, what am really looking to accomplish is to write a code that will listen to the same port as in my setup above and listen for specific words or string and simply flag them on a GUI if I choose to. This way, I get to do a 4 hr work in 10 minutes.With his more info, do you suppose I need more hardware still?
Hello,i have tried the code written by shamol roy but ii got this error. Please help me out.Stable LibraryNative lib Version = RXTX-2.1-7pre16Java lib Version = RXTX-2.1-7WARNING: RXTX Version mismatchJar version = RXTX-2.1-7native lib Version = RXTX-2.1-7pre16gnu.io.NoSuchPortExceptionat gnu.io.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:218)at com.java.filehandling.TwoWaySerialComm.connect(TwoWaySerialComm.java:27)at com.java.filehandling.TwoWaySerialComm.main(TwoWaySerialComm.java:131).
Thank you for your response!So if the write to serial takes an int, then you can not send characters (such as a, b, c)? I have been using characters as an initialization byte to ensure that the program is starting with the right information. Is this a property of the output method for RXTX?
And do you know of any documentation of RXTX that I can use to see these sort of requirements (or do you just know this from experience?) The wikibook page for Java serial communication appears to be sending strings with the output method – it is using JavaComms, but I was under the impression that the way you used RXTX and Javacomms was the same.