首页 新闻 会员 周边 捐助

C# web 串口打开关闭

0
悬赏园豆:10 [已解决问题] 解决于 2015-03-27 23:10

做个网页串口调试工具,但是在打开串口后,再次点击, 发现port不是打开的port,而是com1,具体的代码如下。请问哪我处理错了? 

void InitPort()
    {
            com.Items.Clear(); 
            //添加串口项目
            foreach (string s in System.IO.Ports.SerialPort.GetPortNames())
            {//获取有多少个COM口           
                com.Items.Add(s);
            }   
            //串口设置默认选择项
            com.SelectedIndex = 1;       
            port.BaudRate = 9600;

            port.DataReceived += new SerialDataReceivedEventHandler(sp1_DataReceived);
            //sp1.ReceivedBytesThreshold = 1;

            // radio1.Checked = true;  //单选按钮默认是选中的
            // rbRcvStr.Checked = true;

            //准备就绪             
            port.DtrEnable = true;
            port.RtsEnable = true;
            //设置数据读取超时为1秒
          //  port.ReadTimeout = 1000;

            port.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (str == null)
        {
            warning.Text = "本机没有串口!";
            return;
        }
        if (!IsPostBack) // 原来在程序运行过程中,因为DropDownList的项被再一次重新绑定
        {
            InitPort();
            return;
        }
        else
        {
          
        }
    }

 
    protected void Button1_Click(object sender, EventArgs e)
    {
      
        if (!port.IsOpen)
        {
            try
            {
                //设置串口号      
                string serialName = com.SelectedValue;
                port.PortName = serialName;
              
                //设置各“串口设置”
                string strBaudRate =  Baudrate.Text;
                string strDateBits = Data_bit.Text;
                string strStopBits = Stop_bit.Text;
                Int32 iBaudRate = Convert.ToInt32(strBaudRate);
                Int32 iDateBits = Convert.ToInt32(strDateBits);

                port.BaudRate = iBaudRate;       //波特率
                port.DataBits = iDateBits;       //数据位
                switch (Stop_bit.Text)            //停止位
                {
                    case "1":
                        port.StopBits = StopBits.One;
                        break;
                    case "1.5":
                        port.StopBits = StopBits.OnePointFive;
                        break;
                    case "2":
                        port.StopBits = StopBits.Two;
                        break;
                    default:
                        warning.Text =  "Error:参数不正确!";                        
                        break;
                }
                switch (Check_bit.Text)             //校验位
                {
                    case "无":
                        port.Parity = Parity.None;
                        break;
                    case "奇校验":
                        port.Parity = Parity.Odd;
                        break;
                    case "偶校验":
                        port.Parity = Parity.Even;
                        break;
                    default:
                        warning.Text = "Error:参数不正确!";
                        break;
                }

                if (port.IsOpen == true)//如果打开状态,则先关闭一下
                {
                    port.Close();
                }
                //状态栏设置
                note.Text = "串口号:" + port.PortName + "|";
                note.Text = note.Text + "波特率:" + port.BaudRate + "|";
                note.Text = note.Text + "数据位:" + port.DataBits + "|";
                note.Text = note.Text + "停止位:" + port.StopBits + "|";
                note.Text = note.Text + "校验位:" + port.Parity + "|";

                //设置必要控件不可用

                //Baudrate.Enabled = false;
                //Data_bit.Enabled = false;
                //Stop_bit.Enabled = false;
                //Check_bit.Enabled = false;
                //com.Enabled = false;
                port.Open();     //打开串口
                if (port.IsOpen)
                {
                   warning.Text = "Error:串口打开失败。";
                }
                Open_com.Text = "关闭串口";
            }
            catch (System.Exception ex)
            {
                warning.Text = "Error:串口打开错误 " + ex.Message;
               // tmSend.Enabled = false;
                return;
            }
        }
        else
        {
            //状态栏设置
            note.Text = "串口号:未指定 |";
            note.Text = note.Text + " 波特率:未指定 |";
            note.Text = note.Text + " 数据位:未指定 |";
            note.Text = note.Text + " 停止位:未指定 |";
            note.Text = note.Text + " 校验位:未指定 |";
            //恢复控件功能
            //设置必要控件不可用
            Baudrate.Enabled = true;
            Data_bit.Enabled = true;
            Stop_bit.Enabled = true;
            Check_bit.Enabled = true;
            try
            {
                port.Close();                    //关闭串口
            }
            catch (System.Exception ex)
            {
                warning.Text = "Error:串口关闭失败 " + ex.Message;
                // tmSend.Enabled = false;
                return;
            }

            Open_com.Text = "打开串口";
           // tmSend.Enabled = false;         //关闭计时器
        }
        warning.Text = port.IsOpen.ToString();
        TextBox1.Text = port.PortName;
    }

笨笨,跳跳的主页 笨笨,跳跳 | 初学一级 | 园豆:164
提问于:2015-03-05 14:39
< >
分享
最佳答案
0

你是要打开服务器上的串口,还是要打开客户机上的串口?

收获园豆:10
Launcher | 高人七级 |园豆:45050 | 2015-03-05 14:57

我想做个单片机的调试  ,打开单片机的串口。

笨笨,跳跳 | 园豆:164 (初学一级) | 2015-03-05 15:32

@笨笨,跳跳: 那你写个 Winform 程序不就行了,为啥要用 Asp.Net ?

Launcher | 园豆:45050 (高人七级) | 2015-03-05 15:49

@Launcher: 用asp.net的话,可以放到服务器上用。

笨笨,跳跳 | 园豆:164 (初学一级) | 2015-03-05 16:03

@笨笨,跳跳: 哦,那么你知道 InitPort 和 Button1_Click 都是在服务器上执行的吗?也就是说这里枚举和要打开 COM 都是服务器上的。你要的是这个结果吗?

Launcher | 园豆:45050 (高人七级) | 2015-03-05 16:14

@Launcher: 不是,谢谢你提醒。如果我想要打开的是本地的,如何来做呢?

笨笨,跳跳 | 园豆:164 (初学一级) | 2015-03-05 16:53

@笨笨,跳跳: 你需要写一个 ActiveX 控件嵌入到网页中去,将串口的操作都封装在 ActiveX 控件中。

Launcher | 园豆:45050 (高人七级) | 2015-03-05 16:56
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册