Archive for the ‘serialport communication’ Category
installing comport api in windows
comm.jar should be placed in:
%JAVA_HOME%/lib
%JAVA_HOME%/jre/lib/ext
win32com.dll should be placed in:
%JAVA_HOME%/bin
%JAVA_HOME%/jre/bin
%windir%System32
javax.comm.properties should be placed in:
%JAVA_HOME%/lib
%JAVA_HOME%/jre/lib
practice programs:
http://javamix.wordpress.com/2009/03/25/serialport-communication-using-java/
http://javamix.wordpress.com/2009/01/21/sending-sms-using-serialport-communication-api/
serialport communication using Java
before going to execute this program read how to install comport api for windows
In java there is no inbuilt support to access serial or parallel ports, to over come this we are taking support of Java communication API.
after downloading the java communication api extract the folder then you can get the following…
in Commapi folder we require
comm.jar
javax.comm.properties
win32com.dll
after getting these files perform following operations….
- Copy file comm.jar in <JAVA_HOME>\jre\lib\ext
- Copy file javax.comm.properties in <JAVA_HOME>\jre\lib
- Copy file win32com.dll in <JAVA_HOME>\lib
now you can able to access the commport by Java programm.
The following example displays the available ports(serial and parallel ports) and their type:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package keyboardreader;
/**
*
* @author vijay
*/
import javax.comm.*;
import java.util.Enumeration;
public class ListPorts {
public static void main(String args[]) {
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements()) {
CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
String type;
switch (port.getPortType()) {
case CommPortIdentifier.PORT_PARALLEL:
type = “Parallel”;
break;
case CommPortIdentifier.PORT_SERIAL:
type = “Serial”;
break;
default: /// Shouldn’t happen
type = “Unknown”;
break;
}
System.out.println(port.getName() + “: ” + type);
}
}
}
output is like this :
COM1: Serial
COM2: Serial
LPT1: Parallel
LPT2: Parallel
sending sms using serialport communication API
before going to execute this program read how to install comport api for windows
/* this is the program used to send the message from javaprogram to the mobile phone */
/*
Note :- before using this porogram first read the post “serialport communication using Java” by me in this blog.
*/
package sms; // it is my own package
import java.io.*;
import java.util.*;
import javax.comm.*; //for accessing serialport
/**
*
* @author katta vijay
*
*
*/
public class OwnPort {
static CommPortIdentifier portId;
static CommPortIdentifier saveportId;
static Enumeration portList;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static boolean outputBufferEmptyFlag = false;
public static void main(String[] args) {
boolean portFound = false;
String defaultPort= “COM1″;
System.out.println(“Set default port to “+defaultPort);
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println(“Found port: “+defaultPort);
portFound = true;
// init reader thread
OwnPort op=new OwnPort();
op.initwritetoport();
}
}
}
if (!portFound) {
System.out.println(“port ” + defaultPort + ” not found.”);
}
}
public void initwritetoport()
{
System.out.println(“inside initwriteport”);
String s1=”at”;
String s2=”\r\n”;
String s3=”at+cmgf=1″;
String s4=”at+cmgs=\”9966606454\”";
// char[] s5={‘”‘,’9′,’3′,’4′,’6′,’4′,’6′,’2′,’1′,’6′,’5′,’”‘};
String s6=”32“;
// for(int i=0;i<s5.length;i++)
// {
// System.out.println(s5.toString());
// }
// System.out.println(“s6=”+s6);
String messageString = “hey ee mesage GSM nunchi vachindi!”;
// get the outputstream
try {
serialPort = (SerialPort) portId.open(“SimpleReadApp”, 2000);
} catch (PortInUseException e) {}
try {
// set port parameters
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
outputStream =serialPort.getOutputStream();
inputStream = serialPort.getInputStream();
}
catch (Exception e) {
e.printStackTrace();
}
try
{
System.out.println(“–1–”);
outputStream.write(s1.getBytes()); // AT command
// System.out.println(s1.getBytes());
// outputStream.wait(10000);
outputStream.write(s2.getBytes()); // enter
Thread.sleep(1000); //sleeping thread
System.out.println(“–2–”);
// outputStream.wait(1000);
outputStream.write(s3.getBytes()); //at+cmgf=1 command
// outputStream.wait(1000);
outputStream.write(s2.getBytes()); //enter
Thread.sleep(1000);// thread sleeping
System.out.println(“–3–”);
// outputStream.wait(1000);
outputStream.write(s4.getBytes()); //at+cmgs=”<mobilenumber>”
// outputStream.wait(1000);
outputStream.write(s2.getBytes()); //enter
Thread.sleep(1000); //thread sleeping
System.out.println(“–4–”);
// outputStream.wait(1000);
outputStream.write(messageString.getBytes()); // message
// outputStream.wait(1000);
outputStream.write(s2.getBytes());
Thread.sleep(1000);
outputStream.write(s6.getBytes());
Thread.sleep(1000);
System.out.println(“–5–”);
// outputStream.wait(1000);
byte[] readBuffer = new byte[23];
try
{
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
// System.out.println(numBytes);
// print data
String result = new String(readBuffer);
System.out.println(“Read: “+result);
}
}
catch(Exception e)
{
e.printStackTrace();
}
outputStream.close();
serialPort.close();
}
catch(Exception e)
{
}
}
}

