Problem regarding serial port and multithreading


 
Thread Tools Search this Thread
Top Forums Programming Problem regarding serial port and multithreading
# 1  
Old 11-12-2007
Problem regarding serial port and multithreading

hello,

I am creating a application in which I first open the serial port and then create the thread..for reading the data comming through that serial port. from that same thread I create another thread that is write thread means for writing on the serial port...
I am continously polling again and again to check the data comming through that thread
the read thread is working correctly but due to write thread I am getting some different bahavior of the code...sometime segmentation fault...sometimes unwanted result etc....

can any body guide me where I am wrong...why unwanted bahavior of the code....
can Anybody guide me how many threads I can create in a program and where to create Should I use any precuations while writing multithreaded application....
# 2  
Old 11-12-2007
The most reliable way to used a file descriptor is not to have some functions handled in different threads, but use the same thread to do all IO for a file descriptor, use select or poll and non-blocking IO just like in a single threaded application.

If you take the multithreaded approach having one thread to read, and one to write, how do you stop the reading, how do you close the file descriptor?

And yes, if writing multithreaded code you must take precautions, eg mutexs, condition variables as appropriate.
# 3  
Old 11-12-2007
As you suggested me to use select or poll ....
Here in my code I am using select to select the file descriptor when there is data to read....
and using usleep to free the cpu and da poll after some time..

moreover I am taking care of locking and unlocking mutexes...
# 4  
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,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question