linux串口进程,Linux下串口通信示例程序

已测试的结果是:可以正确发送数据,在接受的时候会存在问题。欢迎批评指正。

参考资料:《linux串口通信入门教程》,作者:左锦

/**

说明:

1.STR_NUM宏为需要发送的字符串的个数+1,这些字符串被存储在指针数组pstr中。

2.如果只是需要发送数据而不读取数据,则取消DEGUB宏的定义。

3.默认的串口设备是"/dev/ttyUSB0",可以修改宏DEFAULT_DEV的定义来设定需要的串口设备。

4.如果需要发送main函数接受的参数而不是pstr指针数据中静态初始化的字符串,则需要定义FROM_ARGV宏(默认情况下该宏未被定义)。

5.如果需要发送不可打印字符,可以参照程序中pstr数组的最后一个元素的形式((char *)26)。

6.超时等待时间由DEFAULT_INTERVAL宏决定,可以修改它以适应具体需要。

7.默认串口参数: 波特率:9600 数据位:8 停止位:1 校验: 无 串口设备: /dev/ttyUSB0(该设备为USB转串口设备)。如果需要修改这些参数,只需要修改对应的宏定义即可。

8.在DEBUG模式下读取的数据可能会不正确。如果您知道原因欢迎指教: hqwemail@163.com

9.by 江湖夜雨 2010-6-18

*/

#include

#include

#include

#include

#include

#include

#include

#include

#define TRUE 0

#define FALSE -1

#define DEFAULT_DEV "/dev/ttyUSB0"

#define DEFAULT_BAUND B9600

#define DEFAULT_DATABIT CS8

#define DEFAULT_PAR ~PARENB

#define DEFAULT_INP ~INPCK

#define DEFAULT_STOPBIT ~CSTOPB

#define DEFAULT_INTERVAL 11

#define STR_NUM 9

#define DEBUG

//#define FROM_ARGV

//测试模式下读取程序读取串口内容

#ifdef DEBUG

#define READ_BUFFER_SIZE 32

#endif

//如果没有定义宏FROM_ARGV则定义pstr为指针数组,其各个元素所指向的内容为需要发送的数据

#ifndef FROM_ARGV

static const char *pstr[] = {0, "AT", "AT", "AT+CMGF=1", "AT+CMGS=", "\"", "13474669578", "\"", ";\r", (char *)26};

#else  //否则pstr被重新重命名为argv,程序将发送main函数接受的字符串参数

#define pstr argv

#endif

int open_dev(const char *dev_name);

int set_port(const int fd);

int send_data(const int fd, const char *buffer, const int buffer_len);

#ifdef DEBUG

int read_data(const int fd, char *read_buffer);

#endif

int main(int argc, char *argv[])

{

int fd;

int i;

char *dev_name = DEFAULT_DEV;

#ifdef DEBUG

char read_buffer[READ_BUFFER_SIZE];

int read_buffer_size;

#endif

//打开串口

if((fd = open_dev(dev_name)) == FALSE){

perror("open error!");

return -1;

}

//设置串口

if(set_port(fd) == FALSE){

perror("set error!");

return -1;

}

//发送数据

do{

for(i = 1; i < STR_NUM; i++){

send_data(fd, pstr[i], strlen(pstr[i]));

}

#ifdef DEBUG

read_buffer_size = read_data(fd, read_buffer);

printf("read: %s\nsize: %d\n", read_buffer, read_buffer_size);

#endif

}while(0);

close(fd);

return 0;

}

//打开串口

int open_dev(const char *dev_name)

{

return open(dev_name, O_RDWR);

}

int set_port(const int fd)

{

//设置速率

struct termios opt;

if(tcgetattr(fd, &opt) != 0){

return FALSE;

}

cfsetispeed(&opt, DEFAULT_BAUND);

cfsetospeed(&opt,DEFAULT_BAUND);

tcsetattr(fd,TCSANOW,&opt);

opt.c_cflag &= ~CSIZE;

//设置数据位

opt.c_cflag |= DEFAULT_DATABIT;

//设置校验位

opt.c_cflag &= DEFAULT_PAR;

opt.c_iflag &= DEFAULT_INP;

//设置停止位

opt.c_cflag &= DEFAULT_STOPBIT;

tcflush(fd, TCIFLUSH);

opt.c_cc[VTIME] = DEFAULT_INTERVAL;

opt.c_cc[VMIN] = 0;

if(tcsetattr(fd, TCSANOW, &opt) != 0){

return FALSE;

}

return TRUE;

}

//发送数据

int send_data(const int fd, const char *buffer, const int buffer_len)

{

return write(fd, buffer, buffer_len);

}

#ifdef DEBUG

//读取数据

int read_data(const int fd, char *read_buffer)

{

return read(fd, read_buffer, READ_BUFFER_SIZE);

}

#endif0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值