关于瑞萨的学习就此结束。对于这块强大的开发板,我很庆幸有这个机会能够学习它。在穷其一生的追求路上,这是一个脚印。因为时间和学业还有种种的原因,我只能停止关于瑞萨板的学习,在3个月的学习中,我尽力学习,但是只是学会了一些许的细枝末节的知识。发挥了这块板子不到2%的作用。希望以后还有机会学习这类的板子,它带给我的是人性化的开发体验,以及其强大功能给予我的震撼。我在此提供我学习的成果项目,如有错误或不足还请指出,相互学习,共同进步。并在此感谢胡益华。
本项目,采用压力感应来实现对滴液的检测,并在上位机产生动画,实现提醒的功能。
上位机端
代码如下:
/********************************************************************
** 文 件 名:瑞萨配套
** 创 建 人:yk
** 最后修改时间:2018年8月1日
*********************************************************************/
#include"iostream"
#include"winsock2.h"
#include"stdlib.h"
#include"graphics.h"
#include "time.h"
#include"math.h"
#include"conio.h"
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"Winmm.lib")
using namespace std;
time_t rawtime;
class tcpsocket //定义tcpsocket类型函数集
{
public: //定义各类函数
unsigned int establish(); //创建socket
unsigned int Connect(const char *cp, u_short hostshortb); //连接服务端
char * communication(); //接受数据
void cls();
private: //定义变量
WSADATA date;
SOCKET tcpclient;
struct sockaddr_in adress;
WORD sockVersion = MAKEWORD(2, 2);
};
unsigned int tcpsocket::establish() //socket函数(客户端)
{
if (WSAStartup(sockVersion, &date) != 0) //初始化
{
return WSAGetLastError();
}
tcpclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //创建客户端
if (tcpclient == INVALID_SOCKET)
{
return WSAGetLastError();
}
return 0;
}
unsigned int tcpsocket::Connect(const char *IP, u_short hostshortb)//连接服务端
{
int i = 0;
xc:adress.sin_family= AF_INET;
adress.sin_port= htons(hostshortb);
adress.sin_addr.S_un.S_addr = inet_addr(IP);
if (connect(tcpclient, (sockaddr *)&adress, sizeof(adress)) != SOCKET_ERROR) //创建连接
{
i++;
Sleep(1000);
if (i > 10)
{
closesocket(tcpclient);
return 1;
}
else goto xc;
}
return 0;
}
char * tcpsocket::communication() //客户端接收信息
{
char recever[10];
recv(tcpclient, recever, 10, 0);
return recever;
}
void tcpsocket::cls() //关闭客户端
{
closesocket(tcpclient);
WSACleanup();
}
class write //定义write类型函数集
{
public:
int write1( float j);
int write2(int i);
int write3(char * t1);
int write4(char * t2);
private:
FILE * fp;
};
int write::write1( float j) //定义函数write1
{
if (!(fp = fopen(".\\information.txt", "a"))) //文件成功打开则将返回的指针赋值给指针标量fp
{
return 1;
}
fprintf(fp, "输液量估计值%f,", j); //向指定文件中输出fp
fclose(fp); //关闭fp
return 0;
}
int write::write2(int i) //定义函数write2
{
if (!(fp = fopen(".\\information.txt", "a"))) //文件成功打开则将返回的指针赋值给指针标量fp
{
return 1;
}
fprintf(fp, "第%d个病人,", i); //向指定文件中输出fp
fclose(fp); //关闭fp
return 0;
}
int write::write3(char * t1) //定义函数write3
{
if (!(fp = fopen(".\\information.txt", "a"))) //文件成功打开则将返回的指针赋值给指针标量fp
{
return 1;
}
fprintf(fp, "开始时间%s,", t1); //向指定文件中输出fp
fclose(fp); //关闭fp
return 0;
}
int write::write4(char * t2) //定义函数write4
{
if (!(fp = fopen(".\\information.txt", "a"))) //文件成功打开则将返回的指针赋值给指针标量fp
{
return 1;
}
fprintf(fp, "结束时间%s\n", t2); //向指定文件中输出fp
fclose(fp); //关闭fp
return 0;
}
int Fabs(int a) //定义函数Fabs,计算绝对值
{
return a >= 0 ? a : -a;
}
int readwight()
{
cc:FILE *fp;
char pf[3];
char as[] = "80";
int wight=0;
if (!(fp = fopen(".\\wight.dat", "r"))) //文件成功打开则将返回的指针赋值给指针标量fp
{
fclose(fp);
goto cc;
}
fscanf(fp, "%s",pf);
fclose(fp);
wight = atoi(pf);
return wight;
}
void picturestart(int mode) //定义函数picturestart,绘制瓶子
{
initgraph(640, 480); //初始化一个大小为640*480的图形区域
setorigin(320, 240); //初始化原点坐标。
setbkcolor(0x555555); //设置背景颜色
cleardevice(); //清屏
settextcolor(RGB(153, 153, 153)); //设置字体颜色
settextstyle(25, 25, "黑体"); //设置字体
outtextxy(-310, -240, "DOB"); //在当前光标x=-310,y=-240处输入DOB
settextstyle(15, 15, "宋体"); //设置字体
outtextxy(-320, -210, "状态");
outtextxy(-320, -150, "模式");
if(mode==50)outtextxy(-320, -130, "100ml");
else if (mode == 1966)outtextxy(-320, -130, "250ml");
else if (mode == 150)outtextxy(-320, -130, "500ml");
setlinecolor(RGB(195, 195, 195)); //设置线条颜色
setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 20); //设置线条样式为宽度为10,端点为平的实线
setfillcolor(RGB(37, 170, 226)); //设置矩形填充色
fillroundrect(-210, 200, 210, -200, 100, 100); //绘制一个可填充的带圆角的矩形x=-240,240y=210,210
line(-210, -200, 210, -200); //绘制一条起点为(-240,-200)终点为(240,200)的线
line(-210, -210, -210, 100); //绘制一条起点为(-240,-210)终点为(-240,210)的线
line(210, -210, 210, 100); //绘制一条起点为(240,-210)终点为(240,100)的线
setlinecolor(RGB(37, 170, 226)); //设置线条的颜色
line(-200, -200, 200, -200); //绘制一条起点为(-230,-200)终点为(230,-200)的线
line(-200, -195, 200, -195); //绘制一条起点为(-230,-195)终点为(230,-195)的线
line(-190, -210, -190, 100); //绘制一条起点为(-220,-210)终点为(-220,100)的线
line(190, -210, 190, 100); //绘制一条起点为(220,-210)终点为(220,100)的线
}
int flowup(int line1, int line_ok) //定义函数flowup,绘制未输入的液体对应的矩形区域
{
int NUM_ber = Fabs(line1 - line_ok);
setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 1); //设置线条样式为宽度为1,端点为平的实线
setlinecolor(RGB(37, 170, 226)); //设置线条颜色
for (int i = 0; i < NUM_ber; i++)
{
line(-200, line_ok, 200, line_ok); //绘制一条起点为(-230,line_ok)终点为(230,line_ok)的线
line_ok -= 1;
Sleep(10); //延迟10s
}
return line_ok;
}
int flowdown(int line1, int line_ok) //定义函数flowdown,绘制已输入的液体对应的矩形区域
{
int NUM_ber = Fabs(line1 - line_ok);
setlinestyle(PS_SOLID | PS_ENDCAP_FLAT, 1); //设置线条样式为宽度为1,端点为平的实线
setlinecolor(0x555555); //设置线条颜色(已输入的液体)
for (int i = 0; i < NUM_ber; i++)
{
line(-200, line_ok, 200, line_ok); //绘制一条起点为(-230,line_ok)终点为(230,line_ok)的线
line_ok += 1;
Sleep(10); //延迟10s
}
return line_ok;
}
int obtain(const char *ip, u_short hostshorta) //定义函数obtain,当socket未连接成功时显示的内容
{
int err = 0;
tcpsocket dob;
char *buf;
int wight = 0;
write DOB; //定义write类型函数的对象DOB
if (dob.establish()) //创建客户端失败
{
setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 1); //画一条宽度为1的实心斜线
settextstyle(15, 15, "楷体"); //设置字体类型为宽度15,高度15,楷体
settextcolor(WHITE); //设置字体颜色为白色
char str[] = " 发生了错误,SOCKET未能成";
char str1[] = "功启用,请重启程序与设备,";
char str2[] = "并检查ip.dat文件和port.da";
char str3[] = "t文件是否正确! ";
outtextxy(-300, 0, str); //在x=-300,y=0处显示str
outtextxy(-300, 25, str1); //在x=-300,y=25处显示str1
outtextxy(-300, 50, str2); //在x=-300,y=50处显示str2
outtextxy(-300, 75, str3); //在x=-300,y=75处显示str3
Sleep(1000);
exit(0);
}
if (dob.Connect(ip, hostshorta)) //服务端连接失败
{
setlinestyle(PS_SOLID | PS_JOIN_BEVEL, 1); //画一条宽度为1的实心斜线
settextstyle(15, 15, "楷体"); //设置字体类型为宽度15,高度15,楷体
settextcolor(WHITE); //设置字体颜色为白色
char str[] = " 发生了错误,SOCKET未能成";
char str1[] = "功连接,请重启程序与设备,";
char str2[] = "并检查ip.dat文件和port.da";
char str3[] = "t文件是否正确! ";
outtextxy(-300, 0, str); //在x=-300,y=0处显示str
outtextxy(-300, 25, str1); //在x=-300,y=25处显示str1
outtextxy(-300, 50, str2); //在x=-300,y=50处显示str2
outtextxy(-300, 75, str3); //在x=-300,y=75处显示str3
Sleep(1000);
exit(0);
}
buf = dob.communication(); //将接收的信息传递给buf
wight = atoi(buf); //将buf由string型转换为int型,并赋值给wight
dob.cls(); //关闭客户端
return wight;
}
int deal() //定义函数deal,检测液滴量估计值
{
FILE *fp, *pf; //声明FILE类型的,名为fp,pf的指针
const char *ip = NULL; //声明char型指针
char str1[20];
char port1[10];
int date = 0;
u_short hostshorta;
fp = fopen(".\\ip.dat", "r"); //若将ip.dat文件以只读的方式成功打开,则将返回的文件指针赋值给fp
pf = fopen(".\\port.dat", "r"); //若将port.dat文件以只读的方式成功打开,则将返回的文件指针赋值给pf
fscanf(fp, "%s", str1); //从文件指针fp中读取字符串,并赋值给str
fscanf(pf, "%s", port1); //从文件指针pf中读取字符串,并赋值给port
fclose(fp); //关闭fp
fclose(pf); //关闭pf
ip = str1; //将str赋值给ip
hostshorta = atoi(port1); //将port内的字符串转换为整数,并将返回值赋值给hostshorta
date = obtain(ip, hostshorta);
return date; //返回液滴量估计值
}
int proportiondeal(double proportion) //定义函数proportiondeal,计算在液体剩余的百分比时对应的高度
{
int line_number = 400;
return (int)(190 - proportion * line_number); //返回整型
}
int linedeal(int line_define_number, double proportion) //定义函数linedeal,显示输液过程中矩形图像的动态变化
{
int line_number = proportiondeal(proportion); //190-
if (line_number > line_define_number) //输液过程中
{
line_define_number = flowdown(line_number, line_define_number); //记录水位当前值
}
else if (line_number == line_define_number) //开始输液时
{
Sleep(100); //延迟100s
}
else if (line_number< line_define_number)
{
line_define_number=flowup(line_number, line_define_number);
}
line_define_number = line_number;
return line_define_number;
}
int start()
{
initgraph(640, 480); //初始化一个大小为640*480的图形区域
setbkcolor(RGB(76, 115, 158)); //设置背景颜色
cleardevice(); //清屏
settextcolor(RGB(52, 60, 63));
settextstyle(100, 100, "黑体");
outtextxy(2, 1, "DOB");
settextcolor(RGB(222, 204, 106));
settextstyle(50, 50, "宋体");
outtextxy(1, 120, "A.100ML");
outtextxy(1, 190, "B.250ML");
outtextxy(1, 260, "C.500ML");
settextcolor(RGB(0, 0, 0));
settextstyle(18, 18, "楷体");
outtextxy(1, 340, "[请用左键单击选择或者键入字母并回车]");
settextcolor(RGB(255, 10, 10));
settextstyle(30, 30, "幼圆");
outtextxy(340, 1, "【退出】");
while (1)
{
mouse:MOUSEMSG m;
char input;
if (MouseHit())
{
m = GetMouseMsg();
if (m.uMsg == WM_LBUTTONUP)
{
if ((m.y >= 120 && m.y <= 170) && (m.x >= 1 && m.x <= 350))
{
return 1163;
}
if ((m.y >= 190 && m.y <= 240) && (m.x >= 1 && m.x <= 350))
{
return 1966;
}
if ((m.y >= 260 && m.y <= 310) && (m.x >= 1 && m.x <= 350))
{
return 150;
}
if ((m.y >= 1 && m.y <= 30) && (m.x >= 370 && m.x <= 550))
{
exit(0);
}
goto mouse;
}
}
else if (kbhit())
{
input = _getch();
if (input == 'a' || input == 'A')
{
return 50;
}
if (input == 'b' || input == 'B')
{
return 100;
}
if (input == 'c' || input == 'C')
{
return 150;
}
goto mouse;
}
}
}
int control(int pol) //定义函数control ,显示输液的动态变化过程
{
write DOB; //定义write类型函数的对象DOB
float proportion = 0;
int line_define_number = -210;
int panduan = 0;
float nn = 0;
int count = 1;
int tim = 0;
int byte = 0;
int head = 0,head1=0;
int date = 0;
char s[4] = {0};
while (1)
{
again:date = deal()-pol+42; //记录液滴量估计值
if ((Fabs(head1 - date) <= 78)||head1==0||panduan>2)
{
panduan = 0;
if (head == 0 || date > head) //开始输液时
{
if(tim==0)
{
DOB.write2(count); //记录第几个病人
time(&rawtime); //记录秒数
DOB.write3(ctime(&rawtime)); //记录开始的时间(事件被转换为字符串)
}
tim++;
count++;
head = date;
head1 = date;
line_define_number = -210;
//sprintf(s, "%.3s", "99%");
outtextxy(-310, -185, "99%");
line_define_number = flowup(-210, line_define_number); //记录水位当前值
}
else if (head1 != 0 && date <= head) //输液过程中
{
head1 = date;
DOB.write1(date);
proportion = (float)date / (float)head; //计算液体剩余量的百分比
nn = proportion * 100.0;
byte = (int)nn;
settextcolor(RGB(153, 153, 153));
settextstyle(15, 15, "宋体");
sprintf(s, "%f",nn );
//s[0]=(int)nn % 10;
//s[1]=((int)nn-(int)nn % 10)/10;
s[2] = '%';
s[3] = '\0';
outtextxy(-310, -185, s);
if (proportion < 0.02 || date < 100) //当百分比小于20%或液体质量小于30g时
{
line_define_number=flowdown(190, line_define_number);
for(int i=0;i<15;i++)
{
mciSendString(_T("close bmusic"), NULL, 0, NULL);
mciSendString(_T("open G:\\素材\\飞机大战图片音乐素材\\gotEnemy.mp3 alias bmusic"), NULL, 0, NULL);
mciSendString(_T("play bmusic"), NULL, 0, NULL);
Sleep(1000);
};
time(&rawtime); //记录秒数
DOB.write4(ctime(&rawtime)); //记录结束时间
Sleep(2000);
s[0] = 0;
s[1] = 0;
s[2] = 0;
s[3] = 0;
//return 0;
int mode = 0;
mode = start();
picturestart(mode);
control(mode);
}
line_define_number = linedeal(line_define_number, proportion); //记录结束时的水位当前值
}
}
else if ((Fabs(head1 - date) >= 78))
{
panduan++;
goto again;
}
}
return 1;
}
int main() //主函数
{
while(1)
{
int mode = 0;
mode = start();
picturestart(mode); //展示图片
control(mode); //显示输液的动态变化过程
}
//system("pause");
}
硬件端:
为保证工程的完整性,我将整个工程上传。这里将不在进行代码展示。