Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
Like Tree2Likes

Thread: javax.comm Run Problem

  1. #21
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    I forgot to mention the environment variables I use. For example: %DEV_DRIVE%
    These are set to various paths and drives as per the machine I run the code on. I have had up to 3 machines I was working on, each a bit different, so I used environment variables to tailor the batch files to the PC they were on.

  3. #23
    mr_anderson is offline Member
    Join Date
    May 2010
    Location
    Florida
    Posts
    68
    Rep Power
    0

    Default

    Dear Norm
    The Jar is working finally. Believe it or not (although It is too stupid and doesn't make any sense) I had to edit my classpath to include the win32Com.dll I know it doesn't make any sense because if the extension of the file isn't .JAR or .ZIP or .class it is ignored by I don't know how and why it accepted the .dll file.
    Just wanted to inform you.
    Thx for all the help.
    Regards.

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default

    Glad you got it to work.
    Not sure how the classpath is involved. I thought that finding a .dll would be an OS problem which would be looking in the current directory or on the PATH variable.

  5. #25
    Dec
    Dec is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Default Re: javax.comm Run Problem

    Hi,

    I am also writing an program to take reading from the serial port, for a weighting bridge application. I am writing a java bean that is used by oracle forms.
    I am reading from the serial port using SimpleReader class which I renamed to TestComm.

    I have written a class that continuously reads from the serial port, and parses the lines for the values I need which is ok.
    but I want to create a class that I can initialise and send in variables for the baud rate etc, as there is different types of weighting bridges

    when I initialise the class I want to be able to ask it for one reading of the scales at a time.

    I can check if its valid and read from it again if need be using the code. I can't figure out how to just make one reading.

    below is my code :

    Java Code:
    package testcomm;
    
    import java.io.*;
    import java.util.*;
    import javax.comm.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    
    
    
    public class TestComm implements Runnable, SerialPortEventListener {
        static CommPortIdentifier portId;
        static Enumeration portList;
    
        InputStream inputStream;
        SerialPort serialPort;
        Thread readThread;
        protected BufferedReader is;
        protected String weightValue = "";
        protected String alibiValue  = "";
        
        // parsing variables	
        private int      weightStartpoint = 0;
        private int      weightLength     = 0;
        private int      alibiStartPoint  = 0;
        private int      alibilength      = 0;
        private int      motionStartPoint = 0;
        private int      motionLength     = 0;	
        private String   motionValue      = "";
        private String   returnValues     = "";
        
        // Serial port properties
        private int      baudRate  = 0;
        private int      dataBits  = 0;
        private int      stopBits  = 0;
        private int      parity    = 0;  
    
        public static void main(String[] args) {
            //parsing variables
            String      weightStartpoint = "4 ";
            String      weightLength     = " 6";
            String      alibiStartPoint  = "45";
            String      alibilength      = " 6 ";
            String      motionStartPoint = " 12 ";
            String      motionLength     = "3   ";      
            String      motionValue      = "20"; 
            
            //Serial port varaibles
            String baudRate  = " 9600 ";
            String dataBits  = " 7 ";
            String stopBits  = " 1 ";
            String parity    = " NONE ";
            
            TestComm reader  = new TestComm(weightStartpoint ,weightLength,alibiStartPoint ,alibilength ,motionStartPoint ,motionLength , motionValue);
            reader.setSerialPortproperties(baudRate ,dataBits ,stopBits ,parity);
    	reader.init();
            reader.getValues();
        }
    
        public TestComm(String weightStartpoint,String weightLength,String alibiStartPoint,String alibilength, String motionStartPoint, String motionLength, String motionValue) {
          //trim all white space off variables
          weightStartpoint      = weightStartpoint.trim();
          weightLength          = weightLength.trim();
          alibiStartPoint       = alibiStartPoint.trim();
          alibilength           = alibilength.trim();     
          motionStartPoint      = motionStartPoint.trim();
          motionLength          = motionLength.trim();
          
          //convert to int
          
          this.weightStartpoint = Integer.parseInt(weightStartpoint);   
          this.weightLength     = Integer.parseInt(weightLength);
          this.alibiStartPoint  = Integer.parseInt(alibiStartPoint);
          this.alibilength      = Integer.parseInt(alibilength);
          this.motionStartPoint = Integer.parseInt(motionStartPoint);  
          this.motionLength     = Integer.parseInt(motionLength); 
          
          //motion value remains a string
          this.motionValue      = motionValue;
        }
        
        
        public void setSerialPortproperties(String baudRate, String dataBits, String stopBits, String parity){
        
          //trim all white space off variables
          baudRate      = baudRate.trim();
          dataBits      = dataBits.trim();
          stopBits      = stopBits.trim();
          parity        = parity.trim();
          
          // convert baud rate to int
          this.baudRate = Integer.parseInt(baudRate);
          System.out.println("baudRate set to : "+this.baudRate);
          
          //check data bits property
          if( dataBits.equals("7")){
             this.dataBits = SerialPort.DATABITS_7;
             System.out.println("Databit set to 7");
          }
          else if(dataBits.equals("8")){
             this.dataBits = SerialPort.DATABITS_8;
              System.out.println("Databit set to 8");
          }
          else {
              this.dataBits = SerialPort.DATABITS_7; 
          }
          
          //check stop bits property
          if( stopBits.equals("1")){
             this.stopBits = SerialPort.STOPBITS_1;
             System.out.println("Stopbit set to 1");
          }
          else if( stopBits.equals("1.5")){
             this.stopBits = SerialPort.STOPBITS_1_5 ;
              System.out.println("Stopbit set to 1.5");
          }
          else if(stopBits.equals("2")){
             this.stopBits = SerialPort.STOPBITS_2; 
             System.out.println("stopBit set to 2");
          }
          else{
             this.stopBits = SerialPort.STOPBITS_1; 
          }
          
          //check parity property
          if(parity.equals("EVEN")){
              this.parity = SerialPort.PARITY_EVEN;
              System.out.println("parity set to even");
          }
          else if(parity.equals("MARK")){
              this.parity = SerialPort.PARITY_MARK;
              System.out.println("parity set to mark");
          }
          else if(parity.equals("NONE")){
              this.parity = SerialPort.PARITY_NONE;
              System.out.println("parity set to none");
          }
          else if(parity.equals("ODD")){
              this.parity = SerialPort.PARITY_ODD;  
              System.out.println("parity set to odd");
          }
          else if(parity.equals("ODD")){
              this.parity = SerialPort.PARITY_SPACE;
              System.out.println("parity set to space");
          }
          else {
              this.parity = SerialPort.PARITY_NONE;
          }
        }
        
        public String getValues(){
            returnValues =readValues();
            return returnValues;
        }
        public void run() {
          try {
            Thread.sleep(20000);
          } catch (InterruptedException e) {System.out.println(e);}
        }
    	
        public void init(){
         String driverName = "com.sun.comm.Win32Driver";
          try{
            CommDriver commdriver =
            (CommDriver)Class.forName(driverName).newInstance( );
            commdriver.initialize();
            }
            catch (Exception e2)
            {
            e2.printStackTrace();
            }
            
            portList = CommPortIdentifier.getPortIdentifiers();
    
            while (portList.hasMoreElements()) {
                System.out.println("port list has elements");
                portId = (CommPortIdentifier) portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                     if (portId.getName().equals("COM1")) {
    			//                if (portId.getName().equals("/dev/term/a")) {
                        //TestComm reader = new TestComm();
    		    start();
                    }
                }
            }
        }
    	
    	public void start(){
    	    try {
                serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
            } catch (PortInUseException e) {System.out.println(e);}
            try {
                inputStream = serialPort.getInputStream();
            } catch (IOException e) {System.out.println(e);}
    	    try {
                serialPort.addEventListener(this);
    	    } catch (TooManyListenersException e) {System.out.println(e);}
            serialPort.notifyOnDataAvailable(true);
            try {
                 serialPort.setSerialPortParams(baudRate,
                    dataBits,
                    stopBits,
                    parity);
                System.out.println("set serail port params");
            } catch (UnsupportedCommOperationException e) {System.out.println(e);}
            readThread = new Thread(this);
            readThread.start();
        }
        
        public String readValues(){
        
          String readValue = "";
          boolean steady = false;
          
            while(steady  == false){
            //is = new BufferedReader(new InputStreamReader(inputStream));
                           
            byte[] readBuffer = new byte[500];
            int numBytes = 0;
            try {
              //while (inputStream.available() > 0) {
                numBytes = inputStream.read(readBuffer);
              //}    
              
              
            readValue = new String(readBuffer);
            System.out.println("Value read from Buffer : "+readValue);
            
            
            
            readValue = parseReading(readValue);
            steady = checkIfStable(readValue);
         
              
            }catch (IOException e) {}
            
            }
          return readValue;
        }
        
        
        public String parseReading(String readValue) {
            String[] lines = null;
            String   tempWeight = "0";
            String   tempAlibi  = "0";
            
            
            
            if(readValue.length() > 0)
            {
              lines = readValue.split("\\r\\n");
            }
            
            if( lines.length > 1){
              readValue = lines[lines.length-2];
            }
            
            if(checkIfStable(readValue)){
              System.out.println("Will continue parsing!");
              System.out.println("Line for parsing : "+readValue);
                
              tempWeight  = readValue.substring(weightStartpoint, (weightStartpoint+weightLength));
              tempWeight  = tempWeight.trim();
              tempAlibi   = readValue.substring(alibiStartPoint, (alibiStartPoint+alibilength));
              tempAlibi   = tempAlibi.trim();
                 
              Pattern p  = Pattern.compile(".*\\d.*");
              Pattern p2 = Pattern.compile("(\\d+)");
              Matcher m  = p.matcher(tempWeight);
              Matcher m2 = p2.matcher(tempWeight);
              //boolean b = m.matches();    
               
              System.out.println("tempWeight : "+tempWeight);
              if(m.matches()){
                System.out.println("tempWeight contains a number value");
                if(m2.find()){
                    System.out.println("found number value");
                    weightValue = m2.group(0);
                    System.out.println("weightValue : "+weightValue);
                }
                
              }
              else {
                 System.out.println("weightValue does not contain a number value");
                 System.out.println("tempWeight : "+tempWeight);
                 weightValue = "0";
              }
              
              m  = p.matcher(tempAlibi); 
              m2 = p2.matcher(tempAlibi);
              
              System.out.println("tempAlibi : "+tempAlibi);
              if(m.matches()){
                System.out.println("tempAlibi contains a number value");
                if(m2.find()){
                  System.out.println("found number value"); 
                  alibiValue = m2.group(0);
                  System.out.println("alibiValue : "+alibiValue);
                }
              }
              else{
                  System.out.println("AlibiValue is not a number value");
                  System.out.println("tempAlibi : "+tempAlibi);
                  alibiValue = "0";
              }
                
            }
            else{
                System.out.println("Scales is unsteady check for another value");
            }
            
            returnValues = weightValue+","+alibiValue;
            
            return readValue;
        }
        
        public boolean checkIfStable(String readValue){
            String   tempMotion = "0";
            boolean  steady = false;
            
            try{
              tempMotion  = readValue.substring(motionStartPoint, (motionStartPoint+motionLength ));
            } catch(StringIndexOutOfBoundsException se){}
            
            System.out.println("tempMotion  : "+tempMotion);
            System.out.println("motionValue : "+motionValue);
            
            tempMotion = tempMotion.trim();
            if( tempMotion.equals(motionValue)){
                System.out.println("Scales is steady");
                steady = true;
            }
            else {
                System.out.println("Scales is unsteady");
                steady = false;
            }
            
            return steady;
        }
    
    
        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:
                try
                {
                   readThread.sleep(1000);
                   readValues();
                }
                catch (InterruptedException e)
                {
                }
    
                break;
            }
        }
    }

    I hope you can help me. Thanks in advance.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Add new package javax.comm
    By rjagan in forum New To Java
    Replies: 5
    Last Post: 04-12-2011, 09:07 AM
  2. import javax.comm
    By kareena in forum Threads and Synchronization
    Replies: 2
    Last Post: 09-21-2010, 03:45 PM
  3. Replies: 7
    Last Post: 07-12-2010, 04:24 PM
  4. javax/comm/SerialPortEvent
    By musiigedeo in forum Threads and Synchronization
    Replies: 9
    Last Post: 11-24-2008, 11:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •