LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2009, 10:22 AM   #1
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Rep: Reputation: 29
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.
Code:
fd[0]=connect("/dev/ttyS0");
fd[1]=connect("/dev/ttyS1");
fd[2]=connect("/dev/ttyS2");
fd[3]=connect("/dev/ttyS3");

//do stuff

close(fd[0]);
close(fd[1]);
close(fd[2]);
close(fd[3]);
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?
 
Old 09-22-2009, 10:42 AM   #2
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Are you sure you want to connect()? Why not just open() ?

Connect() is for sockets; open() is for files, and everything in /dev is like a file.
 
Old 09-23-2009, 12:19 AM   #3
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by jiml8 View Post
Are you sure you want to connect()? Why not just open() ?

Connect() is for sockets; open() is for files, and everything in /dev is like a file.
Alright, let me clarify. Connect(char*) is one of my custom functions, it just calls open and returns a file descriptor.
Apologies for the confusion.
 
Old 09-23-2009, 12:22 AM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
In that event, you have not provided nearly enough code to allow anyone to help you.
 
Old 09-23-2009, 10:17 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
The second time it is executed, fd[0] will die
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.
 
Old 09-23-2009, 11:47 PM   #6
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by theNbomr View Post
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).
 
Old 09-23-2009, 11:51 PM   #7
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
but 0 is a valid file handle
 
Old 09-24-2009, 01:01 AM   #8
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by prushik View Post
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.

If you won't post it, no one can help.
 
Old 09-24-2009, 09:31 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 09-25-2009, 12:22 AM   #10
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
Quote:
Originally Posted by theNbomr View Post
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.
 
Old 09-25-2009, 09:17 AM   #11
prushik
Member
 
Registered: Mar 2009
Location: Pennsylvania
Distribution: gentoo
Posts: 372

Original Poster
Rep: Reputation: 29
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
User session opened and closed msg in /var/log/messages andiramesh Linux - Newbie 9 09-05-2008 05:47 AM
should port 6000 be opened? hottdogg Slackware 9 06-01-2007 01:05 PM
Port Scan: Closed Port instead of Stealth unihiekka Linux - Security 9 12-26-2005 08:51 PM
tftp - "Destination Unreachable" due to "Port Unreachable" renjithgopal Linux - Security 5 07-24-2003 10:36 AM
how is a tcp port opened? Kayaker Linux - Security 7 05-12-2003 12:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration