Advertisement
moonlightcheese

serialtest.c

Nov 9th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <termios.h>
  7.  
  8. #define MAX_WEIGHT_BUFFER   1024
  9. #define WEIGHT_TYPE_GROSS   1
  10. #define WEIGHT_TYPE_TARE    2
  11. #define TRUE    1
  12. #define FALSE   0
  13.  
  14. int main() {
  15.     int fd;
  16.     char buf[14];
  17.     int res=0;
  18.    
  19.     //printf("opening port...%s", "\n");
  20.     fd=open("/dev/ttymxc0", O_RDWR|O_NOCTTY);
  21.  
  22.     if(fd==-1) {
  23.         //printf("unable to open serial port%s", "\n");
  24.         return -2;
  25.     } else {
  26.         tcflush(fd, TCIFLUSH);
  27.         //printf("opened port%s", "\n");
  28.         //printf("getting attr%s", "\n");
  29.         struct termios oldtio,newtio;
  30.         tcgetattr(fd,&oldtio);
  31.         bzero(&newtio, sizeof(newtio));
  32.         newtio.c_cflag = B9600|CS8|CREAD|CLOCAL;
  33.         newtio.c_iflag = 0;
  34.         newtio.c_oflag = 0;
  35.         newtio.c_lflag = ICANON;
  36.         newtio.c_cc[VINTR] = 0;
  37.         newtio.c_cc[VQUIT] = 0;
  38.         newtio.c_cc[VERASE] = 0;
  39.         newtio.c_cc[VKILL] = 0;
  40.         newtio.c_cc[VEOF] = 0;
  41.         newtio.c_cc[VTIME] = 0;
  42.         newtio.c_cc[VMIN] = 14;
  43.         newtio.c_cc[VSWTC] = 0;
  44.         newtio.c_cc[VSTART] = 0;
  45.         newtio.c_cc[VSTOP] = 0;
  46.         newtio.c_cc[VSUSP] = 0;
  47.         newtio.c_cc[VEOL] = 10;
  48.         newtio.c_cc[VREPRINT] = 0;
  49.         newtio.c_cc[VDISCARD] = 0;
  50.         newtio.c_cc[VWERASE] = 0;
  51.         newtio.c_cc[VLNEXT] = 0;
  52.         newtio.c_cc[VEOL2] = 0;
  53.         //printf("flushing buffer%s", "\n");
  54.         //tcflush(fd, TCIFLUSH);
  55.         //printf("setting attr...%s", "\n");
  56.         tcsetattr(fd, TCSAFLUSH, &newtio);
  57.         //printf("attr set%s", "\n");
  58.         /*
  59.         struct termios options;
  60.         tcgetattr(fd, &options);
  61.         cfsetispeed(&options, B9600);
  62.         cfsetospeed(&options, B9600);
  63.         options.c_cflag |= (CLOCAL|CREAD);
  64.         tcsetattr(fd, TCSANOW, &options);
  65.         options.c_cflag &= ~CSIZE;
  66.         options.c_cflag |= CS8;
  67.         options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
  68.         options.c_oflag &= ~OPOST;
  69.         options.c_cc[VMIN]=0;
  70.         options.c_cc[VTIME]=10;
  71.         */
  72.     }
  73.     //printf("connected, starting loop%s", "\n");
  74.     //printf("flushing buffer%s", "\n");
  75.     tcflush(fd, TCIOFLUSH);
  76.     while(res!=14) {
  77.         res=read(fd, buf, 14);
  78.         buf[res]=0;
  79.         //printf(":%s:%d\n",buf,res);
  80.     }
  81.     //'buf' now contains valid 14 characters of data
  82.     int weight=0;
  83.     int motion=FALSE;
  84.     int weight_type=0;
  85.     int negative=TRUE;
  86.  
  87.     //get the sign value
  88.     if(buf[1]=='-') {
  89.         negative=TRUE;
  90.         //printf("negative weight%s", "\n");
  91.     } else {
  92.         negative=FALSE;
  93.     }
  94.  
  95.     //get to the numeric characters
  96.     int num_begin=0;
  97.     while((buf[num_begin]-48)<0 || (buf[num_begin]-48)>9) {
  98.         num_begin++;
  99.     }
  100.  
  101.     //process numerical value
  102.     while((buf[num_begin]-48)>=0 && (buf[num_begin]-48)<=9) {
  103.         weight*=10;
  104.         weight+=buf[num_begin]-48;
  105.         num_begin++;
  106.     }
  107.    
  108.     //printf("weight: %i%s", weight, "\n");
  109.  
  110.     //TODO: get unit type
  111.     num_begin+=2;   //skipping "Lx" for now, will process later
  112.  
  113.     if(buf[num_begin]=='M') {
  114.         motion=TRUE;
  115.         //printf("motion found%s", "\n");
  116.     }
  117.  
  118.     //build return string and send back to stdout
  119.     char weight_buf[MAX_WEIGHT_BUFFER];
  120.     char result[1024]="{\"motion\":";
  121.  
  122.     if(motion==FALSE) {
  123.         strcat(result, "false,");
  124.     } else {
  125.         strcat(result, "true,");
  126.     }
  127.     strcat(result, "\"weight\":");
  128.     if(negative==TRUE) {
  129.         strcat(result, "-");
  130.     }
  131.     //itoa(weight, weight_buf, 10);
  132.     sprintf(result, "%s%d", result, weight);
  133.     //for(int i=0; i<MAX_WEIGHT_BUFFER; i++) {
  134.     //  we
  135.     //strcat(result, weight_buf);
  136.     strcat(result, ",\"uom\":\"");
  137.     strcat(result, "LBS");  //TODO: fill in the actual units here
  138.     strcat(result, "\"}");
  139.     printf("%s\n", result);
  140.  
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement