Main /
RxTxErrorOpeningLockFileSummaryCalling testRead() Lock file failed RXTX fhs_lock() Error: opening lock file: /var/lock/LCK..ttyUSB0: File exists. It is mine What is happening is that the first time we call Then, the second time we call Basically, this message can be ignored. A workaround would be to call A better fix would be to have not print the lock message if we are enumerating through the ports because a port could be locked between the time we enumerate the ports and the time we actually try to open the port. How to RepeatUnder Ubuntu 14.04, running $PTII/bin/vergil $PTII/ptolemy/actor/lib/jjs/modules/serial/test/auto/SerialHelloWorld.xml The error messages appear: testRead() Lock file failed RXTX fhs_lock() Error: opening lock file: /var/lock/LCK..ttyUSB0: File exists. It is mine Does the lock file exist?Does /var/lock/LCK..ttyUSB0 exist before you run the model? If so then remove it. Also, it could be that the XCTU process is holding the lock. Also, it could be that the model is referring to the same port in both actors. SourceCodePtolemy is using nrjavaserial. To get the sources, use: git clone https://github.com/NeuronRobotics/nrjavaserial.git To find the file: cd nrjavaserial find . -type f | xargs grep "opening lock file" The results include: ./src/main/c/src/SerialImp.c: "RXTX fhs_lock() Error: opening lock file: s.", ./src/main/c/src/SerialImp.c: "RXTX uucp_lock() Error: opening lock file: s\n", ./src/main/c/src/SerialImp.c: "RXTX is_device_locked() Error: opening lock file: s\n", Thus src/main/c/src/SerialImp.c is the file and int fhs_lock( const char *filename, int pid ) { /* * There is a zoo of lockdir possibilities * Its possible to check for stale processes with most of them. * for now we will just check for the lockfile on most * Problem lockfiles will be dealt with. Some may not even be in use. * */ #if defined(__linux__) //return 0; #endif int fd,j; char lockinfo[12], message[200]; char file[200], *p; j = strlen( filename ); p = ( char * ) filename + j; /* FIXME need to handle subdirectories /dev/cua/... SCO Unix use lowercase all the time taj */ while( *( p - 1 ) != '/' && j-- != 1 ) { #if defined ( __unixware__ ) *p = tolower( *p ); #endif /* __unixware__ */ p--; } sprintf( file, "%s/LCK..%s", LOCKDIR, p ); if ( check_lock_status( filename ) ) { report_warning( "fhs_lock() lockstatus fail\n" ); return 1; } fd = open( file, O_CREAT | O_WRONLY | O_EXCL, 0666 ); if( fd < 0 ) { sprintf( message, "RXTX fhs_lock() Error: opening lock file: %s: %s.", file, strerror(errno) ); report_error( message ); fd = open( file, O_WRONLY ); if( fd < 0 ){ sprintf( message, " FAILED TO OPEN: %s\n", strerror(errno) ); report_error( message ); return 1; } if(check_lock_pid( file, pid )==0){ report_error(" It is mine\n" ); } report_error( "\n" ); close( fd ); return 1; } sprintf( lockinfo, "%10d\n",(int) getpid() ); sprintf( message, "fhs_lock: creating lockfile: %s\n", lockinfo ); report( message ); if( ( write( fd, lockinfo, 11 ) ) < 0 ) { sprintf( message, "RXTX fhs_lock() Error: writing lock file: %s: %s\n", file, strerror(errno) ); report_error( message ); close( fd ); return 1; } close( fd ); return 0; } Building nrjavaserialSee , but basically: cd ~/src git clone https://github.com/NeuronRobotics/nrjavaserial.git sudo add-apt-repository ppa:cwchien/gradle sudo apt-get update sudo apt-get install gradle cd nrjavaserial make linux64 cp ./build/libs/nrjavaserial-3.11.0.jar $PTII/lib/nrjavaserial-3.11.0.devel.jar Debugging
The above looks like a locking problem. AnalysisSearching using the error message comes up with:
I tried export JAVAFLAGS=-Dgnu.io.rxtx.SerialPorts=/dev/ttyUSB1:/dev/ttyUSB0 but that did not help. Use XCTUAs an experiment, I first used the XBeeJava Example to verify that the XBee Java example would write data to one XBee and then use XCTU to read from another XBee. Then, I exited XCTU, started the model, removed the reader portion of the model and ran it. The data did not appear in XCTU, indicating that the writer portion (which is like the XBeeHello example), does not work. I then did the reverse, where I removed the writer portion of the model and ran the XBeeHello example. The data did not appear. So, it seems like the SerialHello.xml example is not the same as the XCTU/XBeeHello example. This is probably because the XBee devices are in AP mode when I use XCTU and XBeeHello. Revised TestAs per the above, $PTII/bin/vergil $PTII/ptolemy/actor/lib/jjs/modules/serial/test/auto/SerialHelloWorld.xml does not work. However, $PTII/bin/vergil $PTII/org/terraswarm/accessor/demo/XBeeHelloWorld/XBeeHelloWorld.xml does work. The problem is that the second time, it fails. To replicate:
"15) where sprintf is being used, ensure that the existing ptr refers to
an empty string (uses memset to sizeof)" Instead of (*env)->ReleaseStringUTFChars(env, tty_name, name); report_error(name); report_error( " testRead() Lock file failed\n" ); do: memset(&message[0], 0, sizeof(message)); sprintf(message, "%s testRead() Lock file failed!\n", name ); (*env)->ReleaseStringUTFChars(env, tty_name, name); report_error(message); It is probably more that we have the call to One thing that is interesting is that when we start, we sometimes see: RXTX Warning: Removing stale lock file. /var/lock/LCK..ttyUSB1 RXTX Warning: Removing stale lock file. /var/lock/LCK..ttyUSB0 However, we don't see that the second time, when it fails. The above lines occur in /*---------------------------------------------------------- is_device_locked accept: char * filename. The device in question including the path. perform: see if one of the many possible lock files is aready there if there is a stale lock, remove it. return: 1 if the device is locked or somethings wrong. 0 if its possible to create our own lock file. exceptions: none comments: check if the device is already locked ----------------------------------------------------------*/ int is_device_locked( const char *port_filename ) { ... if( kill( (pid_t) pid, 0 ) && errno==ESRCH ) { sprintf( message, "RXTX Warning: Removing stale lock file. %s\n", file ); report_warning( message ); if( unlink( file ) != 0 ) { snprintf( message, 80, "RXTX Error: Unable to \ remove stale lock file: %s\n", file ); report_warning( message ); return 1; } } This suggests that during the second time, we already have the lock. Replication using a small test caseimport gnu.io.CommPort; import gnu.io.CommPortIdentifier; import java.util.Enumeration; public class SerialPortLockTest { /** * Test for a locking problem * <p>Usage:</p> * <pre> * javac -classpath .:lib/nrjavaserial-3.11.0.devel.jar SerialPortLockTest.java * java -classpath .:lib/nrjavaserial-3.11.0.devel.jar SerialPortLockTest * </pre> */ public static void main(String [] args) { try { System.out.println("Opening Port 1"); CommPort port1 = SerialPortLockTest.openPort(); System.out.println("Port 1: " + port1); System.out.println("Opening Port 2"); CommPort port2 = SerialPortLockTest.openPort(); System.out.println("Port 2: " + port2); System.out.println("Closing port1: "); port1.close(); System.out.println("Closing port2: "); port2.close(); System.out.println("Done."); } catch (Throwable throwable) { throwable.printStackTrace(); } } /** Open the first non-Bluetooth and non-cu Serial port. * @return the port that was opened * @exception Throwable If looking up a port failed. */ public static CommPort openPort() throws Throwable { System.out.println("Calling gnu.io.CommPortIdentifier.getPortIdentifiers()"); Enumeration ports = CommPortIdentifier.getPortIdentifiers(); System.out.println("Done calling gnu.io.CommPortIdentifier.getPortIdentifiers()"); while (ports.hasMoreElements()) { CommPortIdentifier identifier = (CommPortIdentifier) ports .nextElement(); if (identifier.getName().indexOf("/dev/cu.") != -1 || identifier.getName().indexOf("Bluetooth") != -1) { System.out.println("Skipping " + identifier.getName()); } else { CommPortIdentifier portID = CommPortIdentifier.getPortIdentifier(identifier.getName()); CommPort port = portID.open("SerialPortLockTest", 1000 /* timeout for opening */); return port; } } throw new RuntimeException("Could not find any non-Bluetooth, non tty serial ports."); } } [cxh@terra ptII]$ javac -classpath .:lib/nrjavaserial-3.11.0.devel.jar SerialPortLockTest.java [cxh@terra ptII]$ java -classpath .:lib/nrjavaserial-3.11.0.devel.jar SerialPortLockTest Opening Port 1 Calling gnu.io.CommPortIdentifier.getPortIdentifiers() Done calling gnu.io.CommPortIdentifier.getPortIdentifiers() Port 1: /dev/ttyS0 Opening Port 2 Calling gnu.io.CommPortIdentifier.getPortIdentifiers() RXTX fhs_lock() Error: opening lock file: /var/lock/LCK..ttyS0: File exists. It is mine \234^\\244^R^? testRead() Lock file failed Done calling gnu.io.CommPortIdentifier.getPortIdentifiers() Port 2: /dev/ttyS1 Closing port1: Closing port2: Done. [cxh@terra ptII]$ AnalysisIn the above run, we get port 1 (/dev/ttyS0) and then call RXTX fhs_lock() Error: opening lock file: /var/lock/LCK..ttyS0: File exists. It is mine \234^\\244^R^? testRead() Lock file failed This happens because
What is happening is that the first time we call Then, the second time we call Basically, this message can be ignored. A workaround would be to call A better fix would be to have not print the lock message if we are enumerating through the ports because a port could be locked between the time we enumerate the ports and the time we actually try to open the port. See Also |