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

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

Java program using Method Overriding

Archived Comments

1. This code is only for send sms? can it also alert
View Tutorial          By: abdul kadir at 2009-10-23 05:37:46

2. Good explanation
View Tutorial          By: Siddesh at 2012-07-12 03:53:07

3. I have a problem , i cant see the Microsoft.Office
View Tutorial          By: Japz at 2012-01-16 09:51:11

4. What is AWS?
View Tutorial          By: Toja at 2013-01-22 10:36:45

5. After edit tomcat-user file and restart sevice ..i
View Tutorial          By: Rajesh Perumal at 2013-01-08 06:29:21

6. Thanks. Your post was clear and worked perfectly a
View Tutorial          By: Suma at 2009-03-05 09:40:08

7. great material for language
View Tutorial          By: hafiz at 2015-03-24 12:18:45

8. yes 29 you are correct! 27's example is prone to a
View Tutorial          By: sameendra at 2011-07-24 11:55:54

9. Interesting introduction...thank you
View Tutorial          By: arun at 2010-04-05 05:59:34

10. yes it is good example for abstract classes.Thanks
View Tutorial          By: Nilesh Chavan at 2010-07-17 02:24:42