Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <termios.h>
- #define MAX_WEIGHT_BUFFER 1024
- #define WEIGHT_TYPE_GROSS 1
- #define WEIGHT_TYPE_TARE 2
- #define TRUE 1
- #define FALSE 0
- int main() {
- int fd;
- char buf[14];
- int res=0;
- //printf("opening port...%s", "\n");
- fd=open("/dev/ttymxc0", O_RDWR|O_NOCTTY);
- if(fd==-1) {
- //printf("unable to open serial port%s", "\n");
- return -2;
- } else {
- tcflush(fd, TCIFLUSH);
- //printf("opened port%s", "\n");
- //printf("getting attr%s", "\n");
- struct termios oldtio,newtio;
- tcgetattr(fd,&oldtio);
- bzero(&newtio, sizeof(newtio));
- newtio.c_cflag = B9600|CS8|CREAD|CLOCAL;
- newtio.c_iflag = 0;
- newtio.c_oflag = 0;
- newtio.c_lflag = ICANON;
- newtio.c_cc[VINTR] = 0;
- newtio.c_cc[VQUIT] = 0;
- newtio.c_cc[VERASE] = 0;
- newtio.c_cc[VKILL] = 0;
- newtio.c_cc[VEOF] = 0;
- newtio.c_cc[VTIME] = 0;
- newtio.c_cc[VMIN] = 14;
- newtio.c_cc[VSWTC] = 0;
- newtio.c_cc[VSTART] = 0;
- newtio.c_cc[VSTOP] = 0;
- newtio.c_cc[VSUSP] = 0;
- newtio.c_cc[VEOL] = 10;
- newtio.c_cc[VREPRINT] = 0;
- newtio.c_cc[VDISCARD] = 0;
- newtio.c_cc[VWERASE] = 0;
- newtio.c_cc[VLNEXT] = 0;
- newtio.c_cc[VEOL2] = 0;
- //printf("flushing buffer%s", "\n");
- //tcflush(fd, TCIFLUSH);
- //printf("setting attr...%s", "\n");
- tcsetattr(fd, TCSAFLUSH, &newtio);
- //printf("attr set%s", "\n");
- /*
- struct termios options;
- tcgetattr(fd, &options);
- cfsetispeed(&options, B9600);
- cfsetospeed(&options, B9600);
- options.c_cflag |= (CLOCAL|CREAD);
- tcsetattr(fd, TCSANOW, &options);
- options.c_cflag &= ~CSIZE;
- options.c_cflag |= CS8;
- options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
- options.c_oflag &= ~OPOST;
- options.c_cc[VMIN]=0;
- options.c_cc[VTIME]=10;
- */
- }
- //printf("connected, starting loop%s", "\n");
- //printf("flushing buffer%s", "\n");
- tcflush(fd, TCIOFLUSH);
- while(res!=14) {
- res=read(fd, buf, 14);
- buf[res]=0;
- //printf(":%s:%d\n",buf,res);
- }
- //'buf' now contains valid 14 characters of data
- int weight=0;
- int motion=FALSE;
- int weight_type=0;
- int negative=TRUE;
- //get the sign value
- if(buf[1]=='-') {
- negative=TRUE;
- //printf("negative weight%s", "\n");
- } else {
- negative=FALSE;
- }
- //get to the numeric characters
- int num_begin=0;
- while((buf[num_begin]-48)<0 || (buf[num_begin]-48)>9) {
- num_begin++;
- }
- //process numerical value
- while((buf[num_begin]-48)>=0 && (buf[num_begin]-48)<=9) {
- weight*=10;
- weight+=buf[num_begin]-48;
- num_begin++;
- }
- //printf("weight: %i%s", weight, "\n");
- //TODO: get unit type
- num_begin+=2; //skipping "Lx" for now, will process later
- if(buf[num_begin]=='M') {
- motion=TRUE;
- //printf("motion found%s", "\n");
- }
- //build return string and send back to stdout
- char weight_buf[MAX_WEIGHT_BUFFER];
- char result[1024]="{\"motion\":";
- if(motion==FALSE) {
- strcat(result, "false,");
- } else {
- strcat(result, "true,");
- }
- strcat(result, "\"weight\":");
- if(negative==TRUE) {
- strcat(result, "-");
- }
- //itoa(weight, weight_buf, 10);
- sprintf(result, "%s%d", result, weight);
- //for(int i=0; i<MAX_WEIGHT_BUFFER; i++) {
- // we
- //strcat(result, weight_buf);
- strcat(result, ",\"uom\":\"");
- strcat(result, "LBS"); //TODO: fill in the actual units here
- strcat(result, "\"}");
- printf("%s\n", result);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement