Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

how to read data from serial port

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 296
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic