程序运行出现的问题。
通过USB口读取数据,以下是编的代码,但是发现从“comm.BaudRate = 9600; ”这个语句之后貌似就直接跳过不运行了,不知道问题出在哪里,求老师们帮助。多谢!!using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
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;
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;
}
}
} 没做过 顶一下
页:
[1]