求做一个web页面控制服务器串口,可用Mscomm控件做!满意会继续最佳。htm页面都能操作本地串口。
是操作web服务器的串口。权限不是问题。。如果有必要也可也付钱。有样本代码!用asp或asp。net或php做都可以。...
是操作web服务器的串口。权限不是问题。。如果有必要也可也付钱。有样本代码!
用asp 或asp。net或php做都可以。 展开
用asp 或asp。net或php做都可以。 展开
2个回答
展开全部
看了你的留言直接在这回吧,其实网页方面我也是个菜鸟。去年我做过一个类似的,当时也是自学了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)
{
}
}
做法:用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)
{
}
}
更多追问追答
追问
可不可以改成对串口进行读写的。。代码呢!。。能读写十六进制的数据就行。。单片机的代码我自己写。
追答
下面的是asp.net,用C#编写的网页直接控制串口的方法,网上的我试过,是可以的 ,就是操作不熟。你练练吧 (截取的一段,字数有限制)
//初始化SerialPort对象方法.PortName为COM口名称,例如"COM1","COM2"等,注意是string类型
public void InitCOM(string PortName)
{
port1 = new SerialPort(PortName);
port1.BaudRate = 9600;//波特率
port1.Parity = Parity.None;//无奇偶校验位
port1.StopBits = StopBits.Two;//两个停止位
port1.Handshake = Handshake.RequestToSend;//控制协议
port1.ReceivedBytesThreshold = 4;//设置事件发生前内部输入缓冲区中的字节数
port1.DataReceived += new SerialDataReceivedEventHandler(port1_DataReceived);//DataReceived事件委托
}
//打开串口的方法
public void OpenPort()
{
try
{
port1.Open();
}
catch { }
if (port1.IsOpen)
{
Console.WriteLine("the port is opened!");
}
else
{
Console.WriteLine("failure to open the port!");
}
}
//关闭串口的方法
public void ClosePort()
{
port1.Close();
if (!port1.IsOpen)
{
Console.WriteLine("the port is already closed!");
}
}
//向串口发送数据
public void SendCommand(string CommandString)
{
byte[] WriteBuffer = Encoding.ASCII.GetBytes(CommandString);
port1.Write(WriteBuffer, 0, WriteBuffer.Length);
}
下面的是VB控制串口的核心代码,加个timer用起来比较方便
MSComm1.CommPort = "3"
MSComm1.PortOpen = True
MSComm1.Output = "1"
timesave = Timer '延时
While Timer = 3 Then
yyy1 = Trim(MSComm1.Input) 'yyy1
End If
2012-02-01
展开全部
推荐看书
Visual C++_Turbo C串口通信编程实践
Visual C++_Turbo C串口通信编程实践
追问
很明显。。我是网页白痴。。。要不然这么简单的是也不敢来麻烦大家了。。能帮忙弄一个吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询