自己写的Linux下的串口操作

代码是纯C写的,大于115200的非标准波特率是和平台相关的,这里是基于PC104的,写非标准波特率时参考了stackoverflow的一个帖子,当时竟然忘了存了。。。#ifndef USER_TYPE_H_#define USER_TYPE_H_#define SUCCESS 0#define FAILURE 1#define STRING(x) #xtypedef
摘要由CSDN通过智能技术生成

代码是纯C写的,大于115200的非标准波特率是和平台相关的,这里是基于PC104的,写读取操作时参考了AUPE,写非标准波特率时参考了stackoverflow的一个帖子,当时竟然忘了存了。。。

#ifndef USER_TYPE_H_
#define USER_TYPE_H_


#define SUCCESS			0
#define FAILURE			1
#define STRING(x)		#x

typedef unsigned char  UCHAR;
typedef unsigned short USHORT;
typedef unsigned int   UINT;
typedef unsigned long  ULONG;


typedef unsigned int   uint24;


#endif

#ifndef SERIAL_PORT_H_
#define SERIAL_PORT_H_

#include "user_type.h"


/*
 * configuration of a serial port
 */
struct Config{
	int  baudRate;		      //baud rate
	int  dataBits;			  //data bits
	char parity;			  //parity
	int  stopBits;			  //stop bits
	int  minToRead;			  //minimum bytes to return when calling read
	int  timeoutBetweenBytes; //time out between bytes, unit in 100ms

	//baseAddr is used only when baudRate is supported nonstandard baud rate
	USHORT baseAddr;  //serial port's hardware base address
};

/*
 * This function is used to check if the baud rate is supported.
*@param baudRate: baud rate to check
*@return:
  	  	 1 if the baud rate is supported
  	  	 0 otherwise
*/
int isSupportedBaudRate(int baudRate);

/*
 * This function is used to get the type of given baudRate
*@param baudRate: baud rate to check
*@return:
  	  	 0 if given baud is supported standard baud rate
  	  	 1 if given baud is supported nonstandard baud rate
  	  	-1 if given baud is not supported
 */
int getBaudRateType(int baudRate);

/*
 * This function is used to check if the data bits is supported.
*@param dataBits: data bits to check
*@return:
  	  	 1 if the dataBits is supported
  	  	 0 otherwise
*/
int isSupportedDataBits(int dataBits);

/*
 * This function is used to check if the parity is supported.
*@param parity: parity to check
*@return:
  	  	 1 if the parity is supported
  	  	 0 otherwise
*/
int isSupportedParity(char parity);

/*
 * This function is used to check if the stop bits is supported.
*@param stopBits: stop bits to check
*@return:
  	  	 1 if the stopBits is supported
  	  	 0 otherwise
*/
int isSupportedDataBits(int stopBits);




/*
 * This function is used to open a serial port.
*@param dev:   	  serial port's system file path
*@param mode:     "ReadOnly","WriteOnly","ReadWrite"
*@param blocking: whether to set the read mode to be blocking or not
*@return:
  	  	 corresponding file handle if open the serial port successfully
  	  	 a negative number otherwise
*/
int openSerialPort(
	const char* dev,
	const char* mode,
	int         blocking
);



/*
 * This function is used to set configuration for the serial port.
*@param fd:       		     file handle of target serial port
*@param baud:     		   	 38400, 19200, 9600, 4800, 2400, 1200, 300
*@param dataBits:     	   	 7 or 8
*@param stopBits:            1 or 2
*@param parity:              'N' or 'E' or 'O' or 'S'
*@param minToRead: 	   	  	 minimum bytes to return when calling read
*@param timeoutBetweenBytes: time out between bytes, unit in 100ms
*@return:
		 0 if done configuration successfully
   	   	 negative numbers if error happens
*/
int configurateSerialPort(
	int fd,
	int baud,
	int dataBits,
	char parity,
	int stopBits,
	int minToRead,
	int timeoutBetweenBytes
);



/*
 * This function is used to open a serial port.
*@param dev:  	 serial port's system file path
*@param mode: 	 "ReadOnly","WriteOnly","ReadWrite"
*@param blocking: whethe
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值