# include <stdio.h>
# include <unistd.h>
# include <stdlib.h>
# include <termios.h>
# include <fcntl.h>
# include <string.h>
# include <sys/time.h>
# include <sys/types.h>
# include <pthread.h>
int fd,i;
pthread_t thread[2];
pthread_mutex_t mut;
/****************************************************************************
结构体说明:传送控制单片机的信息
成员1:select 选择功能模块
成员2:control 控制单片机相应的动作
*****************************************************************************/
struct protocal
{
unsigned char select;
unsigned char control;
};
struct protocal ptr[14]={{0xa1,0x01},{0xa1,0x02},{0xa1,0x03},{0xa1,0x04},{0xa1,0x05},
{0xa1,0x06},{0xa1,0x07},{0xa1,0x08},{0xb2,0x00},{0xb2,0x03},
{0xb2,0x06},{0xb2,0x09},{0xb2,0x0e},{0xc3,0xd4}};
/********************************************************************
功能说明:设置linux串口参数
传入参数:
fd nspeed nbit nevent nstop
文件句柄 波特率 数据位 奇偶校验 停止位
返回值: fd 文件句柄
********************************************************************/
int set_port(int fd ,int nspeed ,int nbits ,char nevent ,int nstop)
{
struct termios newtio,oldtio;
if (tcgetattr(fd,&oldtio)!= 0)
{
perror("setup serial");
return -1;
}
bzero(&newtio,sizeof(newtio));
newtio.c_cflag |= CLOCAL|CREAD;
newtio.c_cflag &= ~CSIZE;
switch (nbits)
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}
switch (nevent)
{
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
switch (nspeed)
{
case 9600:
cfsetispeed(&newtio,B9600);
cfsetospeed(&newtio,B9600);
break;
}
switch (nstop)
{
case 1:
newtio.c_cflag &= ~CSTOPB;
break;
case 2:
newtio.c_cflag |= CSTOPB;
break;
}
newtio.c_cc[VTIME]= 0;
newtio.c_cc[VMIN]= 14;
tcflush(fd,TCIFLUSH);
if ((tcsetattr(fd,TCSANOW,&newtio))!= 0)
{
perror("com set");
return -1;
}
return 0;
}
int open_port(int fd,int comport)
{
if (comport == 1){
fd = open ("/dev/ttyUSB0",O_RDWR);
if ( -1 == fd ){
perror("can't open serial port");
return -1;
}
}
return fd;
}
void write_port(void)
{
int nwrite;
for (i=0;i<14;i++)
{
nwrite = write(fd,&ptr[i],2);
usleep(200000);/*每200ms秒发一次数据*/
}
return;
}
void read_port(void)
{
fd_set rd;
int nread,retval;
unsigned char msg[14];
struct timeval timeout;
FD_ZERO(&rd);
FD_SET(fd,&rd);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
retval = select (fd+1,&rd,NULL,
linux串口测试程序_计算机软件及应用_IT/计算机_专业资料 暂无评价0人阅读0次下载举报文档 linux串口测试程序_计算机软件及应用_IT/计算机_专业资料。...
linux下串口驱动测试程序... 4页 1下载券 linux下串口,多线程测试... 暂无评价 2页 免费 使用USB转串口线烧写单片... 3页 免费喜欢此文档的还喜欢...
口通信 的多线程实现 ui 杜志秀 郑海 昕 北京 110)040 (..12 装备指挥技术学 院 摘要 :介绍 了一种 Lnxiu 下用多线程编程实现嵌入式设备 串口通信 的方法...
这是一个 linux 下串口,多线程测试程序 Version 0.26 ***/ # include <stdio.h> # include <unistd.h> # include <stdlib.h> # include <termios.h>...
linux 下的串口通讯源程序--(测试版)根据前面文章讲到的内容,为了说明问题,下面给出测试程序来理解 linux 下的串口操作流程,例程 receive.c 用来接 收从串口发来...
linux串口编程_计算机软件及应用_IT/计算机_专业...Linux下进程线程开发 进程间通信 多线程编程 嵌入式...(int fd,fd_set *set);测试描述词组set中相关fd...
基于LabWindows/CVI多线程编程的串口测试实现_信息与通信_工程科技_专业资料。为了解决在利用LabWindows/CVI编写程序控制仪器对串口进行测试过程中,采集数据时在程序...
本实例代码经测试有效,拿来给大家分享一下。...第 1 节所述 API 实现一个多线程的串口通信程序... Linux下多线程编程详解 16页 1下载券 VC MFC串口...
linux下dpdk的testpmd测试_计算机软件及应用_IT/计算机_专业资料。linux下dpdk... linux下使用文件来模拟硬... 2页 免费 linux下串口,多线程测试... 暂无...
LINUX下的串口测试的GCC程序__IT/计算机_专业资料。LINUX下的串口测试的GCC程序_#include <stdio.h> /*标准输入输出定义*/ //#include <stdlib.h> /*标准...
热门文档