</div> </form> </body> </html> 2、 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.VisualBasic.Devices;namespace mytestWEB { public partial class rs232 : System.Web.UI.Page { public static System.IO.Ports.SerialPort com=new System.IO.Ports.SerialPort ();
你有没发现要串口的基本都要装一个东西的!就用ActiveX做吧!在ActiveX做和winform做一样的,再引用下就好了!然后让人家安装下就好了
楼上的朋友,我不是在客户端做的。就在本地服务端做。
智能设备通过串口连接PC机——PC机读取串口数据——在该PC机架设WEB服务器——静态IP指向到该PC机——域名绑定到目标静态IP——访问域名,远程控制设备。我只是想实现这样的一个功能而已。
1、<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="rs232.aspx.cs" Inherits="mytestWEB.rs232" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="cbxPortName" runat="server">
</asp:DropDownList>
<asp:Button ID="btnOpenPort" runat="server" onclick="btnOpenPort_Click"
Text="连接" />
<asp:Button ID="btnClose" runat="server" Text="断开" onclick="btnClose_Click" />
<br />
<asp:TextBox ID="txtSendMsg" runat="server" Width="100px"></asp:TextBox>
<asp:Button ID="btnSend" runat="server" Text="发送" onclick="btnSend_Click" />
<br />
<asp:TextBox ID="txtReceive" runat="server" Height="49px" Width="112px"></asp:TextBox>
<asp:Button ID="btnReceive" runat="server" Text="接收"
onclick="btnReceive_Click" />
<br />
<asp:Label ID="lbStatus" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
2、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic.Devices;namespace mytestWEB
{
public partial class rs232 : System.Web.UI.Page
{
public static System.IO.Ports.SerialPort com=new System.IO.Ports.SerialPort ();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
//获取所有的串口
Microsoft.VisualBasic.Devices.Computer pc = new Microsoft.VisualBasic.Devices.Computer();
foreach (string s in pc.Ports.SerialPortNames)
{
this.cbxPortName.Items.Add(s);
}
} } protected void btnOpenPort_Click(object sender, EventArgs e)
{
if (cbxPortName.SelectedItem != null)
{
com.Close();
com.PortName = cbxPortName.SelectedItem.ToString();
com.Open();
if (com.IsOpen)
{
btnClose.Enabled = true;
lbStatus.Text = "串口" + cbxPortName.SelectedItem.ToString() + "已经连接";
}
}
else
{
Response.Write("hello");//MessageBox.Show("请选择串口!");
}
} protected void btnClose_Click(object sender, EventArgs e)
{
com.Close();
if (!com.IsOpen)
{
btnClose.Enabled = false;
}
} protected void btnSend_Click(object sender, EventArgs e)
{
if (com.IsOpen)
{
com.WriteLine(txtSendMsg.Text);
}
else
{
//MessageBox.Show("请先连接串口!");
}
} protected void btnReceive_Click(object sender, EventArgs e)
{
txtReceive.Text = "";
if (com.IsOpen)
{
try
{
com.ReadTimeout = 5000;
txtReceive.Text = com.ReadLine();
}
catch
{ //MessageBox.Show("读取数据超时!");
} }
else
{
//MessageBox.Show("请先连接串口!");
}
}
}
}
designer.cs 就不发给你了。你右键根目录,轮换成WEB程序,就会自动建立 。
网页串口偶都没用过了!