Com port Communication

Discussion in 'Java' started by gy_722, Jul 12, 2005.

  1. gy_722

    gy_722 Guest

    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.
    gy_722, Jul 12, 2005
    #1
    1. Advertising

  2. gy_722 wrote:
    > 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.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Jul 12, 2005
    #2
    1. Advertising

  3. gy_722

    gy_722 Guest

    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.
    gy_722, Jul 12, 2005
    #3
  4. gy_722

    Roedy Green Guest

    On 11 Jul 2005 21:28:26 -0700, "gy_722" <> wrote or
    quoted :

    >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
    Roedy Green, Jul 12, 2005
    #4
  5. gy_722

    Roedy Green Guest

    On 11 Jul 2005 22:32:07 -0700, "gy_722" <> wrote or
    quoted :

    >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
    Roedy Green, Jul 12, 2005
    #5
  6. gy_722

    Roedy Green Guest

    On Mon, 11 Jul 2005 21:32:46 -0700, Knute Johnson
    <> wrote or quoted :

    >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
    Roedy Green, Jul 12, 2005
    #6
  7. gy_722

    Roedy Green Guest

    On Mon, 11 Jul 2005 21:32:46 -0700, Knute Johnson
    <> wrote or quoted :

    >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
    Roedy Green, Jul 12, 2005
    #7
  8. gy_722

    gy_722 Guest

    Hi,

    Where can i find the breakout box ?
    gy_722, Jul 12, 2005
    #8
  9. Thomas Weidenfeller, Jul 12, 2005
    #9
  10. gy_722 wrote:

    > 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(); }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Jul 12, 2005
    #10
  11. gy_722

    IchBin Guest

    Roedy Green wrote:
    > On Mon, 11 Jul 2005 21:32:46 -0700, Knute Johnson
    > <> wrote or quoted :
    >
    >> 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.
    >


    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-)
    IchBin, Jul 12, 2005
    #11
  12. gy_722

    gy_722 Guest

    Hi,

    I can confirm both the program work. But they can't communication
    together.
    gy_722, Jul 13, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bernhard Voigt

    Serial Port communication

    Bernhard Voigt, Oct 23, 2003, in forum: Java
    Replies:
    1
    Views:
    1,087
    Dale King
    Oct 27, 2003
  2. fdunne2
    Replies:
    2
    Views:
    14,002
    Thomas Lutz
    Jun 30, 2004
  3. Andreas Horneff

    Borland C++ Serial Port communication

    Andreas Horneff, Aug 15, 2004, in forum: C++
    Replies:
    1
    Views:
    9,931
    Bill Thompson
    Aug 15, 2004
  4. shashi
    Replies:
    0
    Views:
    323
    shashi
    Nov 18, 2004
  5. shashi
    Replies:
    1
    Views:
    354
    Miki Tebeka
    Nov 18, 2004
Loading...

Share This Page