Fazecast / jSerialComm

Platform-independent serial port access for Java
GNU Lesser General Public License v3.0
1.32k stars 279 forks source link

Denying access to multiple applications on serialport JSerialComm JavaSwing #63

Closed lnsriravichandran22 closed 6 years ago

lnsriravichandran22 commented 7 years ago

Hello Everyone out there!!

I have been developing a Java application for implementing a serial communication using JSerialcomm Library. My Operating System is Ubuntu 14.04.

As per the instructions in the API page, I have been opening the serial port, using it and close it whenever , I'm done with the task.

Problem: Let's say that I have opened the application, have done communicating and have closed the port. Now until I access the port again, it will not be opened by this instance of application.

But whenever I open another instance of the application and try opening the port, the newer instance of the application is able to open the serial port and hence the previously opened instance of the application , no more has access to the serial port.

Help Needed: Is there a way to lock the serial port to any instance of the application on First Come First Server Basis. That is the instance of the application opened first should lock the serial port to itself and any other successive instances should be unable to open it.

Thanks in Advance

haniibrahim commented 7 years ago

Looks like that the port was not closed properly. On GNU/Linux closePort() works in general. Do you have any code.

I observed a weird behavior in conjunction with multi-threading (SwingWorker) and streams (BufferedReader) especially on macOS => #58

lnsriravichandran22 commented 7 years ago

Thanks a lot for your response!! Issue #58 looks a bit different from what I face. Correct me if I'm wrong.

My understanding is that even though the port is closed properly in the application, it is possible for another instance of the same application (or some other application) to access the serial port. This is because, in my case once if I use the serial port once, I am sure that closing it by calling closePort() function and after calling this function, I checked whether the serial port is opened or closed on a button click event. It says that the port is closed.

Hence a way to lock the serial port to the specific instance of application should be worked out it seems.

By the way What do you mean by "On GNU/Linux closePort() works in general. "

haniibrahim commented 7 years ago

My understanding is that even though the port is closed properly in the application, it is possible for another instance of the same application (or some other application) to access the serial port.

Yes, that works.

But when you use another thread make sure that the closePort() method is really called before the other thread is calling openPort(). They need to be synchronized.

In two different apps I wrote I open the port in a SwingWorker threat and close it in the event dispatch threat (EDT). Than reopen the port and so on. Works on GNU/Linux.

How do you check whether the port is open or not? With the isOpen() method? It just checks if openPort() has opened the port only. If the port was open from another application it does not work. In this case you have to check it with openPort() and evaluate the boolean return value. But openPort() is expensive.

By the way What do you mean by "On GNU/Linux closePort() works in general. "

That means in special circumstances (refer #58) after interrupted the reading via stream and reopen the port the first line received is suppressed on GNU/Linux. Opening and closing happen in different threats (SwingWorker->EDT). But reopening itself is not a problem at all.

Excerpt #58: When I open a connection on GNU/Linux the first time all work well. When I close and reopen the port (w/o closing and reopening the program) the first line committed is suppressed the following lines are displayed. This happens the same with every following attempt. When I close the program and reopen it the game starts all over again. The first connection works always well.

I use(d) the library 1.3.11 on Oracle Java 6 and OpenJDK 7/8 on Mint 17,3/18 where 17.3 is similar to Ubuntu 14.04.3.

hedgecrw commented 6 years ago

Going back to the original problem statement, it seems as though you are looking to have jSerialComm lock access to a serial port regardless of whether or not the port is open, thereby only allowing the first instance of your application to have access to the port. Unfortunately, port locking is an operating system process which is not accessible unless the port in question is currently opened. So the only way to achieve what you are trying to do would be to have your first application open the port and keep it open (thereby locking out all future application instances), or to rewrite some of your application logic so that it has some sort of "lockfile" mechanism to be able to determine whether another instance of your application exists and has access to the (presumably closed) port.

lnsriravichandran22 commented 6 years ago

@hedgecrw Thank you so much for the suggestion. Anyways I have implemented it in the same way!!. But I just wanted to know the proper way of implementing this.