wood burning stoves 2.0*
The moose likes Java in General and the fly likes how to read data from serial port Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to read data from serial port " Watch "how to read data from serial port " New topic
Author

how to read data from serial port

arpita sabat
Greenhorn

Joined: Dec 12, 2013
Posts: 15
Hi Everyone,

As i am new to java(Linux),I had done write byte data to serial port but i din't get read data from SerialPort..i did the connection well,i did the send the byte data like (Hex code-0x02,0x00,0x01,0xFE) but read data give me wrong output like 123a245d...i expect out like decimal..please help to solve my issue..

Thank u.
Paweł Baczyński
Bartender

Joined: Apr 18, 2013
Posts: 1020
    
  16

So you don't have a problem with communication via serial port. You only have an issue with displaying your data.
Maybe show us some code?


Formely Pawel Pawlowicz
arpita sabat
Greenhorn

Joined: Dec 12, 2013
Posts: 15
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SerialPort {
InputStream in;
OutputStream out;

public SerialPort() {
super();
}

public void open() {
Enumeration port_list = CommPortIdentifier.getPortIdentifiers();

while (port_list.hasMoreElements()) {
CommPortIdentifier port_id = (CommPortIdentifier) port_list.nextElement();
if (port_id.getName().equals("/dev/ttyS0")) {

try {
SerialPort port = (SerialPort) port_id.open("PortListOpen", 20);
System.out.println("Opened successfully");
try {
int baudRate = 9600; //
port.setSerialPortParams(
baudRate,
SerialPort.DATABITS_7,
SerialPort.STOPBITS_1,
SerialPort.PARITY_EVEN);
port.setDTR(true);

System.out.println("properties are set");
} catch (UnsupportedCommOperationException e) {
System.out.println(e);
}
}
}

public SerialReader ( InputStream in )
{
this.in = in;
}

public void run ()
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = this.in.read(buffer)) > -1 )
{
//System.out.println("Received a signal.");
System.out.print(new String(buffer,0,len));
}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}

public SerialWriter ( OutputStream out )
{
this.out = out;
}

public void run ()
{
try
{
// String toSend = "229";
// this.out.write(229);
byte[] array = {0x02, 0x00, 0x01, 0xFE};
while ( true )
{
this.out.write(new byte[]{0x02, 0x00, 0x01, 0xFE};
this.out.flush();

}
}
catch ( IOException e )
{
e.printStackTrace();
}
}
}
}

This is my Code and i this got from google..kindly send me proper code to communicate with my serial port..
Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 133

The code is HARD to read and understand.
use code tags with tabs


Are you better than me? Then please show me my mistakes..
arpita sabat
Greenhorn

Joined: Dec 12, 2013
Posts: 15

This is my Code and i this got from google..kindly send me proper code to communicate with my serial port..
surlac surlacovich
Ranch Hand

Joined: Mar 12, 2013
Posts: 296

What library do you use?
I believe RXTX is the most popular one for communicating with serial port in both ways.
arpita sabat
Greenhorn

Joined: Dec 12, 2013
Posts: 15
I am using rxtx-2.1.7.jar.
Actually i m using simplepoll command to send data to serial port i send the hex values but i didn't get response from Serial Port( Simplepoll command-Coin Acceptor).
Kimd;y send me the code for Simple poll command.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to read data from serial port