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 06-02-2006, 02:52 PM   #1
kad_79
LQ Newbie
 
Registered: Jun 2006
Posts: 10

Rep: Reputation: 0
write() not working?


Hello all,

I have a small program which should be writing on to the serial port. I send a 'space' character and the device connected to the serial port returns '>' as an acknowledgement.

With the same setting(serial port settings of my program) if i try to send a space character with another readymade program i get an ackowledgement without any issue.

But somehow my writes are not reaching the device i believe. Is there anything that i need to flush?
I have put a sniffer(portmon) and watching whats happening and I see that it says 1 character written successfully, although this does not guarantee that it has reached the device properly because, even if the serial port cable is dangling without any connection to the extenral device, i get a message saying the write was successful.

Below is my program. Could anyone please let me know what am I doing wrong here?

#include<unistd.h>
#include<fcntl.h>
int main(void)
{
int fd=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd,F_SETFL,0);
unsigned char sim = 0x20;
write(fd,&sim,1);
char *bfr=new char[255];
read(fd,bfr,1);
}
As the write has not yet reached the device, the read continuosly blocks!!!
Any clue on whats hapening.
I am trying this on cygwin. But i tried this on red hat linux enterprise 4, with the same result. The strange part is, when i started out to write this program, it was working perfectly fine on cygwin, with data being sent and recvd, but when i started adding extra code to it(like setting serial attributes etc), at some point it broke down and its not recovering

Please help!
 
Old 06-02-2006, 03:48 PM   #2
kad_79
LQ Newbie
 
Registered: Jun 2006
Posts: 10

Original Poster
Rep: Reputation: 0
i am stuck with this from the past 2days. any pointers would be of great help. even if its a weblink i am ready to read and implement

thanks again
 
Old 06-02-2006, 11:25 PM   #3
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Try d/l my serial utils .c and .h file. Its too large to post here.

Last edited by randyding; 03-19-2009 at 10:47 PM. Reason: web site is now gone, removed url
 
Old 06-03-2006, 03:25 AM   #4
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
can you not start commenting out you modifications until you get to the point where it starts working again,then go forward making small change at a time trying to pin point which line of new code breaks it.
Sorry only thing I can sugest
 
Old 06-03-2006, 02:55 PM   #5
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Code:
#include<unistd.h>
#include<fcntl.h>
int main(void)
{
int fd=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd,F_SETFL,0);
unsigned char sim = 0x20;
write(fd,&sim,1);
char *bfr=new char[255];
read(fd,bfr,1);
}
Ok 2 problems here in this code

1. you have no return statement use int for main with return 0;
2. You are declareing the char incorrectly.

char *bfr=new char[255];

This doesent compile with gcc under linux.

you should define all of your variables before making function calls. But that is not whats wrong with the char. it should be in 2 parts.

char *bfr;
char new[255];
 
Old 06-04-2006, 06:07 PM   #6
kad_79
LQ Newbie
 
Registered: Jun 2006
Posts: 10

Original Poster
Rep: Reputation: 0
hey exvor..
thanks for the response...but this is c++, sorry i didnt tell u that before..
1) return statement is not necessary. cuz it compiled without issues.
2) dynamic allocation.

slzckboy

i tried dat. no use. it still fails

randyding

i will take a peek at it.
 
Old 06-05-2006, 05:39 PM   #7
kad_79
LQ Newbie
 
Registered: Jun 2006
Posts: 10

Original Poster
Rep: Reputation: 0
any help..anyone. i am still stuck
 
Old 06-06-2006, 09:53 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Test the return values of functions like 'open' and 'write' and print them, so you can see what happens.

Compiling without issues probably means that you don't have all warnings on.

And the fact that you can't go back more-than-likely means that your HW is in an undefined state somewhere.

If of interest, I posted a setup function for RS232 in this thread
 
Old 06-06-2006, 07:01 PM   #9
kad_79
LQ Newbie
 
Registered: Jun 2006
Posts: 10

Original Poster
Rep: Reputation: 0
I felt the urge to respond back
There was nothing wrong with the program that I wrote to begin with. It seems the serial device that I am connecting to needs a wait period of 1.6milliseconds before it starts responding to my characters. I did not give a breathing 'space' to the poor guy.
Now things are fine. Thanks a lot for all your help.

Cheers,
Kiran
 
Old 06-09-2006, 08:46 AM   #10
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Quote:
Originally Posted by kad_79
hey exvor..
thanks for the response...but this is c++, sorry i didnt tell u that before..
1) return statement is not necessary. cuz it compiled without issues.
2) dynamic allocation.

slzckboy

i tried dat. no use. it still fails

randyding

i will take a peek at it.


Yea i figured it was some C++ crazyness :P but dident know if it was diffrent in that language.
 
  


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
how to write multiple threads working simultaneously ? indian Programming 3 09-19-2011 11:41 PM
NTFS Write support not working in Laptops WhiteDevel Slackware 13 05-10-2006 10:40 AM
10.1 Alpha / Betas NTFS write working 1kyle SUSE / openSUSE 3 03-21-2006 08:16 AM
Write Access on Samba Server not working urzumph Linux - Networking 2 03-04-2004 05:03 AM
inability to write a working chat script.... Slestak Linux - Networking 8 05-30-2002 01:26 AM

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

All times are GMT -5. The time now is 08:48 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