求做一个web页面控*务器串口,可用Mscomm控件做!满意会继续最佳。htm页面都能操作本地串口。
发布网友
发布时间:2022-04-23 19:58
我来回答
共2个回答
热心网友
时间:2023-10-07 00:10
看了你的留言直接在这回吧,其实网页方面我也是个菜鸟。去年我做过一个类似的,当时也是自学了1个月的iis的配置,asp.net,vb串口操作等,大二假期比较闲。
做法:用DS18B20温度传感器连接51单片机,然后单片机通过串口与电脑通信,电脑成为服务器,手机连接网页,进行温度监控,开关单片机外接的继电器等等操作。
硬件端的代码对你没用,服务器端我的基本思路就是asp.net连接数据库,然后用vb实时扫描数据库,通过vb中的MScomm控件连接串口,vb的代码对你也没啥用,因为MScomm连接单片机的代码的“通信协议”我是写在单片机上的,不配套就没用了
这是网页的代码,不精简,高手莫笑...
主要功能就是连接数据库。。。里面没能直接通过asp.net控制串口,貌似也有C#直接控制的,你去CSDN看看吧,我试过是没问题的,就是操作不太方便(可能我太菜了)
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//温度
SqlConnection cnn1 = new SqlConnection(@"Data Source=YINGJHSHP541\SQLEXPRESS;Initial Catalog=test3;User ID=sa;Password=yingjh");
cnn1.Open();
//表
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = cnn1;
cmd1.CommandText = "select * from T";
SqlDataReader data1;
data1 = cmd1.ExecuteReader();
GridView1.DataSource = data1;
GridView1.DataBind();
cnn1.Close();
//图
cnn1.Open();
SqlDataAdapter Da = new SqlDataAdapter();
Da.SelectCommand = new SqlCommand("select 时间,[温度(℃)] from T", cnn1);
DataSet ds = new DataSet();
Da.Fill(ds);
DataView myView = new DataView(ds.Tables[0]);
Chart1.Series["Series1"].Points.DataBindXY(myView, "时间", myView, "温度(℃)");
cnn1.Close();
cnn1.Dispose();
//状态
SqlConnection cnn2 = new SqlConnection();
cnn2.ConnectionString = @"Data Source=YINGJHSHP541\SQLEXPRESS;Initial Catalog=test3;User ID=sa;Password=yingjh";
cnn2.Open();
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = cnn2;
cmd2.CommandText = "select * from ST";
SqlDataReader data2;
data2 = cmd2.ExecuteReader();
data2.Read();
string a = data2.GetString(0);
switch (a)
{
case "1":
Label2.Text = "打开";
break;
case "0":
Label2.Text = "预设";
break;
case "-1":
Label2.Text = "关闭";
break;
default:
break;
}
Label4.Text = data2.GetString(1) + "℃";
cnn2.Close();
cnn2.Dispose();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(@"Data Source=YINGJHSHP541\SQLEXPRESS;Initial Catalog=test3;User ID=sa;Password=yingjh");
SqlCommand cmd = new SqlCommand();
cnn.Open();
cmd.Connection = cnn;
//设置开关,flag为开关控制权
switch (DropDownList1.Text)
{
case "1":
cmd.CommandText = "update ST set STATE='1'" + ",flag='1'";
break;
case "0":
cmd.CommandText = "update ST set STATE='0'" + ",STEMP='" + TextBox1.Text + "',flag='1'";
break;
case "-1":
cmd.CommandText = "update ST set STATE='-1'" + ",flag='1'";
break;
default:
cmd.CommandText = "update ST set STATE='0'" + ",STEMP='" + TextBox1.Text + "',flag='1'";
break;
}
cmd.ExecuteNonQuery();
cnn.Close();
cnn.Dispose();
//温度
SqlConnection cnn1 = new SqlConnection(@"Data Source=YINGJHSHP541\SQLEXPRESS;Initial Catalog=test3;User ID=sa;Password=yingjh");
cnn1.Open();
//表
SqlCommand cmd1 = new SqlCommand();
cmd1.Connection = cnn1;
cmd1.CommandText = "select * from T";
SqlDataReader data1;
data1 = cmd1.ExecuteReader();
GridView1.DataSource = data1;
GridView1.DataBind();
cnn1.Close();
//图
cnn1.Open();
SqlDataAdapter Da = new SqlDataAdapter();
Da.SelectCommand = new SqlCommand("select 时间,[温度(℃)] from T", cnn1);
DataSet ds = new DataSet();
Da.Fill(ds);
DataView myView = new DataView(ds.Tables[0]);
Chart1.Series["Series1"].Points.DataBindXY(myView, "时间", myView, "温度(℃)");
cnn1.Close();
cnn1.Dispose();
//状态
SqlConnection cnn2 = new SqlConnection();
cnn2.ConnectionString = @"Data Source=YINGJHSHP541\SQLEXPRESS;Initial Catalog=test3;User ID=sa;Password=yingjh";
cnn2.Open();
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = cnn2;
cmd2.CommandText = "select * from ST";
SqlDataReader data2;
data2 = cmd2.ExecuteReader();
data2.Read();
string a = data2.GetString(0);
switch (a)
{
case "1":
Label2.Text = "打开";
break;
case "0":
Label2.Text = "预设";
break;
case "-1":
Label2.Text = "关闭";
break;
default:
break;
}
Label4.Text = data2.GetString(1) + "℃";
cnn2.Close();
cnn2.Dispose();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
热心网友
时间:2023-10-07 00:10
推荐看书
Visual C++_Turbo C串口通信编程实践追问很明显。。我是网页白痴。。。要不然这么简单的是也不敢来麻烦大家了。。能帮忙弄一个吗?