|
发表于 2016-7-18 15:22:17
|
显示全部楼层
chalot 发表于 2016-7-18 11:46
谢谢! 是0-10V的 所以我才想加外一个ADC模块 但不知道要怎么调用引脚
通过串口读adc模块传回来的数据吧,
cb2 有很多个串口可以用的,去修改script.fex可以开启需要的串口
首先你要挂载
mount /dev/mmcblk1p1 /mnt
bin2fex script.bin script.fex
vi script.fex
查找uart 开启你需要的串口
这个我烟雾传感器与cb2 串口和蜂鸣器实现报警的情况
开了uart3
我的程序,你参考一下- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #include <sys/time.h>
- #include <string.h>
- #define TRUE 1
- #define FALSE -1
- #define FREZZ_ON "echo 0 > /sys/class/gpio/gpio10_pd17/value"
- #define FREZZ_OFF "echo 1 > /sys/class/gpio/gpio10_pd17/value"
- int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300,
- B38400, B19200, B9600, B4800, B2400, B1200, B300, };
- int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300,
- 38400, 19200, 9600, 4800, 2400, 1200, 300, };
- void set_speed(int fd, int speed)
- {
- int i;
- int status;
- struct termios Opt;
- tcgetattr(fd,&Opt);
- for (i= 0;i<sizeof(speed_arr)/sizeof(int);i++)
- {
- if(speed == name_arr[i])
- {
- tcflush(fd, TCIOFLUSH);
- cfsetispeed(&Opt, speed_arr[i]);
- cfsetospeed(&Opt, speed_arr[i]);
- status = tcsetattr(fd, TCSANOW, &Opt);
- if(status != 0)
- perror("tcsetattr fd1");
- return;
- } tcflush(fd,TCIOFLUSH);
- }
- }
- int set_Parity(int fd,int databits,int stopbits,int parity)
- {
- struct termios options;
- if( tcgetattr( fd,&options)!= 0)
- {
- perror("SetupSerial 1");
- return(FALSE);
- }
- options.c_cflag &= ~CSIZE;
- switch(databits)
- {
- case 7:
- options.c_cflag |= CS7;
- break;
- case 8:
- options.c_cflag |= CS8;
- break;
- default:
- fprintf(stderr,"Unsupported data size\n");
- return (FALSE);
- }
- switch(parity)
- {
- case 'n':
- case 'N':
- options.c_cflag &= ~PARENB; /* Clear parity enable */
- options.c_iflag &= ~INPCK; /* Enable parity checking */
- options.c_iflag &= ~(ICRNL|IGNCR);
- options.c_lflag &= ~(ICANON );
- break;
- case 'o':
- case 'O':
- options.c_cflag |= (PARODD | PARENB);
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'e':
- case 'E':
- options.c_cflag |= PARENB; /* Enable parity */
- options.c_cflag &= ~PARODD;
- options.c_iflag |= INPCK; /* Disnable parity checking */
- break;
- case 'S':
- case 's': /*as no parity*/
- options.c_cflag &= ~PARENB;
- options.c_cflag &= ~CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported parity\n");
- return (FALSE);
- }
- switch(stopbits)
- {
- case 1:
- options.c_cflag &= ~CSTOPB;
- break;
- case 2:
- options.c_cflag |= CSTOPB;
- break;
- default:
- fprintf(stderr,"Unsupported stop bits\n");
- return (FALSE);
- }
- /* Set input parity option */
- if(parity != 'n')
- options.c_iflag |= INPCK;
- options.c_cc[VTIME] = 150; // 15 seconds
- options.c_cc[VMIN] = 0;
- tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
- if(tcsetattr(fd,TCSANOW,&options) != 0)
- {
- perror("SetupSerial 3");
- return (FALSE);
- }
- return (TRUE);
- }
- int main(int argc, char **argv)
- {
- int fd;
- int nread; int nwrite;
- int n=0; int i=0;
- char buffer[200];
- char devname_head[10] = "/dev/";
- char dev_name[20];
-
- #if 1
- if(argc < 2)
- {
- printf("Please input './test_uart ttySx'\n");
- exit(1);
- }
- else
- {
- strcpy(dev_name, devname_head);
- strcat(dev_name, argv[1]);
- }
- fd = open(dev_name, O_RDWR);
- if(fd < 0)
- {
- perror("error to open /dev/ttySx\n");
- exit(1);
- }
- #endif
- if (fd > 0)
- {
- set_speed(fd,9600);
- }
- else
- {
- printf("Can't Open Serial Port!\n");
- exit(0);
- }
- if (set_Parity(fd,8,1,'N') == FALSE)
- {
- printf("Set Parity Error\n");
- exit(1);
- }
-
- system(FREZZ_OFF);
- memset(buffer,0,sizeof(buffer));
- while(1)
- {
- nread = read(fd,&buffer,200);
- if(nread < 0)
- {
- printf("read error\n");
- }
-
- char a=atoi(buffer);
-
-
- if(a > 80)
- {
- system(FREZZ_ON);
-
- }
- else
- {
- system(FREZZ_OFF);
- }
- memset(buffer,0,sizeof(buffer));
-
- }
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|