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



Subscribe to Tutorials

Related Tutorials

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

Java program using Method Overriding

Java program to check if user input is an even number

Archived Comments

1. Hi Alvin, in order to send sms from phone I suuges
View Tutorial          By: Vasan at 2008-03-21 01:44:02

2. Thanks Friend it was very helpfull.
View Tutorial          By: UMAR FAROOQ at 2015-03-01 07:46:43

3. very nice..thank you so much.all the concepts are
View Tutorial          By: Priya at 2013-01-01 07:39:04

4. i want to know when we're importing a package , w
View Tutorial          By: bilal ahmad at 2013-02-14 14:11:24

5. It may be good ,when you stressed on the point why
View Tutorial          By: Kishore Mylavarapu at 2011-05-25 01:28:50

6. By running ListPorts, I am not getting any output
View Tutorial          By: Srinivasa Rao at 2009-04-02 05:17:20

7. very nice example
View Tutorial          By: lois at 2010-10-05 23:05:01

8. I got intersting idea about gauge,thanks...
View Tutorial          By: Marimuthu at 2009-07-20 00:56:27

9. How do I insert an image in a list? I am using Vis
View Tutorial          By: anu at 2010-04-16 03:07:00

10. I'm trying to learn navigating between activities.
View Tutorial          By: Ben at 2012-01-17 23:42:38