Serial port becomes unreachable after being opened and closed once.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Serial port becomes unreachable after being opened and closed once.
I am writing a program to read and write to devices over my serial port. My program will need to connect to up to four other machines at once. I can connect to machines successfully, it works well. However, after I connect and disconnect once, attempting to connect again will cause the FIRST serial connection will fail and all the ones after it will pass.
e.g.
That works as long as it is executed only once in the program. The second time it is executed, fd[0] will die and fd[1-3] will work normally.
I can't figure out why this happens, but I found a workaround by connecting to "/dev/null" before any real serial ports.
What should I do?
It would be helpful to know exactly what you mean by 'die'. Are you testing the return status of all system calls, especially open()? Printing error messages/codes?
--- rod.
It would be helpful to know exactly what you mean by 'die'. Are you testing the return status of all system calls, especially open()? Printing error messages/codes?
--- rod.
Its just the exact same code being executed twice. All connections are closed before trying to reopen any. The first time open() is called for fd[0] it returns 4 usually. then the 2nd time it gets called it returns 0, and when open() is called again for fd[1] in the very next line, it returns 4 (usually).
Its just the exact same code being executed twice. All connections are closed before trying to reopen any. The first time open() is called for fd[0] it returns 4 usually. then the 2nd time it gets called it returns 0, and when open() is called again for fd[1] in the very next line, it returns 4 (usually).
Without seeing your code, we have only your word that it is right...when obviously isn't right.
Filehandles get re-used. Opening and closing a file/device and then re-opening the same file or device will not necessarily return the same handle. Unless the return value is negative, it is a valid file handle.
You haven't said what you mean by 'die'. Is there more to the error condition?
--- rod.
Filehandles get re-used. Opening and closing a file/device and then re-opening the same file or device will not necessarily return the same handle. Unless the return value is negative, it is a valid file handle.
You haven't said what you mean by 'die'. Is there more to the error condition?
--- rod.
I just meant that it won't work. I realize that it won't necessarily return the same handle, I was just giving an example. Now you are saying that a negative return value is an error condition, which is what I would assume. but if I connect to a serial port that is not in use (nothing connected) it will return 0. Now in the past when I have written to 0, it goes to standard output. So I assumed 0 was a special file descriptor that was always open and reserved for standard IO. I could of course, be totally wrong and that isn't the correct way to write to standard output. Anyway, so anyway, with that in mind I may have figured this whole thing out, heres my theory, tell me if it makes sense:
If only ports 1 and 2 are in use
Theory (in c-like pseudo-code):
Code:
fd[0] = open("/dev/ttyS0") //will return 4 since it is the 1st available
fd[1] = open("/dev/ttyS1") //returns 5 since thats the next available
fd[2] = open("/dev/ttyS2") //returns 0 since nothing is connected
fd[3] = open("/dev/ttyS3") //also returns 0
//-----IO operations here-------
close(fd[0]) //closes descriptor 4 making it available again
close(fd[1]) //closes descriptor 5 making it available again
close(fd[2]) //closes descriptor 0 making it available again!!!!
close(fd[3]) //probably fails because its already closed. I should check
//Now the next time open is called, the first available file descriptor will be 0 since I closed it.
That makes sense to me and would explain my problem perfectly. If there is any reason this can't be, please tell me.
I added some code to check each fd before closing it. If it is equal to zero it will not get closed. Now the whole thing works as I would expect it to.
If someone can confirm this or if no one can explain otherwise, then this thread is solved.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.