• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to read data from serial port

 
arpita sabat
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1831
33
Firefox Browser IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
arpita sabat
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is HARD to read and understand.
use code tags with tabs
 
arpita sabat
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 296
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What library do you use?
I believe RXTX is the most popular one for communicating with serial port in both ways.
 
arpita sabat
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic