玩转开发板--CH341串口编程及测试

 


    

开发环境:CentOS6.7(虚拟机)

交叉编译器:arm-linux-gcc  gcc version 4.4.6 (crosstool-NG 1.16.0)

开发板fl2440(内核版本-linux-3.0)

开发模块:SIM900 GPRS


串口编程:

    编写串口需要注意的点:

    1.串口为串行通信,全双工,异步通信,因此需要了解它的异步通信的速率;

    2.了解串口包含哪几种数据位,异步通信速率,校验位及停止位等;

   


    简易串口set_comport.c:

 

/*********************************************************************************
*      Copyright:  (C) 2018 Lingyunstudio
*                  All rights reserved.
*
*       Filename:  set_comport.c
*    Description:  This file
*                 
*        Version:  1.0.0(04/28/2018)
*         Author:  guozhihao <810170156@qq.com>
*      ChangeLog:  1, Release initial version on "04/28/2018 01:04:27 AM"
*                 
********************************************************************************/
/* head of sturct termios */
#include <termios.h>
#include <unistd.h>

/* head of perror */
#include <stdio.h>
#include <errno.h>

/* head of bzero() */
#include <strings.h>

/********************************************************************************
*  Description:comport about ttyUSB0
*   Input Args:fd=open("/dev/ttyUSB0",O_RDWR | O_NOCTTY | O_NDELAY) nspeed nBits:8 nEvent:N nStop:1
*  Output Args:comport failure:return -1
* Return Value:
********************************************************************************/

int set_comport(int fd,int nSpeed,int nBits,char nEvent,int nStop)    //串口设置,第一个传递文件描述符,第二个是设置异步通信的速率,第三个设置数据位,第四个设置校验位,第五个设置停止位;
{
    struct termios newttyUSB0,oldttyUSB0;        //设置新旧结构体

    if(tcgetattr(fd,&oldttyUSB0) != 0)                //保存原先串口配置
    {
        perror("Setupserial 1");
        return -1;
    }

    bzero(&newttyUSB0,sizeof(newttyUSB0));    //将这个结构体中的内存区域的内容全清为0
    newttyUSB0.c_cflag|=(CLOCAL|CREAD);       //将c_c
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值