Com port Communication


G

gy_722

Hi all,

How can i open my com1 communicate with com2 in same computer ?
What type of cable should i use to connect com1 and com2 ?
I am using java + comm.jar to control com port.

Please help.
 
Ad

Advertisements

K

Knute Johnson

gy_722 said:
Hi all,

How can i open my com1 communicate with com2 in same computer ?
What type of cable should i use to connect com1 and com2 ?
I am using java + comm.jar to control com port.

Please help.

You need to get the JavaComm API and a null modem cable. Then you need
to learn how to program it.
 
G

gy_722

Hi,

Thank you for your reply. I have get the null modem cable already. And
i have two java simple read and simple write program in hand, but it
don't work.

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;

/**
* Method declaration
*
*
* @param args
*
* @see
*/
public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "com1";

if (args.length > 0) {
defaultPort = args[0];
}

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;
SimpleRead reader = new SimpleRead();
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}

}

/**
* Constructor declaration
*
*
* @see
*/
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();
}

/**
* Method declaration
*
*
* @see
*/
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}

/**
* Method declaration
*
*
* @param event
*
* @see
*/
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;
}
}

}

SimpleWrite.java


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

/**
* Class declaration
*
*
* @author
* @version 1.10, 08/04/00
*/
public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!";
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;

public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "com2";

if (args.length > 0) {
defaultPort = args[0];
}

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;

try {
serialPort =
(SerialPort) portId.open("SimpleWrite", 2000);
} catch (PortInUseException e) {
System.out.println("Port in use.");

continue;
}

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

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


try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}


System.out.println(
"Writing \""+messageString+"\" to "
+serialPort.getName());

try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}

try {
Thread.sleep(2000); // Be sure data is xferred before closing
} catch (Exception e) {}
serialPort.close();
System.exit(1);
}
}
}

if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}


}


I have try to complie this two program, it nothing error. But when i
run them, nothing show in the output of SimpleRead.java when my
SimpleWrite.java send the string out.

Please help.

Thank you.
 
R

Roedy Green

How can i open my com1 communicate with com2 in same computer ?
What type of cable should i use to connect com1 and com2 ?
I am using java + comm.jar to control com port.

you want a null modem cable, sometimes called a cross over cable or a
patch cable.. If you can't find one, I give the instructions for
building one at http://mindprod.com/jgloss/cables.html

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
R

Roedy Green

Thank you for your reply. I have get the null modem cable already. And
i have two java simple read and simple write program in hand, but it
don't work.

you might dig an old DOS terminal program like Procomm to act as you
temporary other end.

Each end is likely waiting for the other end to raise its dtr. You
have 25 pins in an RS232C interface to play with. It gets more
complicated with a crossover since each end sees the same wire by a
different pin.


see http://mindprod.com/jgloss/rs232c.html

If your problem is to it working rather than to learn, I am available
for a fee. I have been doing this sort of thing since the 60s.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
R

Roedy Green

You need to get the JavaComm API and a null modem cable. Then you need
to learn how to program it.

The other tool very useful for this is a breakout box. It monitors all
the wires and lets you fake signals, and reroute wires to simulate any
custom cable. Mine died in a sewage flood.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Ad

Advertisements

R

Roedy Green

You need to get the JavaComm API and a null modem cable. Then you need
to learn how to program it.

If you have the bucks, another tool is a protocol analyser than snoops
on the conversations. Normally you only need this to figure out some
undocumented protocol so you can simulate one of the ends with your
own box.

You can also rent these to get you over a hump. They used to be very
expensive.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
T

Thomas Weidenfeller

gy_722 said:
Where can i find the breakout box ?

In an electronics shop near you. Or you could build one if you know the
right end of a soldering iron from the wrong end.

/Thomas
 
K

Knute Johnson

gy_722 said:
Hi,

Thank you for your reply. I have get the null modem cable already. And
i have two java simple read and simple write program in hand, but it
don't work.

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;

/**
* Method declaration
*
*
* @param args
*
* @see
*/
public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "com1";

if (args.length > 0) {
defaultPort = args[0];
}

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;
SimpleRead reader = new SimpleRead();
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}

}

/**
* Constructor declaration
*
*
* @see
*/
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();
}

/**
* Method declaration
*
*
* @see
*/
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}

/**
* Method declaration
*
*
* @param event
*
* @see
*/
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;
}
}

}

SimpleWrite.java


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

/**
* Class declaration
*
*
* @author
* @version 1.10, 08/04/00
*/
public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!";
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;

public static void main(String[] args) {
boolean portFound = false;
String defaultPort = "com2";

if (args.length > 0) {
defaultPort = args[0];
}

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;

try {
serialPort =
(SerialPort) portId.open("SimpleWrite", 2000);
} catch (PortInUseException e) {
System.out.println("Port in use.");

continue;
}

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

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


try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}


System.out.println(
"Writing \""+messageString+"\" to "
+serialPort.getName());

try {
outputStream.write(messageString.getBytes());
} catch (IOException e) {}

try {
Thread.sleep(2000); // Be sure data is xferred before closing
} catch (Exception e) {}
serialPort.close();
System.exit(1);
}
}
}

if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}


}


I have try to complie this two program, it nothing error. But when i
run them, nothing show in the output of SimpleRead.java when my
SimpleWrite.java send the string out.

Please help.

Thank you.

One of the biggest problems with the Comm API is getting it installed
correctly. Use the program below to check. If your ports are listed
then you've installed it correctly.

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

public class Ports {
public Ports() {
CommPortIdentifier cpi = null;

Enumeration e = CommPortIdentifier.getPortIdentifiers();

while (e.hasMoreElements()) {
try {
cpi = (CommPortIdentifier) e.nextElement();
} catch (NoSuchElementException n) {}
System.out.println(cpi.getName());
}
}
public static void main(String[] args) { new Ports(); }
}
 
I

IchBin

Roedy said:
If you have the bucks, another tool is a protocol analyser than snoops
on the conversations. Normally you only need this to figure out some
undocumented protocol so you can simulate one of the ends with your
own box.

You can also rent these to get you over a hump. They used to be very
expensive.

There is a free product called Ethereal. It has been around for some
time. This is a GUI network protocol analyzer. It lets you interactively
browse packet data from a live network or from a previously saved
capture file. Also 683 protocols can currently be dissected.

See: http://www.ethereal.com for new versions, documentation or for it's
features http://www.ethereal.com/introduction.html#features

Under widows It use to require WinPcap http://www.winpcap.org. No big
deal. I think it use to use its Packet Capture Library for windows.
--


Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.servebeer.com
__________________________________________________________________________

' If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
Ad

Advertisements


Top