标题:新人求助:程序运行出现的问题。
只看楼主
andreiguodal
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-5-31
结帖率:0
已结贴  问题点数:20 回复次数:1 
新人求助:程序运行出现的问题。
最近在做光通信,通过USB口读取数据,以下是编的代码,但是发现从“comm.BaudRate = 9600; ”这个语句之后貌似就直接跳过不运行了,不知道问题出在哪里,求老师们帮助。多谢!!

using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
using System.Text.RegularExpressions;
using System.Threading;

namespace 读串口
{
    public partial class Form1 : Form
    {
        private string ll = "55";
        private StringBuilder builder = new StringBuilder();
        private SerialPort comm = new SerialPort();
        //private Thread thread;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            comm.PortName = "COM1";
            comm.BaudRate = 9600;
            //comm.Open();
            int n;
            //while (true)
            //{
            //System.Threading.Thread.Sleep(2000);

            n = comm.BytesToRead;
            byte[] buf = new byte[n];
            comm.Read(buf, 0, n);
            ll = Encoding.Default.GetString(buf);
            //MessageBox.Show(ll);
            if (ll.StartsWith("U"))
            {
                ll = "55";
            }
            else
            {
                ll = "66";
            }
            comm.Close();
            textBox1.Text = ll;
        }
    }
}
搜索更多相关主题的帖子: 通信 public 
2014-05-31 17:12
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2545
专家分:9359
注 册:2007-11-3
得分:20 
程序代码:
private void button1_Click(object sender, EventArgs e)
         {
             serialPort1.PortName = "COM2";
             serialPort1.BaudRate = 9600;
             serialPort1.DataReceived +=new SerialDataReceivedEventHandler(serialPort1_DataReceived);
             serialPort1.Open();
             byte[] data = Encoding.Unicode.GetBytes(textBox2.Text);
             string str = Convert.ToBase64String(data);
             serialPort1.WriteLine(str);
             MessageBox.Show("数据发送成功!", "系统提示");
         }
         private string buffer;
         private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
         {
             this.buffer += this.serialPort1.ReadExisting();
         }
         private void button2_Click(object sender, EventArgs e)
         {
             textBox2.Text = buffer;
             serialPort1.Close();
             MessageBox.Show("数据接收成功!", "系统提示");
         }

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2014-06-03 08:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-432335-1-1.html




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

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