[Linux项目实践] 物联网单板测试之任务五:ZigBee Module之Fuction

完成单板代码

/*任务5ZigBee无线模块应用*/

ZigBee Module传递信息到主机2440

1头文件

#include <stdio.h>

#include <termios.h>

#include <unistd.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdlib.h>

#include <errno.h>

#define SIZE 12

#define BUFF 10

#define MAX_COM_NUM 3

typedef struct {

char x;

char y;

char z;

unsigned char temp[2];

unsigned char hum[2];

unsigned long lux;

}BOARD_INFO;

BOARD_INFO board_info; 

                           

int open_port(fd);    //打开串口

int set_uart_config(int fd, int baud_rate, int data_bits, char parity, int stop_bits);  

//2打开串口

int open_port(int com_port)

{

int fd;

//2.1、使用普通串口

#if (COM_TYPE == GNR_COM)   

char *dev[] = {"/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2"};

#else

//2.2、使用USB转串口

char *dev[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2"};   

#endif

if((com_port < 0) || (com_port > MAX_COM_NUM))

{

return -1;

}

printf("1> get a com_port success\n");

//2.3、打开串口

fd = open(dev[com_port -1], O_RDWR|O_NOCTTY|O_NDELAY);

if(fd < 0)

{

perror("open serial port");

return -1;

}

printf("2> open serial port success\n");

//2.4、回复串口为阻塞状态

if(fcntl(fd, F_SETFL, 0) < 0)

{

perror("fcntl F_SETFL\n");

return -1;

}

printf("3> set the port success\n");

//2.5、测试打开的文件是否为终端设备

if(isatty(fd) == 0)

{

perror("This is not a  terminal device");

return -1;

}

printf("4> get a terminal device success\n");

return fd;

}

3、设置串口属性

/*

termios是在POSIX规范中定义的标准接口,表示终端设备

串口设置主要是设置struct termios结构体的各成员

struct termios

{

unsigned short c_iflag;   //输入模式标志位

unsigned shrot c_oflag;   //输出模式标志位

unsigned short c_cflag;   //控制模式标志位

unsigned short c_lflag;   //本地模式标志位

unsigned char c_line;    //线路规程

unsigend char c_cc[NCC];   //控制特性

speed_t c_ispeed;    //输入速度

speed_t c_ospeed;   //输出速度

}

*/

//设置串口属性,参数:串口文件描述符、波特率、数据位、奇偶校验位、停止位

int set_uart_config(int fd, int baud_rate, int data_bits, char parity, int stop_bits)

{

//3.1、串口

struct termios old_cfg,new_cfg;

int speed;

//3.2、保存并测试现有串口参数设置

if(tcgetattr(fd,&new_cfg) != 0)

{

perror("tcgetattr");

return -1;

}

new_cfg = old_cfg;

//3.3、串口设置为原始模式

cfmakeraw(&new_cfg);  

new_cfg.c_cflag &= ~CSIZE;

//3.4、设置串口波特率

switch(baud_rate)

{

case 2400:

{

speed = B2400;

}

break;

case 4800:

{

speed = B4800;

}

break;

case 9600:

{

speed = B9600;

}

break;

case 19200:

{

speed = B19200;

}

break;

case 38400:

{

speed = B38400;

}

break;

case 115200:

{

speed = B115200;

}

break;

}

cfsetispeed(&new_cfg, speed);

cfsetospeed(&new_cfg, speed);

//3.5、设置字符大小

switch(data_bits)   

{

case 7:

{

new_cfg.c_cflag |= CS7;

}

break;

case 8:

{

new_cfg.c_cflag |= CS8;

}

break;

}

//3.6、设置奇偶校验位

switch(parity)

{

default:

case 'n':

case 'N':

{

new_cfg.c_cflag &= ~PARENB;

new_cfg.c_iflag &= ~INPCK;

}

break;

case 'o':

case 'O':

{

new_cfg.c_cflag |= (PARODD | PARENB);

new_cfg.c_iflag |= INPCK;

}

break;

case 'e':

case 'E':

{

new_cfg.c_cflag |= PARENB;

new_cfg.c_cflag &= ~PARODD;

new_cfg.c_iflag |= INPCK;

}

break;

case 's':

case 'S':

{

new_cfg.c_cflag &= ~PARENB;

new_cfg.c_cflag &= ~CSTOPB;

}

break;

}

//3.7、设置停止位

switch(stop_bits) 

{

default:

case 1:

{

new_cfg.c_cflag &= ~CSTOPB;

}

break;

case 2:

{

new_cfg.c_cflag |= CSTOPB;

}

}

//3.8、设置等待时间和最小接收字符

new_cfg.c_cc[VTIME] = 0;

new_cfg.c_cc[VMIN] = 0;

//3.9、清除串口缓冲

tcflush(fd,TCIFLUSH);

//3.10、激活配置

if((tcsetattr(fd,TCSANOW,&new_cfg)) != 0)

{

perror("com set error");

return -1;

}

//3.11、串口设置完成

printf("set done!\n");

return 0;

4main.c

int main(void)

{

         int i,fd,nread;

 char buff[SIZE];

//4.1、打开串口

         if((fd = open_port()) < 0)       

         {

    printf("open_port error\n");

                 return 0;

         }

 printf("open port success!\n");

//4.2、设置串口

         if((i = set_uart_config(fd, B115200, 8, 's', 1)) < 0)

         {

                 perror("set_uart_config error\n");

                 return 0;

         }

         printf("uart open success!\n");

//4.3、循环从串口读取信息    

    while(1){

printf("Read the information from the UART poart:\n");

nread = read(fd,buff,SIZE);

for(i=0;i<nread;i++)

{

*((char *)&board_info+i) = buff[i];

printf("***");

}

//4.4、显示读取信息

printf("\nShow the information get from the node_board by ZigBee module:");

printf("temp: %d.%d\n",board_info.temp[0],board_info.temp[1]);

printf("hum:  %d.%d\n",board_info.hum[0],board_info.hum[1]);

printf("light:%d \n",board_info.lux);

printf("ACC x:%d,y:%d,z:%d\n",board_info.x,board_info.y,board_info.z);

printf("\n");

memset(buff,0,SIZE);//清零

sleep(1);

        close(fd);

        return 0;

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值