Comment on Tutorial - Sample Java program shows how to write to COM port using Java. By Johanes



Comment Added by : Anonymous

Comment Added at : 2013-03-29 02:39:00

Comment on Tutorial : Sample Java program shows how to write to COM port using Java. By Johanes
import gnu.io.*;
import java.io.*;


public class ListPortClass implements SerialPortEventListener
{

public static void main(String[] s)
{
try
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(\"/dev/ttyS0\");
if (portIdentifier.isCurrentlyOwned())
System.out.println(\"Port in use!\");

else {
System.out.println(portIdentifier.getName());

SerialPort serialPort = (SerialPort) portIdentifier.open(\"ListPortClass\",300);
int b = serialPort.getBaudRate();
System.out.println(Integer.toString(b));
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
serialPort.notifyOnCarrierDetect(true);

serialPort.setOutputBufferSize(100);
serialPort.setInputBufferSize(1000);
serialPort.addEventListener(new ListPortClass());
OutputStream mOutputToPort = serialPort.getOutputStream();

InputStream mInputFromPort = serialPort.getInputStream();
PrintWriter pr=new PrintWriter(mOutputToPort);
FileReader fr=new FileReader(\"/root/Desktop/satya/input.txt\");
BufferedReader br =new BufferedReader(fr);
String st=br.readLine();
System.out.print(st);
mOutputToPort.flush();
mOutputToPort.flush();
System.out.println(\"beginning to Write . \\r\\n\");
mOutputToPort.write((\"$B\").getBytes());

System.out.println(\" Written to Port. \\r\\n\");
mOutputToPort.flush();
System.out.println(\"Waiting for Reply \\r\\n\");

//Thread.sleep(50);
System.out.println(mInputFromPort.available());
byte mBytesIn [] = new byte[20];
int n=mInputFromPort.read(mBytesIn);
//mInputFromPort.read(mBytesIn);
String value = new String(mBytesIn);
System.out.println(\"Response from Serial Device: \"+value);
mOutputToPort.close();
mInputFromPort.close();
}
}
catch (Exception ex)
{
System.out.println(\"Exception : \" + ex.getMessage());
}

}


@Override
public void serialEvent(SerialPortEvent spe) {
System.out.println(\"data available\");
}
}


This is my java code for Communicate with ETIM machine....
whenever the machine gets the data of input.txt file, it gives the output as its version .

But while excecuting this code, machine not giving any response back...please solve my problem...

my OS:LINUX


View Tutorial



Related Tutorials

Java program to get location meta data from an image

Program using concept of byte long short and int in java

Update contents of a file within a jar file

Tomcat and httpd configured in port 8080 and 80

Java File

Java String

Count number of vowels, consonants and digits in a String in Java

Reverse a number in Java

Student marks calculation program in Java

Handling Fractions in Java

Calculate gross salary in Java

Calculate average sale of the week in Java

Vector in Java - Sample Program

MultiLevel Inheritance sample in Java

Multiple Inheritance sample in Java

Archived Comments

1. please tell me.........
View Tutorial          By: case c++ at 2012-02-20 02:45:55

2. The above codes is working for me~
yeah, wh

View Tutorial          By: rejectee at 2009-10-23 22:14:25

3. super
View Tutorial          By: chalamareddy at 2012-10-08 18:08:29

4. great explaination !!!
View Tutorial          By: aman at 2012-10-02 06:55:55

5. steps 2 create and use tiles
View Tutorial          By: sasank at 2008-10-03 08:06:43

6. its not solve my problem.i.e
the concept is

View Tutorial          By: murli at 2010-10-02 23:23:11

7. java.lang.NoSuchMethodError: main

<

View Tutorial          By: mark at 2010-09-27 03:46:48

8. Hi.....u have put an extra backslash in the last f
View Tutorial          By: Samantha at 2011-05-12 12:54:39

9. Awesome .
Helpfull!!

View Tutorial          By: ARMLAB-NG at 2009-06-06 09:22:00

10. hi MR. A.Balaji
can u help me how you have

View Tutorial          By: mjnanthini at 2010-10-03 23:04:35