[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: proprieties of termios are not saved!!

abdelkader belahcene wrote:
> I wrote a program which have to change proprieties
> for the serial port  via the termios commands, but it not saved.
> When I ran the program it gave the old values then after changes
> gave the new, that seems correct,   but when  i ran again i got
> same value,  Normally I had to get the last value,  i.e :  the new value
> of the previous running ????

If I recall correctly the kernel has a built in default for device
attributes.  The kernel resets the device to this internal setting
every time the device is opened for the first time.  After a device is
opened and a file handle pointing to it then it may be modified.  This
modification is maintained for as long as the file handle is open.
When the file handle is closed then the kernel resets the attributes.

Due to this programs that open serial ports typically open and then
set the desired termio attributes.  That is normal.  This setting of
termio attributes is done every time the program runs and opens the

> Compteur# ./oo
> Old cflag=00001cb2
> New cflag=00000cbd
> Compteur#

The kernel notices that the reference count on /dev/ttyS0 has returned
to zero and therefore resets the device attributes.

> Compteur# ./oo
> Old cflag=00001cb2     // value here should be 00000cbd
> New cflag=00000cbd

This second run has no memory of the first run.  This is expected.

> #include <stdio.h>
> #include <unistd.h>
> #include <fcntl.h>
> #include <stdlib.h>
> #include <sys/io.h>
> #include <termios.h>

I see you including <sys/io.h> but I don't see why.  I think you
should avoid <sys/io.h> if you don't specifically need it.  It would
be more portable without it.

> int main(){
>   int fd=0;
>   fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
>   if (fd < 0) {  printf("open error ");

The error message is missing an ending newline.

  printf("open error\n");

You may want to look into using perror(3) or strerror(3) for more
specific error messages.  Such as:


>   exit(-1); }

Using exit(-1) is out of the valid range.  The parent will only
receive the lower eight bits of the value.  Therefore the valid range
for exit is 0-255.  The functional internally does 'status & 0377'
truncating to the lower eight bits.  Most programs by convention use 1
for an error and not 255.  Using -1 to me feels wrong because it
doesn't represent the actual value that will be seen by the parent
when the parent calls wait(2) to get the exit code of the child.

I suggest using either:


or using the macro


Either would be okay.  See the standards docs for more information:


>   struct termios my_termios;
>   tcgetattr(fd, &my_termios);
>   printf("old cflag=%08x\n", my_termios.c_cflag);
>   cfsetospeed(&my_termios, B9600);
>   printf("old cflag=%08x\n", my_termios.c_cflag);
>   exit(0);
> }

A very minor point is that you have defined 'int main()' to return an
integer.  But your program never does.  Personally I use the macro
with exit for failures with "exit(EXIT_FAILURE);" and in main() to
indicate a successful exit I use "return 0;".  Returning a value from
main() is the same as exit()'ing with that value.  It is the same
thing.  But if 'int main()' is going to declare that main returns an
integer then I think main should return an integer.  Otherwise we
would declare 'void main()' to not return anything instead.


Attachment: signature.asc
Description: Digital signature

Reply to: