免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1799 | 回复: 0
打印 上一主题 下一主题

zigbee模块(顺舟)+串口发送接收+mini2440 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-12-22 11:29 |只看该作者 |倒序浏览

                下面是mini2440的发送程序,
arm-linux-gcc -o send send.c
起初用串口线跟mini2440的232接口想连接,但是不理想,可能是
mini2440的232转换芯片不好使,反正怎么都调试不通过。看看原理
图,发现有个con1,就是对应着串口1的cpu的ttl引脚,连上就ok了。
send.c-------------------------------------
#include      
#include      
#include      
#include      
#include
#include
#include
#define BAUDRATE B115200     
#define DEV "/dev/tts/0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */      
#define FALSE 0      
#define TRUE 1              
volatile int STOP=FALSE;            
main() {
    int fd,c, res;  
    struct termios oldtio,newtio;   
    char buf[255];
    fd = init_dev(&oldtio);
    while (STOP==FALSE) {
        send_cmd(fd,"hello");putchar(0x0d);
        usleep(500000);
    }
    tcsetattr(fd,TCSANOW,&oldtio);
}
/* write the users command out the serial port */
int send_cmd(int ftty,char * str)
    {
     int result;
     result = write(ftty,str,strlen(str));/*argv[4], strlen(argv[4]));*/
     if (result      
#include      
#include      
#include      
#include
#include
#include
#define BAUDRATE B115200     
#define DEV "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */      
#define FALSE 0      
#define TRUE 1              
volatile int STOP=FALSE;            
main() {
    int fd,c, res;   
    char buf[255];
    fd = open(DEV, O_RDWR| O_NOCTTY|O_NDELAY);
    if (fd ==-1)
    {
        perror("open"DEV);
        return -1;
    }
    else //set to block;
        fcntl(fd, F_SETFL,0);
    printf("open serial ok\n");
    if(cfg_serial(fd,8,1,'N')== FALSE)
    {
        printf("cfg serial Error\n");
        exit(1);
    }
    printf("start recieve\n");
    while (STOP==FALSE) {       /* loop for input */
        static counter = 0;
        counter ++;
        res = read(fd,buf,200);   /* returns after 200 chars have been input */   
        buf[res]=0;               /* so we can printf... */  
        printf("%d(%d)\t%s\n",counter,res,buf);
        fflush(stdout);
        if(buf[0]=='z') STOP=TRUE;   
    }
}
/*
*@brief   设置串口数据位,停止位和效验位
*@param fd     类型 int 打开的串口文件句柄*
*@param databits 类型 int 数据位   取值 为 7 或者8*
*@param stopbits 类型 int 停止位   取值为 1 或者2*
*@param parity 类型 int 效验类型 取值为N,E,O,S
*/
int cfg_serial(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_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
            options.c_oflag &= ~OPOST;   /*Output*/
            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')&&(parity != 'N'))
    options.c_iflag |= INPCK;
    options.c_cc[VTIME] = 5; // 0.5 seconds
    options.c_cc[VMIN] = 1;
    options.c_cflag &= ~HUPCL;
    options.c_iflag &= ~INPCK;
    options.c_iflag |= IGNBRK;
    options.c_iflag &= ~ICRNL;
    options.c_iflag &= ~IXON;
    options.c_lflag &= ~IEXTEN;
    options.c_lflag &= ~ECHOK;
    options.c_lflag &= ~ECHOCTL;
    options.c_lflag &= ~ECHOKE;
    options.c_oflag &= ~ONLCR;
    tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
    if (tcsetattr(fd,TCSANOW,&options) != 0)
    {
        perror("SetupSerial");
        return (FALSE);
    }
    return (TRUE);
}
上面的函数都是从网上找来的,做个技术积累。
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93291/showart_2127775.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP