Results 1 to 6 of 6

Thread: Redirecting to a COM port

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Redirecting to a COM port

    Hello,
    I use a program in java API javax.comm to read info from a USB GPS Receiver (detected on a virtual serial port). Everything works very well (opening COM port, reading data and then closed without any problem!). But I wonder if there is a solution to detect and configure the COM port automatically. Let me explain: at this stage the user must complete a config file that I created them in informing the COM port number on which is mounted GPS receiver and the speed of the link. Is there a method to redirect the COM port or serial port scanner to identify the receptor and therefore that the bilingual is no longer a config file complete? Thank you in advance.
    Last edited by Remedy; 27-01-2010 at 01:51 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Redirecting to a COM port

    Hello,
    If you want to try you can try the following code, here it is
    Code:
    Enumeration lspt CommPortIdentifier.getPortIdentifiers = ();
    
    while (lspt.hasMoreElements ())
    (
    CommPortIdentifier ptid = (CommPortIdentifier) (CommPortIdentifier) lspt.nextElement ();
                    
    if (ptid.getPortType () == typePort & &! ptid.isCurrentlyOwned ())
    (
      / / Here test device
    }
    In the loop if you send a question found on the port and wait for an answer.

  3. #3
    Join Date
    Dec 2009
    Posts
    192

    Re: Redirecting to a COM port

    Hello,
    Thank you for your answer. By cons I do not understand your line:
    if (ptid.getPortType() == TypePort & &! Ptid.isCurrentlyOwned())
    Where does the typePort redirect the code to? Is it me who should indicate what type of port it is? I am really confused with this now. If you have an more detailed ideas you can explain me in a simple language, it would be great.

  4. #4
    Join Date
    Oct 2009
    Posts
    67

    Re: Redirecting to a COM port

    Hello,
    Even I had a similar kind of a problem some days before. By knowledge the getPortType () returns an int (1 = COM port and LPT = 2). By cons, if there are several devices that use a COM port to be how I speak very good at? For isCurrentlyOwned function () just returns true or false if the device is used. If you know any other methods that can you used for the same reason then please do post back. Thank you in advance.

  5. #5
    Join Date
    Dec 2009
    Posts
    192

    Re: Redirecting to a COM port

    Hello,
    Here is my code, see if you can help me with it.
    Code:
    Public class TESTCOM implements srptEventListener {
    	
        private String ptcm;
        private CommPortIdentifier ptid = null; / / Port ID
        private srpt srpt; / / serial port
        private BufferedReader fllx; / / sequential read port
     
    	
    	/ *
    * Method which initializes the serial port evenementiel
    * /
    	Public void ModeEvenement(String ptcm) {
    		/ / fetch the ID of the port
    		try {
    			ptid = CommPortIdentifier.getPortIdentifier(ptcm);
    		} catch (NoSuchPortException e) {
    		}
    		
    		/ / open port
    		try {
    			srpt = (srpt) ptid.open("ModeEvenement", 2000);
    		} catch (PortInUseException e) {
    		}
    		
    		/ / get stream
    		try {
    			fllx =
    				new BufferedReader(
    					new InputStreamReader(srpt.getInputStream()));
    		} catch (IOException e) {}
    		
    		/ / add the listener
    		try {
    			srpt.addEventListener(this);
    		} catch (TooManyListenersException e) {
    		}
    		/ / port settings
    		srpt.notifyOnDataAvailable(true);
    		try {
    			srpt.setsrptParams(
    				4800,
    srpt.DATABITS_8,
    srpt.STOPBITS_1,
    srpt.PARITY_NONE);
    		} catch (UnsupportedCommOperationException e) {}
    		System.out.System.out.println("open port, waiting to be read");
    	}	
        
        / *
         * Method which performs the reading of 7 frames on the serial port
         * Once the reading is closed the flow of reading and the COM port
         * /
        Public void Readsrpt(){
        	int i =7;
        String response = new String();
    		
    		try {
    			System.out.System.out.println("i ="+ i);
    			while(i! =0){
    				System.out.System.out.println("We read the COM port\ n");
    response = (String) fllx.readLine();
    System.out.System.out.println(Answer);
    i -;
    System.out.System.out.println("i ="+ i);
    			}
    		} catch (IOException e) {}
    		/ / close the flow of reading
    		try {
    			fllx.close();
    		} catch (IOException e) {}
    		/ / Close COM port
    		srpt.close();
    	}
    	
    	Public void serialEvent(srptEvent event) {
    		/ / event management on port:
    		/ / we do nothing except when the data are available
    				switch (event.getEventType()) {
    			box srptEvent.DATA_AVAILABLE :
    				this.Readsrpt();/ / if data available on launch reading
    				break;
    			default:
    				break;/ / nothing is done for other events
    		}
    	}
        
        / *
         * Method which will scan all COM ports and test if the data come from the COM port scan
         * /	
    	Public void listPort(){
    		Enumeration listeports = CommPortIdentifier.getPortIdentifiers();
    		int typePort;
    String GPSPortCOM;
    		
    		while (listeports.hasMoreElements()){
    			ptid = (CommPortIdentifier) (CommPortIdentifier) listeports.nextElement();
    			if(ptid.getPortType()== CommPortIdentifier.PORT_SERIAL){    
      				System.out.System.out.println("Port Name:"+ ptid.getName());
      System.out.System.out.println("User"+ ptid.getCurrentOwner());
      System.out.System.out.println("Use?"+ ptid.isCurrentlyOwned());
      System.out.System.out.println("Type of port:"+ ptid.getPortType());
      				/ / We launch event management on ptid
    				this.ModeEvenement(ptid.getName());
    			}
    		}
    	}
     
    	Public static void hand(String[] args) {
    		/ / initialize the driver
    		Win32Driver w32Driver = new Win32Driver();
    w32Driver.initialize();
    
    TESTCOM test = new TESTCOM();
    test.listPort();
    	}
    }

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: Redirecting to a COM port

    Hello,
    Regarding your bug, would not you forgot to close the ports then? Moreover, I have a question regarding the execution of your code. I work with the same API and I switch from port to port through the loop:
    while (listePorts.hasMoreElements ())
    Is it normally returns 2 times on the COM ports (I have 3 so it is COM1 COM2 COM3 COM1 COM2 COM3)?. The code triggers an exception for the last 3 ports.

Similar Threads

  1. Replies: 5
    Last Post: 26-06-2010, 05:21 PM
  2. Replies: 1
    Last Post: 02-03-2009, 11:15 PM
  3. Redirecting the URL to another Web server
    By Luther in forum Technology & Internet
    Replies: 2
    Last Post: 24-10-2008, 06:03 PM
  4. Help me redirecting dynamic IP
    By Maximus() in forum Networking & Security
    Replies: 3
    Last Post: 04-08-2008, 03:25 PM
  5. Missing port types : local port and Standard TCP/IP port
    By NIcaBoy in forum Windows XP Support
    Replies: 3
    Last Post: 28-12-2006, 07:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,659,656.02694 seconds with 17 queries