C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 07-24-2007, 11:05 PM   #1
Registered User
 
Join Date: May 2007
Posts: 5
how to write multiports serial application

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 .....etc
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 16 diff threads for each port or what
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
pansuriya.amit is offline   Reply With Quote
Old 07-24-2007, 11:59 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
Using threads is one way, providing each thread only accesses a single port, and has only thread-local data.

But if there is any shared data, say reading from one port and writing to another, then threads will be harmful to your code. Debugging massively threaded code which doesn't have a solid design and poor use of say semaphores to protect access to shared data will be a nightmare.

http://www.linuxmanpages.com/man2/select_tut.2.php
http://www.linuxmanpages.com/man2/select.2.php
The select() system call offers a way to monitor many file descriptors at the same time.
Essentially, when something interesting happens, select() returns, and then you can deal with it.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 07-25-2007, 03:11 AM   #3
Registered User
 
Join Date: May 2007
Posts: 5
i want to transfer file over serial port from my c++ or c Application

Quote:
Originally Posted by Salem View Post
Using threads is one way, providing each thread only accesses a single port, and has only thread-local data.

But if there is any shared data, say reading from one port and writing to another, then threads will be harmful to your code. Debugging massively threaded code which doesn't have a solid design and poor use of say semaphores to protect access to shared data will be a nightmare.

http://www.linuxmanpages.com/man2/select_tut.2.php
http://www.linuxmanpages.com/man2/select.2.php
The select() system call offers a way to monitor many file descriptors at the same time.
Essentially, when something interesting happens, select() returns, and then you can deal with it.
Sir,
i m going to use 32 com port.
if i create two thread for each serial port(one thread for read and one for write), then there are total 64 threads are required.
is it good design .
actually there is one server pc and i use 32 port serial device.
now i want to read data from particular portnumber after that server decide on what port number i have to transfer data
plz suggest me whats the best design for multiport serial communication
amit
pansuriya.amit is offline   Reply With Quote
Old 07-25-2007, 03:50 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
I wouldn't regard so many threads as being good design, no.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 07-25-2007, 07:29 AM   #5
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
What is a good design in one case, is quite often a bad design in another situation - and the right answer is always "what works well for what you want to do". I agree with Salem, that is' unlikely that having 64 threads is a good design - it MAY be if all ports are almost always used, then that's a good way to do it. But not if the common case is that most ports are "not in use".

So, you have 32 serial ports, one of which is sending stuff to your application, to then send something out on another port.

I would think that you could create a thread (or pair of threads if that's a better fit) based on the request to send data. That way you don't have several dozen threads "laying about doing nothing". When the data transfer is finished, the thread kills itself (or sends a message back to the controller thread to "ask to be killed").

If you don't know which port data is coming in, use select() to listen to several serial ports at once, and then create threads as necessary.

The "create thread as necessary" assumes that you're actually sending data for some time on the port choosen, rather than a few bytes - if it's a few bytes, it's probably better to just do all of it in a single thread.

--
Mats
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Serial Port Questions valaris Tech Board 2 05-22-2009 08:26 AM
Detect SCSI Hard Drive Serial Number mercury529 Windows Programming 3 10-17-2006 06:23 PM
Reading and writing to a serial port SwarfEye C Programming 2 08-18-2006 12:28 AM
PC104 Serial Port Programming Problem outerspace C Programming 6 11-09-2005 07:07 PM
Some humour... Stan100 A Brief History of Cprogramming.com 8 11-06-2003 10:25 PM


All times are GMT -6. The time now is 02:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22