Setup Serial Port

I need to set a serial port to 9600 7E1. How do I accomplish this?

I've tried every combination, with no luck.

man stty is a good default starting point...
But is dependent on platform OS...

wisecracker -

Thanks. Looked at it. Tried every combination I can think of, with no luck. Google - no luck.

stty   -evenp cs7 -F /dev/ttyS0

stty  9600 cs7 -F /dev/ttyS0

and various combinations of the above.
No Luck.

In what way does it "not work"? How does it behave?

I open the serial port and then send/receive data. The port is never configured and the program hangs. If minicom is running, the port is configured and the program runs.

Just like the last thread we spent weeks hashing this over in, the issue is probably timeouts.

Have you tried stealing minicomm's settings and using them, just like we did in the last thread we spent weeks hashing this over in?

OK
Here is what works.

It passes the tests of loss of power and loss of serial connection, and is not depedent on minicom.

/*
*Scale7  Write data to Serial Port and Read it
*
*/ 

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stropts.h>
#include <asm/termios.h>
#include <unistd.h>
#include <string.h>

#define MODEMDEVICE "/dev/ttyS0"


int main(void) 
{
    char buf[8];
    int pos=0, num, num2,num3;
    FILE *fp1, *fp2;   
    int i, n3;

    struct termios options;


    speed_t baud = B9600; /* baud rate */

    

    cfsetospeed(&options, baud); /* baud rate */


    options.c_cflag |= PARENB;  // Allow parity to be set
    options.c_cflag &= ~PARODD; // Even parity
    options.c_cflag &= ~CSTOPB; // 1 stop bit 
    options.c_cflag &= ~CSIZE;  // Mask the character size bits 
    options.c_cflag |= CS7;     // 7 bits
    tcflush(fp1, TCOFLUSH);
    
    system("touch /tmp/testfile.txt");  


    system("chmod 666 /tmp/testfile.txt");  //ensure that the permissions are 666
    
    fp1 = fopen(MODEMDEVICE, "w+");
    fp2 = fopen("/tmp/testfile.txt", "w+");

    if(fp2 == NULL)
    {
        printf("testfile error. \n");
            return 1;
    }

    
    if(fp1 == NULL)
    {
        printf("initiation error. \n");
            return 1;
    }

    tcgetattr(fp1, &options); //The tcgetattr function fills the termios structure 
    //you provide with the current serial port configuration. 
    //After we set the baud rates and enable local mode and serial data receipt, 
    //we select the new configuration using tcsetattr. The TCSANOW constant specifies 
    //that all changes should occur immediately without waiting for output data to finish 
    //sending or input data to finish receiving. There are other constants 
    //to wait for input and output to finish or to flush the input and output buffers.
    
    tcsetattr(fp1, TCSANOW, &options); /* apply the settings */
    
    
//printf("scale7");
    fprintf(fp1, "%s","W\r\n");
    fread(buf, 1, 7,fp1 );
    fprintf(stdout,"%s", buf);
//printf("\n%s", buf);

    fclose(fp1);
    fwrite(buf, 1, 7,fp2 );
    
    fclose(fp2);

    system("/usr/bin/php /var/www/testscale2.php"); // call a PHP script

}

Thanks to all