Opening the same serial port multiple times

Post bugreports for the Linux version here
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Opening the same serial port multiple times

Post by Cezary »

Dear forum users,
I noticed a strange operation of the OpenSerialPort() command. Execution of such code:

Code: Select all

Debug OpenSerialPort(#PB_Any, "/dev/ttyUSB0",
                     1200, #PB_SerialPort_NoParity,
                     8, 1, #PB_SerialPort_NoHandshake, 256, 256)
Debug OpenSerialPort(#PB_Any, "/dev/ttyUSB0",
                     1200, #PB_SerialPort_NoParity, 
                     8, 1, #PB_SerialPort_NoHandshake, 256, 256)
Debug OpenSerialPort(#PB_Any, "/dev/ttyUSB1", 
                     1200, #PB_SerialPort_NoParity, 
                     8, 1, #PB_SerialPort_NoHandshake, 256, 256)
Debug OpenSerialPort(#PB_Any,  "/dev/ttyUSB1", 
                     1200, #PB_SerialPort_NoParity, 
                     8, 1, #PB_SerialPort_NoHandshake, 256, 256)
returns four non-zero values, for example:

Code: Select all

18019064
18019112
18019160
18019208
In my opinion, the second and fourth values should be zero (no port available). Do I think right?
(PureBasic 6.03 beta 3 LTS (Linux - x64))
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: Opening the same serial port multiple times

Post by Cezary »

Would anyone like to do this test? You only need one USB->RS232 converter to test this code:

Code: Select all

Debug OpenSerialPort(#PB_Any, "/dev/ttyUSB0",
                     1200, #PB_SerialPort_NoParity,
                     8, 1, #PB_SerialPort_NoHandshake, 256, 256)
Debug OpenSerialPort(#PB_Any, "/dev/ttyUSB0",
                     1200, #PB_SerialPort_NoParity, 
                     8, 1, #PB_SerialPort_NoHandshake, 256, 256)
On Linux Mint and on Raspberry Pi I get two nonzero values.
With this code on Windows:

Code: Select all

Debug OpenSerialPort(#PB_Any, "COM8",
                     1200, #PB_SerialPort_NoParity,
                     8, 1, #PB_SerialPort_NoHandshake, 256, 256)
Debug OpenSerialPort(#PB_Any, "COM8",
                     1200, #PB_SerialPort_NoParity, 
                     8, 1, #PB_SerialPort_NoHandshake, 256, 256)
- it behaves as expected: first value nonzero and second is zero.
Shouldn't Linux return that the port is already in use?
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: Opening the same serial port multiple times

Post by Cezary »

After searching the web, I found that in Linux the serial port needs to be locked using ioctl(fd, TIOCEXCL) and/or flock(fd, LOCK_EX | LOCK_NB). Unfortunately, I don't know how to do it in PB, and I don't know which header file contains the definition of TIOCEXCL.
Any idea, please?
Fred
Administrator
Administrator
Posts: 16686
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Opening the same serial port multiple times

Post by Fred »

I just added the flag, could you test it when the next beta is out to see if it works as expected ?
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: Opening the same serial port multiple times

Post by Cezary »

Fred,
of course I will check. Thank you.
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: Opening the same serial port multiple times

Post by Cezary »

Fred,
within a single application it is now fine - once a specific port is opened, it cannot be reopened without first closing it. Previously, I protected against this possibility in my application by creating a list of opened ports. Now I can skip making such a list.
Unfortunately, I also run more than one application using serial ports on a single computer, and then the lock would have to be at the system kernel level. However, since there is no such locking, the ports must be assigned "rigidly". It is not possible to use the flexible search mechanism (by querying) where a device is connected, because it interferes with the communication of already connected devices. This, of course, requires additional software configuration.
But anyway, thanks for trying to solve it and for your effort.
Fred
Administrator
Administrator
Posts: 16686
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Opening the same serial port multiple times

Post by Fred »

Is it a solution for this ? If you run you exe as root, it behaves the same ? I can try to add the flock() to see if it does any different ?
Cezary
User
User
Posts: 89
Joined: Sun Feb 12, 2017 2:31 pm

Re: Opening the same serial port multiple times

Post by Cezary »

I run one program that opens the port, e.g. "/dev/ttyUSB0" and keeps it open. Then I run the second program, which also tries to open the "/dev/ttyUSB0" port: id = OpenSerialPort(#PB_Any, "/dev/ttyUSB0", ...) statement returns a non-zero value. But (what I didn't notice before) subsequent runs of program #2 return id = 0.
It does not matter whether the first program is started by the root user or not.
So it's almost fine, except for the first run of program #2.
Post Reply