📄 blue.c
字号:
/*****************************************************************************
;Institue of Automation, Chinese Academy of Sciences
;www.emsyschina.com
;File Name: 基于蓝牙的串口数据文件传输程序 1.2
;
;Descrjz_IPtion: 使用AT9200上USART0通过蓝牙模块
; 向目标设备发送数据
;Date: 2007-10-31
;Author: 王亮
;E_mail: wangl@emsyschina.com
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
typedef unsigned char U8;
typedef unsigned short U16;
typedef unsigned long U32;
#define BUFSIZE 512
// *******************************************************************************
// 函数名: setTermios
// 功能描述:串口工作模式设置
// *******************************************************************************
void setTermios(struct termios * pNewtio, U16 uBaudRate)
{
bzero(pNewtio, sizeof(struct termios)); // clear struct for new port settings
// 8M1
// 波特率9600 数据位8 接收使能 本地连接
pNewtio->c_cflag = uBaudRate | CS8 | CREAD | CLOCAL ;
pNewtio->c_iflag = 0;
pNewtio->c_oflag = 0;
pNewtio->c_lflag = 0; //non ICANON
// initialize all control characters
// default values can be found in /usr/include/termios.h, and
// are given in the comments, but we don't need them here
pNewtio->c_cc[VINTR] = 0; // Ctrl-c
pNewtio->c_cc[VQUIT] = 0; // Ctrl-\
pNewtio->c_cc[VERASE] = 0; // del
pNewtio->c_cc[VKILL] = 0; // @
pNewtio->c_cc[VEOF] = 4; // Ctrl-d
pNewtio->c_cc[VTIME] = 3; // inter-character timer, timeout VTIME*0.1
pNewtio->c_cc[VMIN] = 40; // blocking read until VMIN character arrives
pNewtio->c_cc[VSWTC] = 0; // '\0'
pNewtio->c_cc[VSTART] = 0; // Ctrl-q
pNewtio->c_cc[VSTOP] = 0; // Ctrl-s
pNewtio->c_cc[VSUSP] = 0; // Ctrl-z
pNewtio->c_cc[VEOL] = 0; // '\0'
pNewtio->c_cc[VREPRINT] = 0; // Ctrl-r
pNewtio->c_cc[VDISCARD] = 0; // Ctrl-u
pNewtio->c_cc[VWERASE] = 0; // Ctrl-w
pNewtio->c_cc[VLNEXT] = 0; // Ctrl-v
pNewtio->c_cc[VEOL2] = 0; // '\0'
}
// *******************************************************************************
// 函数名: delay_m
// 功能描述:延时函数
// *******************************************************************************
void delay_m(unsigned int time)
{
unsigned int i;
while(time--)
{
i=1000;
while(--i);
}
}
// *******************************************************************************
// 函数名: main
// 功能描述:主函数
// *******************************************************************************
int main(void)
{
FILE *fp; // 指向数据文件的指针
int fd,nread;
struct termios oldtio,newtio;
char *dev ="/dev/ttyS3";
char commnt[128];
// ***************串口提示信息*******************
char command1[64] = "waiting command!!!!\n";
char command2[64] = "command right!!!!\n";
char command3[64] = "command error !!!!\n";
char command4[64] = "exit progranmer\n";
// **********************************************
char *argv,*p,flag;
char buff[BUFSIZE],str[4];
struct timeval tv;
fd_set rfds;
argv = commnt; // 将缓冲字符串的首地址赋值给指针argv
// 打开AT9200串口2---ttyS3
if ( (fd = open(dev,O_RDWR | O_NOCTTY ))<0)
{
printf("Can't Open Serial Port!\n");
return -1;
}
tcgetattr(fd,&oldtio); // 保存当前串口工作模式
setTermios(&newtio,B115200); // 设置串口波特率为115200
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
tv.tv_sec = 20; // 串口接收数据等待时间 S
tv.tv_usec = 0; // 串口接收数据等待时间 uS
while(1)
{
tv.tv_sec = 20; // 串口接收数据等待时间 S
tv.tv_usec = 0; // 串口接收数据等待时间 uS
flag = 0; // 清零发送文件标志位
p = buff;
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
write(fd,&command1,strlen(command1)); // 串口提示信息 "waiting command!!!!"
// ************等待接收控制指令*************
if (select(1+fd,&rfds,NULL,NULL,&tv)>0)
{
if(FD_ISSET(fd,&rfds))
{
nread=read(fd, buff,BUFSIZE);
buff[nread]='\0';
strcpy(str,p);
if(strcmp(str,"OK") == 0) // 判断控制指令是否为 OK
{
printf("%s\n",p);
write(fd,&command2,strlen(command2)); // 串口提示信息 "command right!!!!"
printf("data send start!!!!\n");
flag = 1; // 置位发送文件标志位
}
if(strcmp(str,"Q") == 0) // 判断控制指令是否为 Q
{
// *********指令 Q 表示退出程序***********
printf("%s\n",p);
write(fd,&command2,strlen(command2)); // 串口提示信息 "command right!!!!"
write(fd,&command4,strlen(command4)); // 串口提示信息 "exit progranmer"
sleep(1);
tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
return 0;
}
if(flag == 0) // 接收到错误的返回信息
// *********命令不正确***********
write(fd,&command3,strlen(command3)); // 串口提示信息 "command error !!!!"
}
}
// *************根据标志位判断是否发送文件***************
if(flag == 1)
{
// 打开准备通过串口发送的文件
if((fp=fopen("ipconf.txt","r"))==NULL)
{
printf(" Cannot open the file \n");
return 0;
}
// ****************数据传输************************
while(!feof(fp)) // 判断是否到达文件末尾
{
// 从文件得到一个字符赋值给缓冲字符串
*commnt = getc(fp);
// putchar(*commnt); // 在终端打印出来 核实数据(可以不要)
write(fd, argv,1); // 将得到的字符通过串口发送出去
delay_m(3);
}
// ************************************************
fclose(fp);
}
// *******************************************************
}
tcsetattr(fd,TCSANOW,&oldtio);
close(fd);
fclose(fp);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -