C#串口 问题 救命啊
namespace Ex13_01{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM10";
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("数据接收成功!","系统提示");
}
}
}
这是 一本书上的 源程序代码 就2个BUTTON 2个 TEXTBOX 第一步 可以 实现 第二步 就卡住不动了,哪个高手 帮个忙这个我是在不明白 就简单的 告诉我 怎么写就行了 不用告诉我原理 谢谢 --------------------编程问答-------------------- 这个不是怎么写的问题
关键是你的第二个,是否有数据到达,如果你的设备没有给你发送消息,那么它就一直卡着
就像你在控制台写ReadLine()会怎么样?
暂停,等你输入,你输入了,才会继续 --------------------编程问答-------------------- button2_Click事件中单步调试 看看卡在哪一行了 --------------------编程问答-------------------- 1)serialPort1.PortName = "COM10";
你有COM10??,打开你的设备管理器看看,到底是哪个端口
2)没有打开串口,直接去ReadLine是会阻塞的
3)data,你要判断是否为空,是否包含数据,再去做编码转换 --------------------编程问答-------------------- 第二步卡住是因为那个端口没有数据可以接收。
给你推荐个软件,可以生成一对虚拟的串口来进行串口程序的调试。
VSPD XP 5 --------------------编程问答-------------------- 多线程试下,因为一个线程做了收串口就不刷屏了,所以会死掉。
不知道对不对
thread
invoke
--------------------编程问答-------------------- 第一步发数据出去
第二步接收数据,数据从哪里来呢?
百度下“串口调试工具” --------------------编程问答-------------------- namespace ceshi1_1
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
this.btnSend = new System.Windows.Forms.Button();
this.timeSend = new System.Windows.Forms.Timer(this.components);
this.cbxAutoSend = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(15, 100);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(67, 22);
this.button1.TabIndex = 0;
this.button1.Text = "打开";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(100, 72);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(172, 143);
this.textBox1.TabIndex = 1;
//
// serialPort1
//
this.serialPort1.PortName = "COM3";
this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
//
// btnSend
//
this.btnSend.Location = new System.Drawing.Point(15, 163);
this.btnSend.Name = "btnSend";
this.btnSend.Size = new System.Drawing.Size(70, 23);
this.btnSend.TabIndex = 2;
this.btnSend.Text = "发送";
this.btnSend.UseVisualStyleBackColor = true;
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
//
// timeSend
//
this.timeSend.Interval = 500;
this.timeSend.Tick += new System.EventHandler(this.timeSend_Tick);
//
// cbxAutoSend
//
this.cbxAutoSend.AutoSize = true;
this.cbxAutoSend.Location = new System.Drawing.Point(100, 234);
this.cbxAutoSend.Name = "cbxAutoSend";
this.cbxAutoSend.Size = new System.Drawing.Size(72, 16);
this.cbxAutoSend.TabIndex = 3;
this.cbxAutoSend.Text = "自动发送";
this.cbxAutoSend.UseVisualStyleBackColor = true;
this.cbxAutoSend.CheckedChanged += new System.EventHandler(this.cbxAutoSend_CheckedChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(685, 497);
this.Controls.Add(this.cbxAutoSend);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.IO.Ports.SerialPort serialPort1;
private System.Windows.Forms.Button btnSend;
private System.Windows.Forms.Timer timeSend;
private System.Windows.Forms.CheckBox cbxAutoSend;
}
}
--------------------编程问答-------------------- using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace ceshi1_1
{
public partial class Form1 : Form
{
delegate void HandleInte易做图ceUpdateDelegate(string text); //委托,此为重点
HandleInte易做图ceUpdateDelegate inte易做图ceUpdateHandle;
//首次交互发送指令
byte[] FirstCommand = new byte[] { 0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x01, 0x00, 0x05 };
byte[] SecondCommand = new byte[] { 0xEF, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x03, 0x0A, 0x00, 0x0E };
public Form1()
{
InitializeComponent();
inte易做图ceUpdateHandle = new HandleInte易做图ceUpdateDelegate(UpdateTextBox); //实例化委托对象
serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.PortName = "COM3";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Open();
button1.Enabled = false;
if (this.serialPort1.IsOpen)
{
serialPort1.Write(FirstCommand, 0, FirstCommand.Length);
}
else
{
//MessageBox.Show("串口未打开!");
button1_Click(sender, e);
}
}
private void UpdateTextBox(string temp)
{
temp = temp.Replace("-", "");
this.textBox1.Text += temp;
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
byte[] data = new byte[this.serialPort1.BytesToRead];
serialPort1.Read(data, 0, data.Length);
this.Invoke(inte易做图ceUpdateHandle, new string[] { BitConverter.ToString(data) });
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
void RevAppend(string str)
{
if (textBox1.InvokeRequired)
this.Invoke(new MethodInvoker(delegate
{
textBox1.AppendText(str); ;
textBox1.SelectionStart = int.MaxValue;
textBox1.SelectionLength = 1;
textBox1.ScrollToCaret();
}));
else
{
textBox1.AppendText(str);
textBox1.SelectionStart = int.MaxValue;
textBox1.SelectionLength = 1;
textBox1.ScrollToCaret();
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (this.serialPort1.IsOpen)
{
serialPort1.Write(FirstCommand, 0, FirstCommand.Length);
}
else
{
MessageBox.Show("串口未打开!");
}
}
private void timeSend_Tick(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Write(FirstCommand, 0, FirstCommand.Length);
}
else
{
this.timeSend.Enabled = false;
MessageBox.Show("串口已关闭!");
}
}
private void cbxAutoSend_CheckedChanged(object sender, EventArgs e)
{
if (this.cbxAutoSend.Checked) { this.timeSend.Enabled = true; }
else { this.timeSend.Enabled = false; }
}
}
} --------------------编程问答-------------------- 无聊
自己搞清楚什么是串口先
你一个串口通信个毛啊
除非你把串口的引脚短起来,自发自收
--------------------编程问答-------------------- 照着书上写的,在你机器的环境下是不一定能运行的。 --------------------编程问答-------------------- 一般用DataReceived事件来通知你串口已经接收到了数据。然后你再对数据进行格式处理后,显示出来。 --------------------编程问答-------------------- 用comassistant测试下,看看是否有接收到数据 --------------------编程问答-------------------- 这本破书早就该扔了
如果不是虚拟串口,哪明com10
一个示例程序,有必要进行base64编码??
按钮点击
serialPort1.ReadLine()
这样就一定能读取到数据吗?好笑
--------------------编程问答-------------------- 看我以前的一个有关串口的回复
http://topic.csdn.net/u/20120420/11/795f7914-1899-4161-9cc3-3e4d6feeac96.html
补充:.NET技术 , C#