Establish connection between two pc's through serial port using java commAPI.So kindly help me.It's very urgent.
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   PC_Sriraman
Posted On:   Monday, December 20, 2004 03:22 AM

Iam trying to establish a connection between two systems through modem (serialport) using java commAPI.I also execute the sample code and i heard that the modem is responded.That is,it is dialled but sending up the data or a string from one system to another through serial port is a main problem.It will sent the data but at the other side it will print the junk values like smiley,^B and so on.So,plz kindly help me to finish this task.I will pasted the codings which i will tried.Plz give me a proper response and reply.It's very urgent. Dialup.java import java.io.*; import java.util.*; import javax.comm.*; public class Dialup implements Runnable,SerialPortEventListener { static CommPortIdentifier    More>>

Iam trying to establish a connection between two systems through modem (serialport) using java commAPI.I also execute the sample code and i heard that the modem is responded.That is,it is dialled but sending up the data or a string from one system to another through serial port is a main problem.It will sent the data but at the other side it will print the junk values like smiley,^B and so on.So,plz kindly help me to finish this task.I will pasted the codings which i will tried.Plz give me a proper response and reply.It's very urgent.
Dialup.java

			
import java.io.*;
import java.util.*;
import javax.comm.*;

public class Dialup implements Runnable,SerialPortEventListener {

static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort;
Thread dialupThread;
String telNo;
String dialCommand;
String dialLogin;
String dialPwd;
String dailRestCmd;
String dialEcho;
String dialDuplex;


public static void main(String[] args) {

if( args.length != 1 ) {
System.err.println("Usage: Dialup ");
return;
}

System.out.println("Before port list");
portList = CommPortIdentifier.getPortIdentifiers();

System.out.println(portList.hasMoreElements());

while (portList.hasMoreElements()) {
System.out.println("iniside portlist");
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
Dialup dialer = new Dialup(args[0]);
}
}
}

}

public Dialup(String telNo) {
System.out.println("Inside the Cons");
this.telNo = telNo;
// this.dailRestCmd="ATZ
";//added
// this.dialEcho="ATE1
";
// this.dialDuplex="ATF1
";

//this.dialCommand =userName,passWord,phNo,"Standard modem",dialup_name;
this.dialCommand = "ATDT"+telNo+"
";
// this.dialLogin="sbm
"
// this.dialPwd="sbm123
";

try {
serialPort = (SerialPort) portId.open("DialUp", 2000);
}
catch (PortInUseException e) {
}

try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
}
catch (IOException e) {
}

try {
serialPort.addEventListener(this);
}
catch (TooManyListenersException e) {
}

try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
}

dialupThread = new Thread(this);
dialupThread.start();
}

public void run() {
while(true) {
try {
if( !serialPort.isCD() ) {

String res;
System.err.println("Try to connect ...");
// outputStream.write(dailRestCmd.getBytes());
outputStream.write(dialCommand.getBytes());
outputStream.flush();
int avail=inputStream.available();
byte[] recdata=new byte[avail];
inputStream.read(recdata,0,avail);

res= new String(recdata);

System.out.println("Response for Dailing Phone No"+res);

// outputStream.write(dialEcho.getBytes());
// outputStream.write(dialDuplex.getBytes());
outputStream.write(dialLogin.getBytes());
outputStream.flush();

outputStream.write(dialPwd.getBytes());
outputStream.flush();

avail=inputStream.available();
recdata=new byte[avail];
inputStream.read(recdata,0,avail);

res=new String(recdata);

System.out.println("Response for Authentication"+res);

}
Thread.sleep(60000);
}
catch(Exception ex) {
System.err.println("Failed to write message");
}
}
}

public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.DSR:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.err.println("Ignored event");
break;

case SerialPortEvent.BI:
System.err.println("Break Interrupt");
break;

case SerialPortEvent.CTS:
System.err.println("Clear to send");
break;


case SerialPortEvent.RI:
if( event.getNewValue() ) {
System.err.println("Ring Indicator On");
}
else {
System.err.println("Ring Indicator Off");
}
break;

case SerialPortEvent.CD:
if( event.getNewValue() ) {
System.err.println("Connected");
}
else {
System.err.println("Disconnected");
}
break;

case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
String result = new String(readBuffer,0,numBytes);
System.err.print(result);
}
}
catch (IOException e) {
System.err.println("Failed to read");
break;
}
}
}
}


			
SimpleRead.java
import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("inside portList");

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}

public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}

public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
   <<Less
About | Sitemap | Contact