Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2004

Tip: Looking for answers? Try searching our database.

Question: Java Serial Port Communication (javax.comm.*)

Thread view: 
bgao11 - 25 Feb 2004 04:54 GMT
Hello everyone,

I have trouble in using javax.comm.* for my project described as
follows:

Computer will generate pulses on Pin 4 of DB9 serial port. The input
pulse, a square wave or a spike is needed to trigger device. The
square wave starts at 64pps(pulse per second) at 50% cycle. The
voltage of the pulse can be between 5 vdc and 18 vdc. Notes: Pin 4 :
pulse input; Pin 5: ground.

My program is below. When I run the program, nothing happens---no
exception, no device action. Please give me some advice and help.
Thank you so much.

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

public class PumpSerialPort{
   public static final int TIMEOUT = 30;
   public static final int BAUD = 9600;

   int numOfPulse = 0;
   float pulseRate = 1.28f;

   CommPortIdentifier portID;
   CommPort port;
   PrintStream outStream;

   public PumpSerialPort(int duration, float pulseRate, String
portName)
       throws IOException, NoSuchPortException, PortInUseException,
       UnsupportedCommOperationException, InterruptedException
   {
       // calculate the number of pulses through the input duration
and pulseRate
       if(pulseRate>0) this.pulseRate = pulseRate;
       numOfPulse = (int)(duration*this.pulseRate);

       openSerialPort(portName);
   }

   public void openSerialPort(String portName)
       throws IOException, NoSuchPortException, PortInUseException,
       UnsupportedCommOperationException, InterruptedException
   {
       portID = CommPortIdentifier.getPortIdentifier(portName);
       port = portID.open("Pump Serial Port", TIMEOUT*1000);

       SerialPort pumpPort = (SerialPort)port;
       pumpPort.setSerialPortParams(BAUD, SerialPort.DATABITS_8,
           SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

       pumpPort.notifyOnDataAvailable(true);
       pumpPort.notifyOnCTS(true);
       pumpPort.notifyOnDSR(true);

        pumpPort.setDTR(false);
        pumpPort.setRTS(false);

       // generate and send input pulses, a square wave
       for(int i=1; i<=numOfPulse*2; i++){

           if(i%2 == 1) pumpPort.setDTR(true);
           else pumpPort.setDTR(false);

            int waitingTime = (int)(1000/(2*pulseRate));
           Thread.sleep(waitingTime);

            pumpPort.setDTR(false);
       }
   }

   public static void main(String[] argv)
       throws IOException, NoSuchPortException, PortInUseException,
       UnsupportedCommOperationException, InterruptedException
   {
        // device 1: no action
        PumpSerialPort psp = new PumpSerialPort(10, 99f, "COM5");

        // device 2, an optical mouse: the light is flashing---GOOD
        PumpSerialPort mouse = new PumpSerialPort(10, 1.28f, "COM6");
    }
}
//----------------------------------------------------------------------------
William Brogden - 25 Feb 2004 15:08 GMT
> Hello everyone,
>
[quoted text clipped - 10 lines]
> exception, no device action. Please give me some advice and help.
> Thank you so much.

ADVICE - catch the Exceptions and output them so you can see
what is going wrong.

> //------------------------------------------------------------------------
> import java.util.*;
[quoted text clipped - 67 lines]
> }
> }

//--------------------------------------------------------------------------
--
WP - 28 Feb 2004 05:29 GMT
Look at the RXTX library.  I tried a Windows XP project where flow
control would not work under XP but fine under 98.  Switch to RXTX
library and everything worked as expected.  CommAPI seemed unpolished to
me.....but I don't claim to be an expert.

> Hello everyone,
>
[quoted text clipped - 83 lines]
> }
> //----------------------------------------------------------------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.