打印

串口读写程序 (原创,虽然参考了别的程序)

[复制链接]
1509|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
wangkj|  楼主 | 2007-12-5 17:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <string.h> // Check to be sure we need this
#include <stdlib.h>
// below are the includes for raw I/O
#include <fcntl.h>
#include <unistd.h>
//#include "configsym.h"
#define PORT "/dev/ttyS1"
#include <stdio.h>
//#include "curses.h"
#include <stdlib.h>
#include <fcntl.h>
#include "termio.h"
#include <string.h>
#include <time.h>
int portfd;
struct termio newio,oldio;
int SetPort()
{
    if ( portfd < 0 )  return -1;
    if ( ioctl(portfd, TCGETA, &oldio) < 0 ) return -1;
    if ( ioctl(portfd, TCGETA, &newio) < 0 ) return -1;
    
    newio.c_cflag &= ~CBAUD;
    newio.c_cflag &= ~CSIZE;
    newio.c_cflag |= B115200;
    newio.c_cflag |= CS8;
    newio.c_cflag |= (CLOCAL | CREAD);
    newio.c_cflag &= ~(PARENB | CSTOPB);

    newio.c_iflag &= ~( INLCR |ISTRIP |IXON |ICRNL |IGNCR | IGNPAR);
    newio.c_oflag &= ~OPOST;
    newio.c_lflag &= ~( ISIG |ECHO |ICANON | NOFLSH);
    
    newio.c_cc[VMIN] = 1;
    newio.c_cc[VTIME] = 0;
    newio.c_cc[VQUIT] = 0x7f;

    if ( ioctl(portfd, TCSETAW, &newio) ) return -1;
    return 0;
}

main()
{
  int ch;
  static int n;

#if defined(O_NDELAY) && defined(F_SETFL)
    portfd = open(PORT, O_RDWR|O_NDELAY);
    if (portfd >= 0){
        /* Cancel the O_NDELAY flag. */
        n = fcntl(portfd, F_GETFL, 0);
        (void) fcntl(portfd, F_SETFL, n & ~O_NDELAY);
    }
#else
    portfd = open(PORT, O_RDWR);
#endif
    if (portfd >= 0) {
    printf("return value is %d",SetPort());
    }
      n='0';
      for(;;){
           if (n>('0'+30)) n='\n';
       printf("write %d characte:%c\n",write(portfd,&n,1),n);
           n++;
      }
}

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

581

主题

9976

帖子

23

粉丝