Help A Fireman new to programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pgfdbug
    New Member
    • Feb 2009
    • 14

    Help A Fireman new to programming

    First I am learning this as I go so please forgive my ignorance, all is self taught. I was given this as program to run an LED signboard for my fire station. The program is supposed to transmit the calls to the signboard so we can see what piece is due to go out the door. The problem is I am getting several warnings and also the program is sending multi-colors instead of just one. Any help is greatly appreciated.

    betabrite.c In function main :
    betabrite.c:88: warning: comparison between pointer and integer
    betabrite.c:88: warning: comparison between pointer and integer
    betabrite.c:93: warning: comparison between pointer and integer
    betabrite.c:94: warning: passing arg 2 of 'write' makes pointer from integer without a cast
    betabrite.c:96: warning: passing arg 2 of 'write' makes pointer from integer without a cast
    betabrite.c:98: warning: comparison between pointer and integer
    betabrite.c:100 : warning: comparison between pointer and integer


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <termios.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    #include "betabrite.h"
    
    #define DEFAULT_SERIAL_PORT "/dev/com1" /* Cygwin */
    
    int OpenPort(char *serial_port) {
    
       int fd ;
    
       fd = open(serial_port, O_RDWR|O_NOCTTY|O_NDELAY) ;
    
       if (fd == -1) {
          perror("OpenPort: Unable to open port - ") ;
       } else {
          fcntl(fd,F_SETFL,0) ;
       } /* if */
       
       return(fd) ;
    
    } /* OpenPort() */
    
    /* SetupSerial() - open the serial port and setup comm parameters */
    
    int SetupSerial(char *serial_port) {
    
       struct termios options ;
    
       int fd = OpenPort(serial_port) ;
    
       tcgetattr(fd,&options) ;
    
       /* 9600 baud */
    
       cfsetispeed(&options,B9600) ;
       cfsetospeed(&options,B9600) ;
    
       options.c_cflag |= (CLOCAL|CREAD) ;
    
       tcsetattr(fd,TCSANOW,&options) ;
    
       /* 7 bits */
    
       options.c_cflag &= ~CSIZE ;
       options.c_cflag |= CS7 ;
    
       /* even parity */
    
       options.c_cflag |= PARENB ;
       options.c_cflag &= ~PARODD ;
       options.c_cflag &= ~CSTOPB ;
       options.c_cflag &= ~CSIZE ;
       options.c_cflag |= CS7 ;
    
       /* software flow */
    
       options.c_iflag |= (IXON | IXOFF | IXANY) ;
    
       options.c_oflag &= ~OPOST;
       options.c_oflag &= ~ONLCR;
       options.c_oflag &= ~OCRNL;
    
       tcsetattr(fd,TCSANOW,&options);
    
       return(fd) ;
    
    } /* SetupSerial() */
    
    int main(int argc, char **argv) {
    	
    	int fd;
    	int n;
    	char *serial_port = DEFAULT_SERIAL_PORT;
    
    	fd = SetupSerial(serial_port) ;
    
    	n = write(fd,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",20) ;
    	n = write(fd,"\001Z00\002",5);
    
    	if(argv[1] == 'a' && argv[2] == 'a')
    		n = write(fd,"A0",2);
    	else {
    		n = write(fd,"A0\x1B ",3);
    
    		if(argv[2] == 'f')
    			n = write(fd,'c',1);
    		else
    			n = write(fd,'a',1);
    
    		if(argv[1] == 'g')
    			n = write(fd,"\x1C\x32",2);
    		else if(argv[1] == 'r')
    			n = write(fd,"\x1C\x31",2);
    		else
    			n = write(fd,"\x1C\x38",2);
    
    		n = write(fd,argv[3],strlen(argv[3]));
    	}
    
    	//n = write(fd,"AA\x1B b",5);
    	//n = write(fd,"\x1C\x32\x13",3);
    
    	n = write(fd,"\004",1);
    	close(fd);
    
    	return(0);
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    The errors for lines 88, 93, 98, and 100 refer to improper use of argv. You need to tell us how you want your program to use command line arguments before we can suggest how to change this.

    The errors for lines 94 and 96 refer to an improper call to the 'write' function. Notice that on line 91 the second argument to 'write' is enclosed in double quotes, but on lines 94 and 96 you enclose the second argument in single quotes. If you intend to write single characters on lines 94 and 96 then simply change the single quotes to double quotes.

    Comment

    • pgfdbug
      New Member
      • Feb 2009
      • 14

      #3
      lines 94 and 96 are used to define how the message is displayed on the board the "a" is flash and "c" is scrolling. Lines 98 and 100 are used to define the colors. To run the program it comes up as ./betabrite g g "message here"

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by pgfdbug
        To run the program it comes up as ./betabrite g g "message here"
        You should always check the value of argc before looking at the argv array. Somebody might have slipped up and left out an expected command line argument.

        The argv array contains pointers to strings; so you have to perform string comparisons rather than single-character comparisons. For example,
        Code:
        if (strcmp(argv[1],"a") == 0)
        instead of
        Code:
        if (argv[1] == 'a')
        Personally, I find strcmp counterintuitiv e for string equality. Usually I define the following macro:
        Code:
        #define streq(s1,s2)    (strcmp(s1,s2) == 0)
        as in
        Code:
        if (streq(argv[1],"a"))

        Comment

        • pgfdbug
          New Member
          • Feb 2009
          • 14

          #5
          Is the line of code you gave me below used to replace all argv instances?

          Code:
          if (strcmp(argv[1],"a") == 0)

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by pgfdbug
            Is the line of code you gave me below used to replace all argv instances?
            Code:
            if (strcmp(argv[1],"a") == 0)
            Well, you need to match the array index and literal string to what you already have; plus line 88 is a Boolean expression involving two string comparisons. But yes, something very much like this example should replace all places where you were comparing an argv to a character constant.

            Comment

            • pgfdbug
              New Member
              • Feb 2009
              • 14

              #7
              I tried to implement the; #define streq(s1,s2) (strcmp(s1,s2) == 0); but i believe Im using it all wrong. It through up more errors than the before. Thank you for the suggestion but it is a little too advanced for my limited knowledge.

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by pgfdbug
                I tried to implement the; #define streq(s1,s2) (strcmp(s1,s2) == 0); but i believe Im using it all wrong. It through up more errors than the before. Thank you for the suggestion but it is a little too advanced for my limited knowledge.
                Remove that trailing semi colon from your macro definition so that streq(x, y) will be replaced by (strcmp(x, y) == 0) by the C preprocessor.

                kind regards,

                Jos

                Comment

                • pgfdbug
                  New Member
                  • Feb 2009
                  • 14

                  #9
                  I tried the strcmp it removed all the errors but I still have the color problem. Lines 98-103 are what define the colors. Instead of one color as the message scrolls it puts out a different color every time.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Are you getting all the letters of your message? Is there any pattern to the colour of the letters?

                    It might help if you could tell us the exact make and model of your LED display sign.

                    Comment

                    • pgfdbug
                      New Member
                      • Feb 2009
                      • 14

                      #11
                      Its a betabrite LED 213C-1 is model. There is no pattern to the colors and I get the whole message with a 2 on the front of it. Below is a link to programming info.

                      Comment

                      • pgfdbug
                        New Member
                        • Feb 2009
                        • 14

                        #12
                        sorry its a 1040 model

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          OK, from the document you posted pages 15 - 16 then here

                          Code:
                                   n = write(fd,"A0\x1B ",3);
                            
                                   if(argv[2] == 'f')
                                       n = write(fd,'c',1);
                                   else
                                       n = write(fd,'a',1);
                          you have missed out the Display Position character that should come between the 0x1B and the Mode Code ('c' or 'a').

                          You are getting a 2 because 0x32 is the character '2', I don't know why you are getting multi-colours but the malformed message should be fixed first.


                          Finally on a separate note if argv[1] == "a" and argv[2] == "a" you don't output the text because you have the } in the wrong place.

                          Comment

                          • pgfdbug
                            New Member
                            • Feb 2009
                            • 14

                            #14
                            Thank you all for all your help it turns out it was the position code that messed it all up. It took me a year but its all fixed. If any of you have some time and want to give me some more help with some bugs let me know.

                            Comment

                            • Banfa
                              Recognized Expert Expert
                              • Feb 2006
                              • 9067

                              #15
                              No problem. You are always welcome to start a new thread for any further programs you are having, or to continue this one.

                              Comment

                              Working...