Re: proprieties of termios are not saved!!

2012-08-03 Thread Bob Proulx
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
device.

> Compteur# ./oo
> Old cflag=1cb2
> New cflag=0cbd
> 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=1cb2 // value here should be 0cbd
> New cflag=0cbd

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

> #include 
> #include 
> #include 
> #include 
> #include 
> #include 

I see you including  but I don't see why.  I think you
should avoid  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:

  perror("open");

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

  exit(1);

or using the macro

  exit(EXIT_FAILURE);

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

  http://pubs.opengroup.org/onlinepubs/009695399/functions/exit.html

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

Bob


signature.asc
Description: Digital signature


proprieties of termios are not saved!!

2012-08-03 Thread abdelkader belahcene
Hi,

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 
here is the program   and the 2 execution


#include 
#include 
#include 
#include 
#include 
#include 
int main(){
  int fd=0;
  fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
  if (fd < 0) {  printf("open error ");  exit(-1); }
  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);
}

Compteur# ./oo
Old cflag=1cb2
New cflag=0cbd
Compteur#
Compteur# ./oo
Old cflag=1cb2 // value here should be 0cbd
New cflag=0cbd

thanks for help
regards