![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| trying to write a script to loop through a port info file | rcon1 | Shell Programming and Scripting | 5 | 01-09-2008 06:59 AM |
| disabled telnet now need port 23 or port 22 | panzerkw | SUN Solaris | 3 | 03-05-2007 03:08 PM |
| What application is using the port | umen | UNIX for Dummies Questions & Answers | 7 | 05-23-2006 12:30 AM |
| How can I check what port addresses used the application | eykyn17 | IP Networking | 1 | 02-16-2006 12:58 PM |
| which port to write my server application? | rraajjiibb | UNIX for Advanced & Expert Users | 0 | 12-19-2005 02:42 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
how to write application for 32 com port
Dear Sir,
i m going to use NP5610-16 moxa device for multiport serial communication. i m using fedora-core 6 o.s. after installation it will detect serial ports as /dev/ttyr0,/dev/ttyr1...ttyr32. there are total 32 com ports. now i want to write application which monitor all serial ports and received data from particular serial ports. after that it send data on particular port. its ok i understand this concept. but i confuse that what programming approach i have to use to monitor multiport serial ports. i have to fire 32 diff threads for each port or what tell me how i write application such that it is easy to control all 32 com port plz help me to sort out this issue please also guide me that which is the best approach i have to use for this Regards, Amit |
|
||||
|
There are three basic options.
1. Write a single application and run 32 copies of it (this is the same way getty works) 2. Write a single-threaded application and use 'select' to determine which port is ready to read or write from. 3. Write a multi-threaded application and use either a thread per port, or two threads per port (one for reading, one for writing). I would recommend option 1 as it's the easiest to write and test. Option 2 is a more portable solution than 3 as you can avoid multithreading race conditions and synchronisation issues. |
|
||||
|
Quote:
now in my application , data may come simultaneously in any port. i mean data may come on more than one ports at a time and as soon as data come i m going to extract the packet and send that packet to another port. i think option 3 is better for me. but i m confuse here that if i use two thread per port then there are 32 ports and i need to fire 64 threads. is it good solution. some one tell me that if u use 64 threads then your application become hand please give me correct direction because its very important to make a correct design before writing the code. Regards, Amit |
|
||||
|
One technique is to use a thread to read and a thread to write, however what consider what is the purpose of a thread to write when it will be idle 99% of the time. You could have a thread per port which still uses a select().
Personally I would do the single select statement and no threads as this is the most portable solution, select() will tell you when each port needs to be read, and you would manage your own buffers. You would hook in a select() for the write mask only when you have data queued to write out. |
|
||||
|
how to use select() function for 32 com ports
Quote:
can u help me how i can write single select function for all 32 com ports. i mean if i have single thread and i m use select() for all 32 com port then how i write select function to watch multiple fds. Regards, Amit |
|
||||
|
The technique I normally use is to have a list of structs with a file-descriptor and a bitmask
Code:
struct serial_port
{
struct serial_port *next
int fd;
int flags;
.....
};
.....
fd_set fdr,fdw,fde;
int n=-1;
FD_ZERO(&fdr); FD_ZERO(&fdw); FD_ZERO(&fde);
struct serial_port *p=list;
while (p) {
if (p->flags & 1) { FD_SET(p->fd,&fdr); n=max(n,p->fd); }
if (p->flags & 2) { FD_SET(p->fd,&fdw); n=max(n,p->fd); }
if (p->flags & 4) { FD_SET(p->fd,&fde); n=max(n,p->fd); }
p=p->next;
}
...
n=select(n+1,&fdr,&fdw,&fde,NULL);
...
p=list;
while (p)
{
if ((p->flags & 1)&&FD_ISSET(p->fd,&fdr)) .... do read ...
if ((p->flags & 2)&&FD_ISSET(p->fd,&fdw)) .... do write ...
if ((p->flags & 4)&&FD_ISSET(p->fd,&fde)) .... do except ...
p=p->next;
}
|
![]() |
| Bookmarks |
| Tags |
| linux |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|