操作系统:ubuntu10.04
前言:
在串口编程中,当通信的设备不需求并发,可以封装成函数形式。
一,源码
(1) 头文件
-
/*
-
* lower,下位机
-
* {
-
*/
-
/* 串口的初始化 */
-
extern int32_t com_init_lower(void *param);
-
-
/* 串口的发送 */
-
extern int32_t com_send_lower(uint8_t *send_buffer,int32_t buffer_len);
-
-
/* 串口接收 */
-
extern int32_t com_recv_lower(uint8_t *buffer,const int32_t need_to_recv);
-
-
/* 串口关闭*/
-
extern int32_t com_close_lower(void *param);
-
-
/*
-
* lower,下位机
-
* }
-
*/
(2) *.c 文件
-
#define LOWER_DEVICE_COM "/dev/ttySAC0"
-
-
-
-
static int32_t __com_set(int32_t com_fd,speed_t baud_rate)
-
{
-
int32_t fd = com_fd;
-
struct termios opt;
-
// struct timeval start;
-
-
tcgetattr(fd, &opt);
-
// cfsetispeed(&opt, B115200);
-
// cfsetospeed(&opt, B115200);
-
cfsetispeed(&opt, baud_rate);
-
cfsetospeed(&opt, baud_rate);
-
-
// 8n1
-
opt.c_cflag &= ~CSIZE;
-
opt.c_cflag &= ~CSTOPB;
-
opt.c_cflag &= ~PARENB; // 无奇偶校验
-
opt.c_cflag |= CS8;
-
-
opt.c_cflag &= ~CRTSCTS; // 没有使用硬件流控制
-
-
// opt.c_cflag &= ~INPCK;
-
opt.c_cflag |= (CLOCAL | CREAD); // Enable the receiver and set local mode
-
-
opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // 原始数据
-
-
opt.c_oflag &= ~OPOST;
-
opt.c_oflag &= ~(ONLCR | OCRNL); // 添加的
-
-
opt.c_iflag &= ~(ICRNL | INLCR);
-
opt.c_iflag &= ~(IXON | IXOFF | IXANY); // 没有软件流控制
-
-
tcflush(fd, TCIOFLUSH);
-
-
opt.c_cc[VTIME] = 1; // 以 十分之一秒(1/10 s) 为单位
-
opt.c_cc[VMIN] = 0;
-
-
-
-
if(tcsetattr(fd, TCSANOW, &opt) != 0 )
-
{
-
perror("tcsetattr error");
-
return -COM_CONFIG_ERROR;
-
}
-
-
return OPER_OK;
-
}
-
-
/*
-
* lower,下位机
-
* {
-
*/
-
-
static int32_t lower_device_comfd = 0; /* 与下位机通信的串口的描述符(R2000模块)*/
-
-
-
-
static int32_t _com_set_lower()
-
{
-
int32_t ret = 0;
-
-
ret = __com_set(lower_device_comfd,B115200);
-
-
return ret;
-
}
-
-
-
-
-
/* 串口的初始化 */
-
int32_t com_init_lower(void *param)
-
{
-
int32_t ret = 0;
-
-
lower_device_comfd = open(LOWER_DEVICE_COM,O_RDWR | O_NOCTTY); // 阻塞读
-
if(lower_device_comfd< 0)
-
{
-
ret = -COM_OPEN_ERROR;
-
}
-
else
-
{
-
ret = _com_set_lower();
-
}
-
-
return ret;
-
}
-
-
-
/*
-
* 串口的发送
-
* 返回值:正常返回发送的字节数,异常返回负值
-
*/
-
int32_t com_send_lower(uint8_t *send_buffer,int32_t buffer_len)
-
{
-
int32_t ret = 0;
-
-
// 疑问1:发送数据的长度是否需要判断有效值,有效值是多少??
-
-
ret = write(lower_device_comfd, send_buffer, buffer_len);
-
-
-
return ret;
-
}
-
-
-
/* 串口接收 */
-
int32_t com_recv_lower(uint8_t *buffer,const int32_t need_to_recv)
-
{
-
int32_t ret = 0;
-
while( (ret = read(lower_device_comfd, (char*)buffer, need_to_recv)) > 0 )
-
{
-
break;
-
}
-
-
return ret;
-
}
-
-
/* 串口关闭*/
-
int32_t com_close_lower(void *param)
-
{
-
int32_t ret = 0;
-
-
if(lower_device_comfd > 0 )
-
{
-
close(lower_device_comfd);
-
lower_device_comfd = 0;
-
}
-
-
return ret;
-
}
-
-
/*
-
* lower,下位机
-
* }
-
*/
(3) 测试
-
static void com_oper()
-
{
-
unsigned char read_buf[256] = {0};
-
unsigned char write_buf[256] = {0};
-
int index = 0;
-
int data_len = 0;
-
-
-
for(index = 0; index < 3; index++)
-
{
-
bzero(read_buf, sizeof(read_buf)); //类似于memset
-
bzero(write_buf, sizeof(write_buf));
-
write_buf[0] = 0xBB;
-
-
data_len = com_send_lower(write_buf, strlen((char*)write_buf));
-
printf("write : %d\n",data_len);
-
-
while( (data_len = com_recv_lower(read_buf, sizeof(read_buf))) > 0 )
-
{
-
break;
-
}
-
-
printf("read : %d ,", data_len);
-
printf("%02X %02X %02X %02X %02X %02X %02X %02X\n", read_buf[0]
-
,read_buf[1],read_buf[2],read_buf[3]
-
,read_buf[4],read_buf[5],read_buf[6],read_buf[7]);
-
-
sleep(2);
-
}
-
}
-
-
-
void test_com()
-
{
-
printf("hello\n");
-
-
-
com_init_lower(NULL);
-
com_oper();
-
com_close_lower(NULL);
-
-
}
二,测试结果
阅读(425) | 评论(0) | 转发(0) |