Sponsored Content
Top Forums Programming Problem regarding serial port and multithreading Post 302145298 by porter on Tuesday 13th of November 2007 04:46:01 PM
Old 11-13-2007
Quote:
Originally Posted by arunchaudhary19
and using usleep to free the cpu and da poll after some time..
Not sure what you mean by usleep. If you are using select or poll the thread will block until something is ready, (use a timeout pointer NULL).

I basically use a loop with the file-descriptor to read and write from, and another pipe() created pair.

Then the read set is the fd and the read end of the pipe.
THe write set is either emtpy, or if have data to send, put in the fd.

Code:
while (1)
{
    fd_set fdr,fdw;
    FD_ZERO(&fdr);
    FD_ZERO(&fdw);
    FD_SET(fd,&fdr);
    FD_SET(pipe_read,&fdr);
    if (dataToWrite) FD_SET(fd,&fdw);

    if (select(1+max(fd,pipe_read),&fdr,&fdw,NULL,NULL)>0)
    {
         if (FD_ISSET(pipe_read,&fdr)) read(pipe_read,buf,1);
         if (FD_ISSET(fd,&fdr)) { ... }
         if (FD_ISSET(fd,&fdw)) { ... }
    }
}

Then if need to write to serial port set the dataToWrite flag, write a byte to the pipe write end, that will unblock the select, it will loop round and reselect the fd for writing,
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Serial port communication

We're running SCO Unix Openserver 5.05 and I'm having trouble with serial communication between the com2 serial port and a handheld device. Downloading data from Unix to the handheld works perfectly, but the other way around creates a major problem. I don't know whether it's a buffer overflow or... (1 Reply)
Discussion started by: Aretha
1 Replies

2. Programming

problem while having a communication with serial port?????

hello, I am gettin problem while sending and recieving data through seial port... when I am sending Data then the reciever end is not able to recieve that data ..... Reciever end is running in infinite loop just polling after some time to check that there is data on the port and then again... (9 Replies)
Discussion started by: arunchaudhary19
9 Replies

3. Programming

Serial port programming

I am developing an application in c with Linux OS, where a radio modem working at baud rate 9600 will be attached to PC on serial port. More than four such units will be communicating at one time, so there may be jamming or data corruption. Each module will be transmitting Data packets less than... (2 Replies)
Discussion started by: raj8109
2 Replies

4. UNIX for Dummies Questions & Answers

problem with serial port BSD

Hello all , i have change my system debian linux to freebsd( pcBSD) all working as well , but the serial does not work correctly. Under linux the problem does not appears. my problem is than my serial port does work . the first step than i have make is look with dmesg if my serial are... (0 Replies)
Discussion started by: pitbac23
0 Replies

5. Solaris

Serial port problem

I am working with solaris 9 sparc and I want to connect physical device in serial port but when I am connecting it,It is showing the error window saying-- So can any1 tell me the reason or is there any package I have to add to work with serial port??? (2 Replies)
Discussion started by: smartgupta
2 Replies

6. Programming

problem in coding for GSM interfacing using serial port

i am having some coding problem in c for interfacing the GSM module through serial port. i want to send/receive sms. i have done all the setting for the port and know the AT command to use in this but i am actually having problem in reading and writing from serial port how to handle the signal when... (1 Reply)
Discussion started by: harsh_it
1 Replies

7. Shell Programming and Scripting

Need help with serial port

Hi, I have a external board connected to my serial port. I need to execute "shutdown -r now" command when system boot up. When system boots up it requires a username ans password. Then I need to run my command. I can use rc script but that is rebooting system before it asks for username and... (0 Replies)
Discussion started by: charlie.arya
0 Replies

8. Solaris

How to enable Serial port on ILOM, when Network Port is enabled in parallel

Hi Everyone, In my environment, I have few T5220. On the iLOM Management Card, I have both Network and Serial port are cabled, I don't have any issues while I try to connect using Network Management port, but when I try to connect the serial port for the same server which is actually connected... (3 Replies)
Discussion started by: bobby320
3 Replies

9. SCO

Problem finding what is using a serial port

How can I determine what process is currently using a serial port? A good bit of google searching hasn't turned up anything useful, but it seems like there has to be a way to do this without too much difficulty. When I first started looking into this problem, I assumed that when a port was in... (2 Replies)
Discussion started by: jdsnatl
2 Replies

10. Solaris

Cabling and adapters to communicate to service processor serial port from Windows PC with USB port.

Hello, I have an unloaded T5140 machine and want to access the ILOM for the first time and subsequently the network port after that., and then load Solaris 10 the final January 2011 build. The first part is what confuses me -the cabling. I am coming from a Windows machine (w/appropriate... (5 Replies)
Discussion started by: joboy
5 Replies
SELECT(2)						      BSD System Calls Manual							 SELECT(2)

NAME
select -- synchronous I/O multiplexing LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <sys/select.h> int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); FD_SET(fd, &fdset); FD_CLR(fd, &fdset); FD_ISSET(fd, &fdset); FD_ZERO(&fdset); DESCRIPTION
The select() system call examines the I/O descriptor sets whose addresses are passed in readfds, writefds, and exceptfds to see if some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively. The only exceptional condition detectable is out-of-band data received on a socket. The first nfds descriptors are checked in each set; i.e., the descriptors from 0 through nfds-1 in the descriptor sets are examined. On return, select() replaces the given descriptor sets with subsets consisting of those descriptors that are ready for the requested operation. The select() system call returns the total number of ready descriptors in all the sets. The descriptor sets are stored as bit fields in arrays of integers. The following macros are provided for manipulating such descriptor sets: FD_ZERO(&fdset) initializes a descriptor set fdset to the null set. FD_SET(fd, &fdset) includes a particular descriptor fd in fdset. FD_CLR(fd, &fdset) removes fd from fdset. FD_ISSET(fd, &fdset) is non-zero if fd is a member of fdset, zero otherwise. The behavior of these macros is undefined if a descriptor value is less than zero or greater than or equal to FD_SETSIZE, which is normally at least equal to the maximum number of descriptors supported by the system. If timeout is not a null pointer, it specifies the maximum interval to wait for the selection to complete. System activity can lengthen the interval by an indeterminate amount. If timeout is a null pointer, the select blocks indefinitely. To effect a poll, the timeout argument should not be a null pointer, but it should point to a zero-valued timeval structure. Any of readfds, writefds, and exceptfds may be given as null pointers if no descriptors are of interest. RETURN VALUES
The select() system call returns the number of ready descriptors that are contained in the descriptor sets, or -1 if an error occurred. If the time limit expires, select() returns 0. If select() returns with an error, including one due to an interrupted system call, the descrip- tor sets will be unmodified. ERRORS
An error return from select() indicates: [EBADF] One of the descriptor sets specified an invalid descriptor. [EFAULT] One of the arguments readfds, writefds, exceptfds, or timeout points to an invalid address. [EINTR] A signal was delivered before the time limit expired and before any of the selected events occurred. [EINVAL] The specified time limit is invalid. One of its components is negative or too large. [EINVAL] The nfds argument was invalid. SEE ALSO
accept(2), connect(2), getdtablesize(2), gettimeofday(2), kqueue(2), poll(2), read(2), recv(2), send(2), write(2), clocks(7) NOTES
The default size of FD_SETSIZE is currently 1024. In order to accommodate programs which might potentially use a larger number of open files with select(), it is possible to increase this size by having the program define FD_SETSIZE before the inclusion of any header which includes <sys/types.h>. If nfds is greater than the number of open files, select() is not guaranteed to examine the unused file descriptors. For historical reasons, select() will always examine the first 256 descriptors. STANDARDS
The select() system call and FD_CLR(), FD_ISSET(), FD_SET(), and FD_ZERO() macros conform with IEEE Std 1003.1-2001 (``POSIX.1''). HISTORY
The select() system call appeared in 4.2BSD. BUGS
Version 2 of the Single UNIX Specification (``SUSv2'') allows systems to modify the original timeout in place. Thus, it is unwise to assume that the timeout value will be unmodified by the select() system call. FreeBSD does not modify the return value, which can cause problems for applications ported from other systems. BSD
November 17, 2002 BSD
All times are GMT -4. The time now is 11:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy