Problem with string and serial port

This is a discussion on Problem with string and serial port within the C Programming forums, part of the General Programming Boards category; hi in c programming (linux), i search a way to read the setting of the serial port (speed, parity...) and ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    5

    Problem with string and serial port

    hi

    in c programming (linux), i search a way to read the setting of the serial port (speed, parity...) and change it.

    i need also to write to a led display (alpha sign communications protocole)

    with bash i do:

    Code:
    echo -e \\000\\000\\000\\000\\000\\001Z00\\002AA Triplex\\004 >/dev/ttyS2
    my source code

    Code:
    #include <sys/ioctl.h>
    #include <stdio.h>
    #include <stdlib.h>                     /*atoi, system, getenv */
    #include <unistd.h>                     /*sleep, close */
    #include <dirent.h>                     /*opendir, readdir, closedir */
    #include <string.h>
    #include <fcntl.h>                      /*open */
    #include <sys/stat.h>
    #include <sys/errno.h>  
    
    char *ledisplay;
    
    /* Opening port one for read and write */
    int writeopen(char msg[])
    {
        int fd1;
        int wr;
        fd1 = open(ledisplay, O_RDWR | O_NOCTTY | O_NDELAY );
        if (fd1 == -1)
        {
            fprintf(stderr, " %s open_port: Unable to open %s\n", strerror(errno), ledisplay);
        }
        else
        {
            fcntl(fd1, F_SETFL, 0);
            printf(" Port 1 has been sucessfully opened and %d is the file descriptor\n",fd1);
            
            printf("sizeof %d\n", sizeof(msg));
            
            wr=write(fd1, msg, sizeof(msg));
            printf(" Bytes sent are %d \n",wr);
            if (wr < 0)
                fputs("write() of n bytes failed!\n", stderr);
            else
                printf(" Stuff has been written into port 1\n");
            close(fd1);
        }
        return (fd1);
    } 
    
    int readopen()
    {
        int fd1;
        int rd;
        char *buff=NULL;
        fd1 = open(ledisplay, O_RDONLY | O_NOCTTY | O_NDELAY );
        if (fd1 == -1)
        {
            fprintf(stderr, "%s open_port: Unable to open %s\n", strerror(errno), ledisplay); 
        }
        else
        {
            fcntl(fd1, F_SETFL, 0);
            printf(" Port 1 has been sucessfully opened and %d is the file descriptor\n",fd1);
            rd=read(fd1, buff, 100);
            printf(" Bytes recieved are %d \n",rd);
            printf("%s\n",buff);
            close(fd1);
        }
        return (fd1);
    } 
    
    int main()
    {
        int result;
        
        //echo -e \\000\\000\\000\\000\000\\001Z00\\002AA TRIPLEX \\004 >/dev/ttyS2
        char msg[] = {'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0', '\2', 'A', 'A', ' ', 'T', 'R', 'I', 'P', 'L', 'E', 'X', ' ', '\4' };
      
        if((result=(readRtuConfig()))!=-1)
        {
        
            printf("port sÚrie utilisÚ: %s\n", ledisplay);
            printf("msg = %s\n",msg);
    
             if((result=(writeopen(msg)))!=-1)
             {
                printf("ok\n");
             }
        }
        return 1;
    }
    when i run this program i get

    Code:
    Port 1 has been sucessfully opened and 3 is the file descriptor
    Bytes sent are 4
    Stuff has been written into port 1
    ok
    that seem ok but on the led display, i see nothing...

    surely,
    char msg[] = {'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0', '\2', 'A', 'A', ' ', 'T', 'R', 'I', 'P', 'L', 'E', 'X', '\4' };

    is not equivalent to this bash command

    echo -e \\000\\000\\000\\000\\000\\001Z00\\002AA Triplex\\004 >/dev/ttyS2

    any idea?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Bytes sent are 4
    Notice you're only writing out 4 bytes to your port, even though your message is much longer. Once you pass an array to a function, all you get is the size of a pointer. You need to pass the size of the array to the function:
    Code:
             if((result=(writeopen(msg, sizeof(msg))))!=-1)
    Or you could wrap the array in a struct.

    I don't know bash, so I can't tell you whether msg matches what you are sending using bash. If you can tell us what bash actually sends, then we can tell you what msg should be.

  3. #3
    SleepWalker tjohnsson's Avatar
    Join Date
    Apr 2004
    Posts
    70
    There's tcgetattr (...) what you can use to get serial configuration...
    "man termios" gives you more information about it and there's
    Serial-Programming-HOWTO in tldp.org...

    If your schedule is busy enough, hit "setserial" on your console,
    read quick info and do what you need to.
    -- Add Your Signature Here --

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Send image (.jpg) file over serial port
    By aSpareName in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-30-2009, 09:35 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  3. Reading and writing to a serial port
    By SwarfEye in forum C Programming
    Replies: 2
    Last Post: 08-18-2006, 12:28 AM
  4. Problem of serial programming
    By fingerling54 in forum Linux Programming
    Replies: 5
    Last Post: 05-11-2005, 11:05 AM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 03:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21