Thread: LED sign control help for a Fireman

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    2

    LED sign control help for a Fireman

    I have taken an alerting system and built it to use at my station. That being said my programming knowledge is all self taught. Im hoping someone will be able to help me finish my last error or at least point me in the right direction.
    The code below is the control program for a LED signboard. The lines towards the bottom that read "n = write(fd,"\x61\x1C\x32",2);" control the colors for the board. Now to the problem, the board displays any color it wants it is not following what it is supposed to. Any one have any help to give.

    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,"\x61\x1C\x32",2);
    		else if(argv[1] == 'r')
    			n = write(fd,"\x61\x1C\x31",2);
    		else 
    			n = write(fd,"\x61\x1C\x38",2);
    	
    		n = write(fd,argv[3],strlen(argv[3]));
    	}
    
    	//n = write(fd,"AA\x1B b",5);
    	//n = write(fd,"\x1C\x32\x08\x07A",3);
    
    	n = write(fd,"\004",1);
    	close(fd);
    
    	return(0);
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    //if(argv[1] == 'a' && argv[2] == 'a')

    You're can't compare an array of char with a character in C! Use strcmp, eg:

    Code:
    if(strcmp(argv[1], "a") == 0 && strcmp(argv[2], "a") == 0)
    Also, it might be a good idea to check that argc == 3, and if not, print a 'usage' message...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > n = write(fd,"\x61\x1C\x38",2);
    Perhaps because the string you're trying to send is 3 chars, and you only send two.

    With the exception of the first set of nul's, something like this would be a lot less error prone.
    Code:
    int myWriteString ( int fd, const char *msg ) {
      return write( fd, msg, strlen(msg);
    }
    So you can do
    n = myWriteString( fd, "\x61\x1C\x38" );
    without having to do manual edits of the length every time you change the string.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    2
    Salem Im not exactly sure where this line of code is supposed to go. Please forgive me Im still learning all this as I go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch function problem
    By ahming in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 09:40 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM