先声明,没调试过。。仅供参考,有错误的地方请指正,谢谢!
////////////////////////
/******setport.h*******/
////////////////////////
#ifndef SETPORT_H
#define SETPORT_H
/*********************
int dwBaudRate //波特率
char bTTY //串口号
char bDataBit //数据位5,6,7,8
char bParity //奇偶校验 无校验:'N' 偶校验'E' 奇校验'O'
char bStopBit //停止位1,2
char bFctl //流控制无控制:'N' 硬件控制:'H'软件控制:'S'
**********************/
//-------------------打开串口-------------------//bTTY==0,1,2,3, 对应串口Com1到Com4,成功返回文件描述符,失败==(-1)
extern int PortOpen(char bTTY)
//-------------------设置串口-------------------//成功==(0) 失败==(-1)
extern int PortSet(int dwFdcom,int dwBaudRate,char bTTY,char bDataBit,char bParity,char bStopBit,char bFctl)
//-------------------关闭串口-------------------//
extern void PortClose(int dwFdcom)
//-------------------写串口-------------------//成功返回datalen 失败==(<0)
extern int PortWrite(int dwFdcom,char *cpSendBuf,unsigned int DataLen)
//-------------------读串口-------------------//成功返回datalen 失败==(-1)
extern int PortRead(int dwFdcom,char *cpRecvBuf,unsigned int DataLen,unsigned int dwBaudRate)
//-------------------超时设置-------------------//成功==0失败==(<0)
extern int PortTimeOut(int dwFdcom, char timeout, int len)
#endif
/////////////////////////////
/***********setport.c*******/
////////////////////////////
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <sys/times.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "setport.h"
//-------------------打开串口-------------------//
extern int PortOpen(char bTTY)
{
int dwFdcom
unsigned char Ptty[10]="/dev/ttyS"
unsigned char *cpGtty
cpGtty=strcat(Ptty,&bTTY)
if(!cpGtty) return -1
else {
dwFdcom=open(cpGtty,O_RDWR|O_NOCTTY)
}
return (dwFdcom)
}
//-------------------关闭串口-------------------//
extern void PortClose(int dwFdcom)
{
close(dwFdcom)
}
//-------------------设置串口-------------------//
extern int PortSet(int dwFdcom,int dwBaudRate,char bTTY,char bDataBit,char bParity,char bStopBit,char bFctl)
{
static int speed_arr[] = {B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300}
static int name_arr[] = {38400, 19200, 9600, 4800, 2400,1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300}
struct termios oldtm_t,newtm_t
int i=0char *pDatabit
bzero(&oldtm_t,sizeof(oldtm_t))
bzero(&newtm_t,sizeof(newtm_t))
cfmakeraw(&newtm_t)
tcgetattr(dwFdcom,&oldtm_t)
/*------------设置端口属性------------*/
for( i<sizeof(speed_arr)/sizeof(int) i++)
{
if(dwBaudRate==name_arr[i])
{
cfsetispeed(&newtm_t,speed_arr[i])
cfsetospeed(&newtm_t,speed_arr[i])
}
else return -1
}
newtm_t.c_cflag|=CLOCAL
newtm_t.c_cflag|=CREAD
newtm_t.c_cflag &= ~CSIZE
switch(bDataBit) //数据位
{
case '5':
newtm_t.c_cflag|=CS5
break
case '6':
newtm_t.c_cflag|=CS6
break
case '7':
newtm_t.c_cflag|=CS7
break
case '8':
newtm_t.c_cflag|=CS8
break
default:
return -2
}
switch(bParity)
{
case 'N':{ //无校验
newtm_t.c_cflag&=~PARENB
newtm_t.c_iflag&= ~INPCK
}break
case 'O':{ //奇校验
newtm_t.c_cflag |= (PARODD | PARENB)
newtm_t.c_iflag |= INPCK
}break
case 'E':{ //偶校验
newtm_t.c_cflag |= PARENB
newtm_t.c_cflag &= ~PARODD
newtm_t.c_iflag |= INPCK
}break
default:
return -3
}
//停止位
if(bStopBit=='2') newtm_t.c_cflag|=CSTOPB //2
else newtm_t.c_cflag&=~CSTOPB //1
switch(bFctl) //流控制
{
case 'N':{ //无控制
newtm_t.c_cflag &= ~CRTSCTS
newtm_t.c_iflag &= ~(IXON | IXOFF | IXANY )
}break
case 'H':{ //硬件控制
newtm_t.c_cflag |= CRTSCTS
newtm_t.c_iflag &= ~(IXON | IXOFF | IXANY )
}break
case 'S':{ //软件控制
newtm_t.c_cflag &= ~CRTSCTS
newtm_t.c_iflag |= (IXON | IXOFF | IXANY )
}break
default:
return -4
}
tcflush(dwFdcom,TCIFLUSH)//端口复位
tcsetattr(dwFdcom,TCSANOW,&newtm_t) //使端口属性设置生效
return 0
}
//-------------------超时设置-------------------//
extern int PortTimeOut(int dwFdcom, char timeout, int len)
{
struct termios newtm_t
if(tcgetattr(dwFdcom, &newtm_t) != 0)
{
return (-1)
}
newtm_t.c_lflag &= ~ICANON
newtm_t.c_cc[VTIME] = timeout
newtm_t.c_cc[VMIN] = len
if(tcsetattr(dwFdcom, TCSANOW, &newtm_t) != 0)
{
return (-2)
}
return 0
}
////读写串口用文件读写方式,read和write两个方法,
////read是接收数据,write是发送数据
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)