| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 628 人关注过本帖
标题:asp.net中serial port与虚拟串口连接的问题
只看楼主 加入收藏
骨子里的认真
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2013-9-17
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
asp.net中serial port与虚拟串口连接的问题
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM1";
            serialPort1.BaudRate = 9600;
             serialPort1.Open();
            byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
            string str = Convert.ToBase64String(data);
            serialPort1.WriteLine(str);
            MessageBox.Show("数据发送成功!", "系统提示");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
            textBox2.Text = Encoding.Unicode.GetString(data);
            serialPort1.Close();
            MessageBox.Show("数据接收成功!", "系统提示");
        }

    }
错误提示:

未处理 System.InvalidOperationException
  Message="端口被关闭。"
  Source="System"
  StackTrace:
      (String value)
       在 Ex13_01.Form1.button2_Click(Object sender, EventArgs e) 位置 F:\数据库开发\SQL数据库\各种练习\Ex13_01\Ex13_01\Form1.cs:行号 32
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 Ex13_01.Program.Main() 位置 F:\数据库开发\SQL数据库\各种练习\Ex13_01\Ex13_01\Program.cs:行号 17
       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
不懂怎么处理,求指点
搜索更多相关主题的帖子: public private 
2013-09-27 17:55
Aviva_Wang
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:苏州
等 级:贵宾
威 望:26
帖 子:701
专家分:2382
注 册:2013-3-12
收藏
得分:10 
空值异常,他有显示行号,你可以看看是在哪边,如果是空值,你上面的那种数据转换是会报错的。
我想说,我不懂串口

asp
2013-09-30 08:31
骨子里的认真
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2013-9-17
收藏
得分:0 
回复 2楼 Aviva_Wang
我也不懂串口,刚开始学,什么都不会,也不会改错。这个好像不是空值的问题,我改了,但又有新问题了,端口打开时无法设置“PortName”。不过还是谢谢你。
2013-09-30 10:24
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
收藏
得分:10 
非法操作,端口被关闭!

♂ 死后定当长眠,生前何须久睡。♀
2013-09-30 17:54
快速回复:asp.net中serial port与虚拟串口连接的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019363 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved