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. Hello,

I am still getting the below

View Tutorial          By: pavithra at 2012-03-25 19:33:38

2. Can any help me to get Second line,Second word in
View Tutorial          By: Ramesh at 2012-07-21 11:12:05

3. its very helpful and understandable.thanq
View Tutorial          By: Deepika darapaneni at 2012-03-24 14:16:39

4. good explanation ,it is very very understandable
View Tutorial          By: chaitanya at 2011-10-27 16:21:55

5. grazie!!!
View Tutorial          By: Egidio De Lorenzo at 2011-04-20 12:07:25

6. This was very helpful! Thanks!!!
View Tutorial          By: Andreas Clausen at 2009-09-03 15:07:14

7. How to display the ARP Mapping Table of a given ho
View Tutorial          By: Hala at 2013-11-15 22:42:36

8. Thanks dude its really helpful. it clears my doubt
View Tutorial          By: Sriram at 2009-12-28 05:54:37

9. hi.I am new in j2me and really want to learn more
View Tutorial          By: A.vinothKumar at 2012-08-20 07:50:26

10. simple but accurate
View Tutorial          By: Githin Zacharia at 2011-08-10 06:08:32