资源描述
软件综合设计报告
东华大学信息科学与技术学院
目录
一、 设计目的........................................1
二、 实验环境........................................1
三、设计内容及要求..................................1
四、设计思想及原理..................................1
五、实验内容与源程序................................1
六、遇到的问题及其解决..............................14
七、收获与体会......................................14
一、设计目的
通过本课程设计实习,使学生在下列方面有所了解和提高:
1、 掌握Visual Basic进行程序设计的基本思路和方法
2、 能利用Visual Basic编程实现简单的任务
3、 结合控制系统理论用VB进行计算机控制仿真
二、实验环境
运行Microsoft Visual Studio 2010和Microsoft SQL Server 2012的微机。
三、设计内容及要求
1、 实现各种函数发生器并显示
1) 基本要求:实现下列函数发生器,正弦函数、方波函数、锯齿函数、三角函数,函数的生成参数都可以通过菜单设定,并且将生成的函数以图形方式实时显示在窗口中。
2) 附加功能:将系统的时间响应数据保存到数据库中,具体应包括下列属性:时间,输出值。将系统的历史响应重现。使用Teechart控件作为显示输出。
四、设计思想及原理
先通过函数表达式的方式生成一个幅值为1,频率为1Hz的标准函数,调节函数的生成参数即调节幅值和频率时,使对相应的标准函数幅值和频率乘以相应的比例即可得到结果。
根据题目要求为了实现函数发生器各种要求,可采用多种方式和多种程序语言设计实现。本次设计我主要使用C#语言在ASP.net基础上编写,网页发布后可以让用户远程使用,是网络化的一次尝试,另外使用SQL Server 2012数据库存储实时数据,并能够实现数据查询等附加功能。
五、实验内容与源程序
一、 功能模块分析
根据题目要求需要五个模块,分别是初始化、函数生成参数设定、生成函数实时显示、实时数据查询、Chart图显示。函数生成参数设定模块主要设置生成函数类型、幅值和频率参数;生成函数实时显示主要根据参数设置生成对应的函数并保存到数据库中;实时数据查询主要实现从数据库查询并显示出来;Chart图主要实现从数据库查询坐标点并在Chart表上显示,将系统的历史响应重现;初始化主要将参数设置初始化以及数据库清空等功能。
综上所述,本次一共需要五个菜单,即两个主要网页(Function.aspx和ChartShow.aspx),分别实现初始化、函数生成参数设定、生成函数实时显示、实时数据查询、Chart图显示等功能。
二、 源代码及运行结果分析
(一)、初始化模块:
1.源代码
protected void Button2_Click(object sender, EventArgs e)
{
Timer1.Enabled = false;
//初始化参数设置菜单
TextBox10.Text = "";
TextBox6.Text = "";
DropDownList1.SelectedValue = "选择函数类型";
//为防止数据库重复出错,初始化清空数据库内容
//从Web.Config文件提取连接字符串
ConnectionStringSettings Settings;//定义连接字符串变量Settings
Settings =
ConfigurationManager.ConnectionStrings["DhuPID_WebConnectionString2"];
SqlConnection cn = new SqlConnection(Settings.ConnectionString);
//声明和创建SqlConnection(建立与数据库的连接)对象
SqlCommand cmd = new SqlCommand();//对数据源执行命令
cn.Open();//打开数据库连接
//CommandText说明对数据源执行的SQL语句和存储过程名
cmd.CommandText = "delete from T_Function where FunType='1' or FunType='2' or
FunType='3' or FunType='4'";
cmd.Connection = cn;//定义cmd所使用的数据库连接为cn
cmd.ExecuteNonQuery();//执行CommandText的制定的操作
cn.Close();//关闭与数据库连接
GridView2.Visible = false;//为了界面美观,无数据的GridView不可见
}
2. 运行结果
图1 初始化界面
解释:和一般网页类似,红星代表必填项,函数类型下拉选择,默认为正弦函数,在这里可以设置函数发生器的幅值和频率。
(二)、 函数生成参数设定模块
1. 源代码
见下一页生成函数实时显示源程序。
2. 运行结果
图2 初始化界面进行参数设置
(三)、生成函数实时显示
1.源代码
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
string TxtAmplitude = TextBox10.Text.Trim();//幅值
amplitude = float.Parse(TxtAmplitude);
string TxtFrequency = TextBox6.Text.Trim();//频率
frequency = float.Parse(TxtFrequency);
string TxtVary = DropDownList1.SelectedValue;//函数类型
string str0 = "选择函数类型";
string str1 = "正弦函数";
string str2 = "方波函数";
string str3 = "锯齿函数";
string str4 = "三角函数";
int CompareType;
CompareType = String.Compare(TxtVary, str0, false);//默认为正弦波
if (CompareType == 0)
Type = 1;
CompareType = String.Compare(TxtVary, str1, false);
if (CompareType==0)
Type = 1;
CompareType = String.Compare(TxtVary, str2, false);
if (CompareType == 0)
Type = 2;
CompareType = String.Compare(TxtVary, str3, false);
if (CompareType == 0)
Type = 3;
CompareType = String.Compare(TxtVary, str4, false);
if (CompareType == 0)
Type = 4;
Bitmap img = new Bitmap(1000, 1000);//创建Bitmap对象,像素数据定义
MemoryStream stream = draw();//IO.MemoryStream创建其支持存储区为内存的流
img.Save(stream, ImageFormat.Jpeg);//保存绘制的图片
Response.Clear();//清除缓冲区流中的所有内容输出
Response.ContentType = "image/jpeg";//设置或获取输出流HTTP MIME类型
Response.BinaryWrite(stream.ToArray());//将一个二进制字符串写入HTTP输出流
} //调用,创建其支持存储区为内存的流
public MemoryStream draw()
{
Bitmap img = new Bitmap(1000, 1000);//创建Bitmap对象
//class System.Drawing.Graphics 封装一个GDI+绘图图面
Graphics g = Graphics.FromImage(img);//创建Graphics对象
//class System.Drawing.Pen定义用于绘制直线和曲线的对象
Pen Bp = new Pen(Color.Black); //定义黑色画笔
Pen Rp = new Pen(Color.Red);//红色画笔
Pen Sp = new Pen(Color.Blue);//蓝色
//System.Drawing.Drawing2D.AdjustableArrowCap 表示可调整的箭头形状的线帽
AdjustableArrowCap aac; //定义箭头帽
aac = new System.Drawing.Drawing2D.AdjustableArrowCap(4, 6);
// CustomLineCap Pen.CustomStartCap 此Pen绘制的直线起点使用的自定义线帽
Sp.CustomStartCap = aac; //开始端箭头帽
//Drawing.Font 定义特定的文本格式,包括字体、字号和字形属性
Font Bfont = new Font("Arial", 12, FontStyle.Bold);//大标题字体
Font font = new Font("Arial", 6);//一般字
Font Tfont = new Font("Arial", 9);//较大字体
//DrawRectangle(Pen pen,int x,int y,int width,int height)
// 绘制由坐标对、宽度和高度指定的矩形
G.DrawRectangle(new Pen(Color.White, 1200),0,0,img.Width+100,img.Height+100);
//LinearGradientBrush.LinearGradientBrush(Rectangle rect,Color color1,Color
//color2,float angle,bool isAngleScaleable)
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0,
img.Width, img.Height),Color.Black, Color.Black,1.2F, true);//黑色过度型笔刷
LinearGradientBrush Bluebrush = new LinearGradientBrush(new Rectangle(0, 0,
img.Width, img.Height), Color.Blue, Color.Blue, 1.2F, true);//蓝色过渡型笔刷
//void Graphics.DrawString(String s,Font font,Brush brush,float x,float y)
//在指定位置并且用指定的Brush和Font对象绘制指定的文本字符串
g.DrawString("函数发生器", Bfont, brush, 500, 100); //绘制大标题
//void Graphics.DrawRectangle(Pen pen,int x,int y,int width,int height)
g.DrawRectangle(Bp, 0, 0, img.Width, img.Height - 40);
//横坐标轴
g.DrawLine(Sp, 980, 500, 100, 500);
//横坐标数字
g.DrawString("1", font, brush, 220, 515);
g.DrawString("2", font, brush, 340, 515);
g.DrawString("3", font, brush, 460, 515);
g.DrawString("4", font, brush, 580, 515);
g.DrawString("5", font, brush, 700, 515);
g.DrawString("6", font, brush, 820, 515);
//void Graphics.DrawLine(Pen pen,int x1,int y1,int x2,int y2)绘制一条连接由
//坐标对指定的两个点的线条
//横轴标识线
g.DrawLine(Bp, 220, 500, 220, 510);
g.DrawLine(Bp, 340, 500, 340, 510);
g.DrawLine(Bp, 460, 500, 460, 510);
g.DrawLine(Bp, 580, 500, 580, 510);
g.DrawLine(Bp, 700, 500, 700, 510);
g.DrawLine(Bp, 820, 500, 820, 510);
//纵坐标轴
g.DrawLine(Sp, 100, 90, 100, 850);
//纵坐标数字
g.DrawString("0.5", font, brush, 115, 475);
g.DrawString("1.0", font, brush, 115, 450);
g.DrawString("1.5", font, brush, 115, 425);
g.DrawString("2.0", font, brush, 115, 400);
g.DrawString("2.5", font, brush, 115, 375);
g.DrawString("3.0", font, brush, 115, 350);
g.DrawString("-0.5", font, brush, 115, 525);
g.DrawString("-1.0", font, brush, 115, 550);
g.DrawString("-1.5", font, brush, 115, 575);
g.DrawString("-2.0", font, brush, 115, 600);
g.DrawString("-2.5", font, brush, 115, 625);
g.DrawString("-3.0", font, brush, 115, 625);
//纵轴标识线
g.DrawLine(Bp, 100, 475, 110, 475);
g.DrawLine(Bp, 100, 450, 110, 450);
g.DrawLine(Bp, 100, 425, 110, 425);
g.DrawLine(Bp, 100, 400, 110, 400);
g.DrawLine(Bp, 100, 375, 110, 375);
g.DrawLine(Bp, 100, 350, 110, 350);
g.DrawLine(Bp, 100, 525, 110, 525);
g.DrawLine(Bp, 100, 550, 110, 550);
g.DrawLine(Bp, 100, 575, 110, 575);
g.DrawLine(Bp, 100, 600, 110, 600);
g.DrawLine(Bp, 100, 625, 110, 625);
g.DrawLine(Bp, 100, 625, 110, 625);
ConnectionStringSettings Settings;
Settings =
ConfigurationManager.ConnectionStrings["DhuPID_WebConnectionString2"];
/***************正弦波发生函数*************************/
if (Type == 1)
{
Write11 = 0; Write12 = 0;
for (double i = 0; i <= 15; )
{
PointX_1 = (float)i;
PointY_1 = (float)((Math.Sin(frequency * i * 3.14)) * amplitude);
Write11 = (100 + 60 * i);
Write12 = 500 - 50 * (Math.Sin(frequency * i*3.14)) * amplitude;
Write21 = (100 + 60 * (i + 0.01));
Write22 = 500 - 50 * (Math.Sin(frequency * (i+0.01)*3.14 )) * amplitude;
g.DrawLine(Bp,(float)Write11,(float)Write12,(float)Write21,(float)Write22);
i = i + 0.01;
/***********时间响应数据保存到数据库*****************************/
SqlConnection cn1 = new SqlConnection(Settings.ConnectionString);
SqlCommand cmd1 = new SqlCommand();
cn1.Open();
cmd1.CommandText="insert into T_Function(FunType,Amplitude,Frequency,PointX,
PointY)values('1 ','" + amplitude + "','" + frequency + "','" + PointX_1 + "','"
+ PointY_1 + "')";
cmd1.Connection = cn1;
cmd1.ExecuteNonQuery();
cn1.Close();
}
}
/***************方波发生函数*************************/
if (Type == 2)
{
for (double i = 0; i <= 60; )
{
PointX_1 = (float)(i / frequency);
PointY_1 = (-amplitude);
Write11 = (100 + 60 * i /frequency);
Write12 = 500 - 50 * amplitude;
Write21 = (100 + 60 * (i + 1) / frequency);
Write22 = 500 - 50 * amplitude;
g.DrawLine(Bp,(float)Write11,(float)Write12,(float)Write21,(float)Write22);
Write11 = (100 + 60 * (i + 1) / frequency);
Write12 = 500 - 50 * amplitude;
Write21 = (100 + 60 * (i + 1) / frequency);
Write22 = 500 + 50 * amplitude;
g.DrawLine(Bp, (float)Write11, (float)Write12, (float)Write21, (float)Write22);
Write11 = (100 + 60 * (i + 1) / frequency);
Write12 = 500 + 50 * amplitude;
Write21 = (100 + 60 * (i + 2) / frequency);
Write22 = 500 + 50 * amplitude;
g.DrawLine(Bp, (float)Write11, (float)Write12, (float)Write21, (float)Write22);
Write11 = (100 + 60 * (i + 2) / frequency);
Write12 = 500 + 50 * amplitude;
Write21 = (100 + 60 * (i + 2) / frequency);
Write22 = 500 - 50 * amplitude;
g.DrawLine(Bp, (float)Write11, (float)Write12, (float)Write21, (float)Write22);
/**************时间响应数据保存到数据库*******************/
SqlConnection cn4 = new SqlConnection(Settings.ConnectionString);
SqlCommand cmd4 = new SqlCommand();
cn4.Open();
cmd4.CommandText = "insert into T_Function(FunType,Amplitude,Frequency,
PointX,PointY)values('2','" + amplitude + "','" + frequency + "',
'" + PointX_1 + "','" + PointY_1 + "')";
cmd4.Connection = cn4;
cmd4.ExecuteNonQuery();
cn4.Close();
i = i + 2;
}
}
/***************锯齿波发生函数*************************/
if (Type == 3)
{
for (double i = 0; i <= 60; )
{
PointX_1=(float)( i * 2 /frequency);
PointY_1 = (- amplitude);
Write11 = (100 + 60 * i * 2 /frequency);
Write12 = 500 + 50 * amplitude;
Write21 = (100 + 60 * (i + 1) * 2 /frequency);
Write22 = 500 - 50 * amplitude;
g.DrawLine(Bp, (float)Write11, (float)Write12, (float)Write21, (float)Write22);
/*****************时间响应数据保存到数据库*** ******************/
SqlConnection cn1 = new SqlConnection(Settings.ConnectionString);
SqlCommand cmd1 = new SqlCommand();
cn1.Open();
cmd1.CommandText = "insert into T_Function(FunType,Amplitude,Frequency,
PointX,PointY)values('3','" + amplitude + "','" + frequency + "',
'" + PointX_1 + "','" + PointY_1 + "')";
cmd1.Connection = cn1;
cmd1.ExecuteNonQuery();
cn1.Close();
PointX_1 = (float)((i+1) * 2 / frequency);
PointY_1 = amplitude;
Write11 = (100 + 60 * (i + 1) *2/ frequency);
Write12 = 500 -50 * amplitude;
Write21 = (100 + 60 * (i + 1) * 2/ frequency);
Write22 = 500 + 50 * amplitude;
g.DrawLine(Bp, (float)Write11, (float)Write12, (float)Write21, (float)Write22);
i = i + 1;
/*************时间响应数据保存到数据库********************/
SqlConnection cn2 = new SqlConnection(Settings.ConnectionString);
SqlCommand cmd2 = new SqlCommand();
cn2.Open();
cmd2.CommandText = "insert into T_Function(FunType,Amplitude,Frequency,
PointX,PointY)values('3','" + amplitude + "','" + frequency + "',
'" + PointX_1 + "','" + PointY_1 + "')";
cmd2.Connection = cn2;
cmd2.ExecuteNonQuery();
cn2.Close();
}
}
/***************三角波发生函数*************************/
if (Type == 4)
{
for (int i = 0; i <= 60; )
{
PointX_1 = (float)(i / frequency);
PointY_1 = amplitude;
Write11 = (100 + 60 * i /frequency);
Write12 = 500 + 50 * amplitude;
Write21 = (100 + 60 * (i + 1) / frequency);
Write22 = 500 - 50 * amplitude;
g.DrawLine(Bp, (float)Write11, (float)Write12, (float)Write21, (float)Write22);
/********时间响应数据保存到数据库*****************/
SqlConnection cn1 = new SqlConnection(Settings.ConnectionString);
SqlCommand cmd1 = new SqlCommand();
cn1.Open();
cmd1.CommandText = "insert into T_Funct
展开阅读全文