troubleshooting Question

C command line parsing

Avatar of sargent240
sargent240Flag for United States of America asked on
LinuxProgrammingC
19 Comments1 Solution423 ViewsLast Modified:
I am attaching my code and I would like to take the command line argument and put it into buff.  I would like an example on how to get that done.  It seems to me it should not take to many lines of code to do it.  I need to change the line that assigns "Now is the time for all good men" to buff so it will have the info from the command line argument.  Do I just need to change the line in the for statement to assign the command line argument to buff rather than just print it?  Thanks.


    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <termios.h>
    int main(int argc, char *argv[])
    {
    int fd;
    int wd=0;
   
    speed_t baud = B9600; /* baud rate */  

    //unsigned char buff[] = { 0x02, 0xFA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x01, 0x21, 0x03};
    unsigned char buff[] = "Now is the time for all good men\n\r";

    int x;
    for (x=0; x<argc; x++)
      printf("%s\n",argv[x]);
   
    fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
    if (fd == -1)
    {     fprintf(stderr, "open_port: Unable to open /dev/ttyS0 - %s\n",
                            strerror(errno));
    }
    else
    {
     
      struct termios settings;
      tcgetattr(fd, &settings);

      cfsetospeed(&settings, baud); /* baud rate */
      settings.c_cflag &= ~PARENB; /* no parity */
      settings.c_cflag &= ~CSTOPB; /* 1 stop bit */
      settings.c_cflag &= ~CSIZE;
      settings.c_cflag |= CS8 | CLOCAL; /* 8 bits */
      settings.c_lflag = ICANON; /* canonical mode */
      settings.c_oflag &= ~OPOST; /* raw output */

      tcsetattr(fd, TCSANOW, &settings); /* apply the settings */
      tcflush(fd, TCOFLUSH);      
     
     
      printf("%d", fd); //i get fd value to be 3
      fcntl(fd, F_SETFL, 0);
      wd=write(fd, buff, strlen(buff));
    }
      printf("%d", wd); //i get wd to be -1, so what is the problem??
      close(fd);
      return 61;
    }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 19 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 19 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros