今天用C#写了一个单片机串口的上位机,如图,单片机用跳线使串口的第2、3针连接,在本地计算机上实现串口通信
电脑发送数据很正常(提示发送成功),但是一点接收缓冲区的数据的时候电脑就没反应的,好像瘫痪了,不知道哪里出错了~~
菜鸟一只,忘高人相助
NSWEC[GT}K$J3_HM9GV@F.jpg (58.7 KB, 下载次数: 1)
下载附件
2012-5-8 13:54 上传
- 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;
- namespace WindowsFormsApplication1
- {
- public partial class t : Form
- {
- public t()
- {
- InitializeComponent();
- }
- private void t_Load(object sender, EventArgs e)
- {
- label1.Text = "端口";
- button4.Enabled = false;
- //get port names
- comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
- }
- private void button1_Click(object sender, EventArgs e)
- {
- 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("数据接收成功!", "系统提示");
- }
- private void button3_Click(object sender, EventArgs e)
- {
- serialPort1.PortName = "COM4";
- serialPort1.BaudRate = 9600;
- serialPort1.Open();
- button3.Enabled = false;
- button4.Enabled = true;
- }
- private void button4_Click(object sender, EventArgs e)
- {
- serialPort1.Close();
- button4.Enabled = false;
- button3.Enabled = true;
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
-
- }
- private void label1_Click(object sender, EventArgs e)
- {
-
- }
- }
- }
复制代码
{
byte[] data = new byte[serialPort1.BytesToRead];
serialPort1.Read( data, 0, data.Lenght );
textBox2.Text = Encoding.Unicode.GetString(data);
//serialPort1.Close();
MessageBox.Show("数据接收成功!", "系统提示");
}
一周热门 更多>