Unable to parse actuall data from com port

I have a 3G Modem which is installed on my computer on COM9 port. I want to read incoming sms from that port. i am using below code.

import java.io.InputStream; import java.util.Enumeration; import javax.comm.CommPortIdentifier; import javax.comm.SerialPort; import javax.comm.SerialPortEvent; import javax.comm.SerialPortEventListener; /** * * @author IamUsman */ public class ReadingPorts implements SerialPortEventListener, Runnable { static CommPortIdentifier portId; static Enumeration portList; static SerialPort port; static InputStream inputStream; static Thread readThread; static byte buffer[]; /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM9")) { if (!portId.isCurrentlyOwned()) { ReadingPorts rp = new ReadingPorts(); } else { System.out.println("This port is already used by some other program"); } } } } } public ReadingPorts() { try { port = (SerialPort) portId.open("Custom", 500); inputStream = port.getInputStream(); System.out.println("** Connected To Streams **"); port.addEventListener(this); port.notifyOnDataAvailable(true); port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); port.enableReceiveTimeout(500); System.out.println("................................"); readThread = new Thread(this); readThread.start(); } catch (Exception ex) { ex.printStackTrace(); } } public void serialEvent(SerialPortEvent event) { System.out.println("In Callback method"); switch(event.getEventType()){ case SerialPortEvent.DATA_AVAILABLE: buffer = new byte[8]; try{ while (inputStream.available()>0) { int numBytes = inputStream.read(buffer); } System.out.println(new String(buffer)); }catch(Exception ex){ ex.printStackTrace(); } break; } } @Override public void run() { try { Thread.sleep(500); } catch (Exception ex) { ex.printStackTrace();; } } }

I am reading the data but it is meaningful. Things that i read are below

+CMTI: "SM",14

--------------Solutions-------------

the text that I read is meaningless

That's because you read it with the following meaningless code:

while (inputStream.available()>0) {
int numBytes = inputStream.read(buffer);
}
System.out.println(new String(buffer));

It should be:

while (inputStream.available()>0) {
int numBytes = inputStream.read(buffer);
System.out.println(new String(buffer, 0, numBytes));
}

Category:java Time:2019-01-08 Views:4

Related post

  • Reading data from COM port 2012-01-21

    I have a little problem. I try to read data from COM port and I receive "bad" data like following: 0 Fl- 1 i 2 3 ? 4 m? 5 6 7 8 9 10 ? 11 12 13 d 14 D 15 The code that open port is: _port = new SerialPort(Settings.Default.COM, 9600, Parity.None, 8, S

  • Receive NMEA0183 data from COM PORT C++ 2012-04-04

    Hy everybody! I wrote a small gps application in simple C++, what switch the protocoll, send command to the gps chip...etc. The write to the GPS port is working good. But when I try to read the port (for check the accepted commands) I receive a lot o

  • How to read data from COM port using batch file? 2010-10-13

    Is there a DOS/ Batch command which can be used for reading from the COM port? I tried to look for that but didn't find solution. --------------Solutions------------- For reading some information from COM port you can try next command: type COM{n} Fo

  • Parsing/formatting data from serial port - C# 2012-02-29

    I've developed a small program that listens to a serial port. My program is receiving data. The problem is, its not displaying it in the desired format (one string). The data my program is receiving comes in two strings, for example: ID:34242 State:N

  • How to preview data from COM port with telnet or other windows tool 2009-06-16

    I would like to see what data come from COM serial port. Is it possible to do it with telnet or other windows tool? And if so, how to do it? --------------Solutions------------- HyperTerminal can be used to communicate over/read from a serial port. B

  • Application Not Receiving Serial Data from COM Port - C++ 2011-01-12

    My application is not properly receiving data from the COM port. This used to work. I don't know what happened. I know that the proper data is being sent/received over the line because I can see it on my protocol analyzer. The PC gets into the WAIT_O

  • Bluetooth RFCOMM connection: read serial data from com port in Windows 7 2011-11-28

    I am working with the Android Bluetooth Chat sample application. I am trying to read serial data from the Android device in Windows 7. I have been able to pair the Android device to the Bluetooth adapter on my laptop. I replaced the UUID in the sampl

  • Text box to only accept data from COM port 2014-09-11

    I need to enter data into a text box from a com port (Scale) - no problem How do I Disable the data from the keyboard as I can not trust the keyboard data. The only data is the numbers 0 to 9 but that is from the keyboard and scale. Please help. than

  • Read/write data from COM port in Android 2012-03-20

    I need connecting Android tablet with some COM port device and send commands to them. How I understend, I need USB-COM port adapter. What type of adapter I need? Some requirements? When I connect device via USB-COM port adapter, how I must to work wi

  • Read data from serial port 2009-07-29

    How do i read data from serial port using C ? and then again transfer the data to modem ? I am using RS 232 cable for serial communication ... --------------Solutions------------- A lot of useful information about dealing with serial ports both on PC

  • How to Read Device Data From serial port 2010-05-04

    I have one device which sends data on COM port say on COM13. Now i want to read that data and display it in the RichTextBox or in any text control. I have written the application with the help of IO and IO.Ports but comport.DataRecived event does not

  • Capturing the data from different ports on a single port in c# 2011-11-04

    I am making application in c# in which i am getting continuously data from different ports on a single port in packet format.Whatever data i am getting is continuous.I am getting data on port 24097 in packet format from different source port.For that

  • Read image data from COM7 port in Java 2012-04-12

    I am doing a project in which I have to display the image captured from a camera chip on to the PC. The image data is being sent through the COM7 port to the computer. (I have checked it using a dumb terminal program). Basically what I want to do is

  • Serial Port ReadLine vs ReadExisting or how to read the data from serial port properly 2009-03-11

    I am reading data from serial port. The data comes off the scale. I am now using Readline() and getting data dropped even after I removed DiscardInBuffer(). What is the proper way to read the data from the serial port? There are so few examples onlin

  • How to send data to COM PORT using JAVA? 2009-05-23

    Possible Duplicate: Java Serial Communication on Windows Friends, I want to connect and transfer data to COM PORT (either virtual or original) in JAVA? --------------Solutions------------- This question has been asked and answered many times: read fi

  • How to parse a date from a URL format? 2010-10-19

    My database contains URLs stored as text fields and each URL contains a representation of the date of a report, which is missing from the report itself. So I need to parse the date from the URL field to a String representation such as: 2010-10-12 200

  • what is the best way to get live data from serial port and save it in SQL database? 2011-08-17

    what is the best way to get live data from serial port and save it in SQL database? and display it on ASP.NET website ? Is it something that can be done by webservices or client side programm is required to install on the machine ? any suggestions ?

  • IOS: Parsing a date from a string 2011-11-04

    I want to parse a date from a given string. The format of the string is always like this: "YYMMDD" e.g. 110831 which would be the year 2011, month august and day 31. So for testing purpose I wrote this small piece of code: NSString* strDate = [NSStri

  • php parse json data from google directions 2012-02-11

    I'm trying to parse json data from google directions but can't seem to access access any of the values. Here is the php code; $url = "http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles&sensor=false";

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.519 (s). 13 q(s)