一个 linux 的端口读写程序
/* 本程序符合 GPL条约
MyCom.c
*/
#include <stdio.h> // printf
#include <fcntl.h> // open
#include <string.h> // bzero
#include <stdlib.h> // exit
#include <sys/times.h> // times
#include <sys/types.h> // pid_t
#include <termios.h> //termios, tcgetattr(), tcsetattr()
#include <unistd.h>
#include <sys/ioctl.h> // ioctl
#include "MyCom.h"
#define TTY_DEV "/dev/ttyS" // 端口路径
#define TIMEOUT_SEC(buflen,baud) (buflen*20/baud+2) // 接收超时
#define TIMEOUT_USEC 0
/*******************************************
* 获得端口名称
********************************************/
char *get_ptty(pportinfo_t pportinfo)
{
char *ptty;
switch(pportinfo->tty){
case '0':{
ptty = TTY_DEV"0";
}break;
case '1':{
ptty = TTY_DEV"1";
}break;
case '2':{
ptty = TTY_DEV"2";
}break;
}
return(ptty);
}
/*******************************************
* 波特率转换函数(请确认是否正确)
********************************************/
int convbaud(unsigned long int baudrate)
评论0