Java Send Sms Serial Port
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 Data I 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. Hardware Setup My current hardware setup is as follows: • PC connected to an XBee • Arduino connected to an XBee User 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 Code The 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. Imports The 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 Declaration The code here reads: public class Communicator implements SerialPortEventListener I 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().
Send SMS by using Serial Port (Java API forum at Coderanch)hi., Iam trying to develop a java application that use to pass At Commands to my mobile through using. This can, however, be replaced with Java Community Process's Rx-Tx API off IBM's contribution page. The Communication API or Rx-Tx API is used to perform serial communication to and from a COM or Virtual COM Port. As of the internal functional specification of the API, the API can send SMS, read SMS.
This method will be defined later. Class Variables and Constants Below 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. Physical Metallurgy By Vijendra Singh Ebook Readers.
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,,*54 Thanks 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. 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 the while (ports.hasMoreElements()) I don’t know what’s the problem, and am a beginner with this serial programming so pls i need help as soon as possible Thnx 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 Library ========================================= Native lib Version = RXTX-2.1-7pre16 Java lib Version = RXTX-2.1-7 WARNING: RXTX Version mismatch Jar version = RXTX-2.1-7 native lib Version = RXTX-2.1-7pre16 gnu.io.NoSuchPortException at 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). Hi, Thank you very much for sharing such a thorough guide for serial communication with Java – it has helped me understand the usage of RXTX a lot!! I am pretty new to Java – I have used Serial communication in Python before, but for various reasons, I am having to switch over to Java – and so I just wanted to confirm my understanding of some details about your code: so all of the methods (the public void methodName) code blocks are inside the overall class of Communicator and since you declared the variables (such as serialPort, input, output) inside the class but outside of the functions, you can access them from inside any of the functions? (Also, I think they’re called methods, which I usually call functions, so apologies if I’m using the wrong name.) Also, in the code, it looks like the values you’re wanting to write are intergers (leftThrottle, rightThrottle) but you say that serial communication is in bytes – how are you dealing with this or does serial just take care of this issue? I’m mostly wanting to send info as hexidecimal – to make it easy to send characters and integers. Basically is output.write(0x33) the same thing as output.write(‘3’) the same as output.write(51)?
(where 51 is an interger and ‘3’ is a character). Thanks in advance for any help you can give me! 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.
I want to send am SMS to my mobile phone that I have connected to my PC through a USB port. I am using for this purpose. I am trying to run but I am getting the following error. The output of the program including the error message: Example: Send message from a serial gsm modem. SMSLib: A Java API library for sending and receiving SMS via a GSM modem or other supported gateways.
This software is distributed under the terms of the Apache v2.0 License. Web Site: Version: 3.5.4 [main] INFO smslib - SMSLib: A Java API library for sending and receiving SMS via a GSM modem or other supported gateways. This software is distributed under the terms of the Apache v2.0 License. Web Site: [main] INFO smslib - Version: 3.5.4 [main] INFO smslib - JRE Version: 1.8.0_45 [main] INFO smslib - JRE Impl Version: 25.45-b02 [main] INFO smslib - O/S: Windows 7 / amd64 / 6.1 [main] INFO smslib - Queue directory not defined. Queued messages will not be saved to filesystem. [Thread-3] INFO smslib - GTW: modem.com1: Starting gateway, using Generic AT Handler.
[Thread-3] INFO smslib - GTW: modem.com1: Opening: COM4 @115200 [Thread-3] INFO smslib - GTW: modem.com1: Closing: COM4 @115200 [main] INFO smslib - GTW: modem.com1: Stopping gateway. [main] INFO smslib - GTW: modem.com1: Closing: COM4 @115200 [main] INFO smslib - GTW: modem.com1: Gateway stopped. Org.smslib.GatewayException: Comm libraryexception:java.lang.RuntimeException: javax.comm.NoSuchPortException at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102) at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114) at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189) at org.smslib.Service$1Starter.run(Service.java:277) I have simply connected my phone to my PC using a USB cable.
Also, I think from the error, the COM4 cannot be found. When I searched for for this I found out that my laptop does not have COM Ports. I have tried BIOS settings but there is no place to enable COM ports and I cannot see them in Device Manager. How can I solve this problem? What must I replace with COM4?Thanks.
Go to your device manager and check if you see your phone under Ports. If he is listed there, check which COM is using, COM is writen in the brackets besides the device. You need to use the same COM in your application. Bellow is the image of my device manager so that you can see how should it look. But normally devices that can be seen under Ports use serial communication (RS232). Map Of The World. Your phone probably doesn't use serial communication or you need to install drivers.
This is how you should see your phone.