读取串口C#asp.net输入? [英] read input from serial port c# asp.net?

查看:64
本文介绍了读取串口C#asp.net输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个PIC16F877A微控制器和LCD显示器。我做了PIC控制器为每2mins显示在液晶屏的随机数,它也可以通过RS232电缆发送这些随机产生的数以我的笔记本电脑每2分钟。我创建使用ASP.NET 4 C#,我想读的那些数字,做功能的网站。我写了下面的代码,但它不工作,但我在我的笔记本电脑超级终端接收的随机数。请建议我如何实现这一目标 - 我要接收这些codeS每2mins。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;
使用System.Web.UI.WebControls;
使用System.IO.Ports;公共部分类输入code:System.Web.UI.Page
{
    静态的SerialPort mySerialPort =新的SerialPort(COM46,1200,Parity.None,8,StopBits.One);
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        mySerialPort.Open();
        字符串str = mySerialPort.ReadLine();
        mySerialPort.Close();    }
}


解决方案

我建议重新architecturing程序。

ASP.NET被设计为无状态(因为其HTTP基础)。 ASP.NET主机进程(w3wp.exe的)可以被终止,并随意回收,没有警告。如果你需要的可靠性,你应该在专用的Windows服务的过程,通过某种形式的IPC的ASP.NET应用程序进行通信沟通的SerialPort

使用的Page_Load是令人难以置信的不恰当的,更何况可怕的非线程安全的。在 .Close 方法等效于调用 .Dispose ,这违背了你的SerialPort为静态成员的点(作为其生命周期结束)。

我得到你不熟悉ASP.NET或IO口编程的想法。我建议你​​磨练分开这些领域的技能(即首先制定了一个命令行串口程序,即不使用串行端口,然后一个简单的ASP.NET Web应用程序),然后将它们结合起来,你很坚强的后两种区域。

Hello I have a PIC16F877A micro-controller and Lcd display. I have made PIC controller to display random numbers in the LCD screen for every 2mins and it also send those randomly generated number to my laptop for every 2 mins through RS232 cable. I am creating a website using ASP.NET 4 C# where I want to read those numbers and do functions. I have written the following coding but its not working but I am receiving those random numbers in my laptop hyper-terminal. Please suggest me how to achieve this - I have to receive those codes every 2mins.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO.Ports;

public partial class Entercode : System.Web.UI.Page
{
    static SerialPort mySerialPort = new SerialPort("COM46", 1200, Parity.None, 8,     StopBits.One);
    protected void Page_Load(object sender, EventArgs e)
    {
        mySerialPort.Open();
        String str = mySerialPort.ReadLine();
        mySerialPort.Close();

    }
}

解决方案

I suggest re-architecturing your program.

ASP.NET is designed to be stateless (because of its HTTP basis). ASP.NET host processes (w3wp.exe) can be terminated and recycled at will, without warning. If you need reliability you should perform SerialPort communication in a dedicated Windows Service process which communicates with your ASP.NET application via some form of IPC.

Using Page_Load is incredibly inappropriate, not to mention terribly un-thread-safe. The .Close method is equivalent to calling .Dispose, which defeats the point of having your SerialPort as a static member (as its lifetime ends).

I get the idea you're not familiar with either ASP.NET or IO port programming. I suggest you hone your skills in these areas separately (i.e. develop a command-line serial port program first, then a simple ASP.NET web application that doesn't use the serial port), then combine them after you're strong in both areas.

这篇关于读取串口C#asp.net输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆