如何解决 asp.net 2019 读取Com 串口数据 在客户机上完成 在客户机不弹出这个提示
我的代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WeightMain(print).aspx.cs" Inherits="WeighManage.WeightMain_print_" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Print</title>
</head>
<script language="javascript" type="text/javascript">
function init() {
configPort();
document.all.printport.PortOpen = true;
//bindEvent();
}
function send(printmsg) {
document.all.printport.Output = printmsg;
}
function pageEnd() {
document.all.printport.PortOpen = false;
}
function ClosePort() {
if (document.all.printport.PortOpen == true) {
document.all.printport.PortOpen = false;
}
else {
window.alert("串口已经关闭!");
}
}
function OpenPort() {
if (document.all.printport.PortOpen == false) {
document.all.printport.PortOpen = true;
}
else {
document.all.printport.PortOpen = true;
window.alert("串口已经关闭!");
}
}
//var bindEvent = function() {
// function document.all.printport::OnComm() {
// receives(); // 触发收发事件后的处理方法
// }
// }
// 配置端口方法
function configPort() {
document.all.printport.CommPort = 1;//这相当于com1 如果是com2 等于2就可以了
document.all.printport.settings = '4800,n,7,1'; // 9600:波特率 n:奇偶位 8:数据位 1:校验位
document.all.printport.OutBufferCount = 0; //清空发送缓冲区
document.all.printport.InBufferCount = 0; //滑空接收缓冲区
document.all.printport.RThreshold = 14; //这个参数很重要,这个参数配置要符合硬件设备 该参数表示接收硬件设备多少位数据的时候触发onComm事件
}
//定时调用就行了,回调不行
function receives() {
printport.PortOpen = true;
var inputvalue = document.all.printport.Input;
//这里就是接受的数据
//console.log(inputvalue)
document.all.S1.value = inputvalue
}
</script>
<script id="clientEventHandlersJS" language="javascript">
function OnComm() {
switch (document.all.printport.CommEvent) {
case 1: { window.alert("Send OK!"); break; } //发送事件
case 2: { receives(); break; } //接收事件
default: alert("Event Raised!" + document.all.printport.CommEvent);;
}
}
</script>
<script language="javascript" for="printport" event="OnComm">
OnComm();
</script>
<body onload='init();' onunload='pageEnd();'>
<%-- <form id="form1" runat="server">
<div>
</div>
</form>--%>
<form method="post" action="--WEBBOT-SELF--">
<p>收到信息:<textarea rows="21" name="S1" cols="86"></textarea></p>
<p>
<input type="button" value="获取信息" name="B3" onclick='receives();' />
</p>
<p>发送内容:<textarea rows="1" name="S2" cols="86"></textarea></p>
<p>
<input type="button" value="发送内容" name="B1" onclick='send(document.all.S2.value);' />
</p>
<p>
<input type="button" value="关闭串口" name="B2" onclick='ClosePort();' />
<input type="button" value="打开串口" name="B2" onclick='OpenPort();' />
</p>
<p>
<object id="printport" classid="clsid:648A5600-2C6E-101B-82B6-000000000014" style="display: none">
</object>
</p>
</form>
</body>
</html>
在这段代码中,你想要在客户端读取 COM 串口数据而不弹出 ActiveX 控件和页面其他部分交互可能不安全的提示。为了实现这一目标,你可以尝试以下方法:
然而,需要注意的是,这种方法会降低客户端的安全性,因为修改了 Internet Explorer 的安全设置。为了在不降低安全性的情况下实现相同的功能,你可以尝试使用基于 Web 的技术,如 WebSockets 或者 Web Serial API。不过,这些技术可能需要你修改现有的代码结构以适应新的实现方式。
以下回答参考GPT并且由Bony-整理:
根据您提供的信息和截图,我理解您的问题是当您在客户机上运行 ASP.NET 2019 代码时,如果读取 COM 串口数据,会弹出一个用户权限请求对话框,要求您提供管理员权限。您想知道如何在客户机上不弹出该对话框,并成功读取 COM 串口数据。
该用户权限请求对话框弹出是因为您的应用程序需要管理员权限才能读取串口数据。一种解决方法是通过修改应用程序的权限,以便它可以在不需要管理员权限的情况下读取串口数据。
以下是可能的解决方法:
通过修改应用程序的权限,让应用程序在不需要管理员权限的情况下读取串口数据。可以使用类似于下面的命令在应用程序所在的目录上授予用户读取和写入访问权限:
icacls.exe "C:\YourApplicationDirectory" /grant Users:(OI)(CI)R
修改应用程序的清单文件,将权限级别设置为“asInvoker”。在 Visual Studio 中打开清单文件并添加以下代码:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
将应用程序移动到不需要管理员权限的文件夹。例如,将应用程序移动到“C:\Users\Public”或“C:\ProgramData”文件夹中。
注意:上述解决方案并不适用于所有情况。如果应用程序需要特殊的权限才能运行,则需要更改其他配置或使用更安全的方法。
希望这些信息能够帮助您解决问题。如果您需要更多帮助,请提供更多信息和细节,以便我们更好地理解您的问题。
这种一般用于。。。。。提示完之后进行页面的跳转的操作
Response.Write("<script>alert('弹框后跳转!');window.location.href ='./WebForm2.aspx'</script>");
这里用到了Response.Write进行页面的跳转,俺就想起来了页面跳转的几个骚的操作
Response.Write("<script>alert('弹框后跳转!');window.location.href ='./WebForm2.aspx'</script>");
//跳转到新的页面,原来的窗口被替代
Response.Redirect("XXX.aspx", true);
//新打开一个页面用来加载xxx.axpx,原来的页面被保留
Response.Write("<script>window.open('XXX.aspx','_blank')</script>");
//也是用来打开新的页面,经常用于Session传值
Server.Transfer("XXX.aspx");