[prev in list] [next in list] [prev in thread] [next in thread] 

List:       rxtx
Subject:    Re: [Rxtx] Error when trying to open fake serial port
From:       Marcin Depczyński <marcin-depczynski () veraxsystems ! com>
Date:       2013-06-18 12:56:30
Message-ID: 51C058FE.70004 () veraxsystems ! com
[Download RAW message or body]

Thx Alex for fast reply.

Regards,
Marcin


On 2013-06-18 10:53, Alexander Graf wrote:
> Hi Marcin,
> you are somehow fighting the API. (Yes, it is not very well documented 
> ...) Don't ever use reflection to break into an API implementation. 
> For your problem of adding a non standard port it should be sufficient 
> to set the gnu.io.rxtx.SerialPorts property. The properties are read 
> by the API automatically while initialization and so the (only) driver 
> is chosen to access the port. Do not call 
> CommPortIdentifier.addPortName() yourself.
>
> Properties props = System.getProperties();
> props.setProperty("gnu.io.rxtx.SerialPorts", "/dev/test");
> // enumerate ports and use them
>
> Regards
> Alex
>
> On 06/18/2013 09:28 AM, Marcin Depczyński wrote:
>> I try to properly initialize virtual serial port under linux. Because my
>> port isn't discovered as other system serial ports, I must add it
>> manually. The problem is, if the user tries to set fake serial port
>> (e.g. "1234", "abc") instead of put valid one:
>>
>> Serial portName = "abc";
>> CommPortIdentifier.addPortName(portName, CommPortIdentifier.PORT_SERIAL,
>> rxtxDriver) executes normally,
>> but any attempt to open the port leads to abnormal program termination:
>> CommPortIdentifier portId = 
>> CommPortIdentifier.getPortIdentifier(portName);
>> serialPort = (SerialPort) portId.open("Demo application", 5000);
>>
>>
>> Here is my code responsible for preparing serial ports:
>>
>> private void prepareSerialPort(String portName) {
>>          final String pathSep = System.getProperty("path.separator", 
>> ":");
>>          String ports = "";
>>
>>          Enumeration<CommPortIdentifier> portEnum =
>> CommPortIdentifier.getPortIdentifiers(); // discover existing ports
>>          while (portEnum.hasMoreElements()) {
>>              CommPortIdentifier portIdentifier = portEnum.nextElement();
>>              if (CommPortIdentifier.PORT_SERIAL ==
>> portIdentifier.getPortType()
>>                      && !portName.equals(portIdentifier.getName())) {
>>                  ports += portIdentifier.getName();
>>                  ports += pathSep;
>>              }
>>          }
>>
>>          ports += portName;
>>          System.setProperty("gnu.io.rxtx.SerialPorts", ports);
>>          System.out.println("gnu.io.rxtx.SerialPorts: " + ports);
>>
>>          try {
>>              CommPortIdentifier.getPortIdentifier(portName);
>>          } catch (NoSuchPortException ex) {
>>              try {
>>                  // **** This is risky *****
>>                  CommDriver rxtxDriver = (CommDriver)
>> Class.forName("gnu.io.RXTXCommDriver").newInstance();
>>                  rxtxDriver.initialize();
>>                  CommPortIdentifier.addPortName(portName,
>> CommPortIdentifier.PORT_SERIAL, rxtxDriver);
>>              } catch (Throwable t) {
>>                  System.out.println(t + " thrown while loading
>> gnu.io.RXTXCommDriver");
>>              }
>>          }
>>     }
>>
>> Regards,
>> Marcin
>>
>>
>> _______________________________________________
>> Rxtx mailing list
>> Rxtx@qbang.org
>> http://mailman.qbang.org/mailman/listinfo/rxtx
> _______________________________________________
> Rxtx mailing list
> Rxtx@qbang.org
> http://mailman.qbang.org/mailman/listinfo/rxtx

_______________________________________________
Rxtx mailing list
Rxtx@qbang.org
http://mailman.qbang.org/mailman/listinfo/rxtx

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic