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 第一步 可以 实现 第二步 就卡住不动了,哪个高手 帮个忙这个我是在不明白 就简单的 告诉我 怎么写就行了 不用告诉我原理 谢谢
{
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 第一步 可以 实现 第二步 就卡住不动了,哪个高手 帮个忙这个我是在不明白 就简单的 告诉我 怎么写就行了 不用告诉我原理 谢谢
解决方案 »
- 将表中多层数据绑定到treeview 同时还要能够查询等等操作
- 在CheckedListBox中,怎样判新添加的项不能与CheckedListBox中已有项相同? 非常感谢大家帮助一下!!!
- 初学者:在VS2005下c#(Winform)datagridview能否实现固定表格列;
- 请教C#中窗体滚动条设置的问题
- 救命啊,我的服务为什么会暂停????100分就救啊!!!
- 利用axWebBrowser做浏览器,如何取得下载下来的整个html代码
- 复制粘贴的问题
- [编程难题]编写一个求解时间满足约束的的算法.说明内详.真诚求助
- 怎么用C#实现QQ表情的列表框
- 各位,在.net中,如果用代碼知道今天是星期幾阿???
- winform在使用委托时为什么总是“未将对象引用设置到对象的实例”
- 外键字段输入问题
暂停,等你输入,你输入了,才会继续
你有COM10??,打开你的设备管理器看看,到底是哪个端口
2)没有打开串口,直接去ReadLine是会阻塞的
3)data,你要判断是否为空,是否包含数据,再去做编码转换
给你推荐个软件,可以生成一对虚拟的串口来进行串口程序的调试。
VSPD XP 5
不知道对不对
thread
invoke
{
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.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 HandleInterfaceUpdateDelegate(string text); //委托,此为重点
HandleInterfaceUpdateDelegate interfaceUpdateHandle; //首次交互发送指令
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();
interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(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(interfaceUpdateHandle, 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; }
}
}
}
如果不是虚拟串口,哪明com10
一个示例程序,有必要进行base64编码??按钮点击
serialPort1.ReadLine()
这样就一定能读取到数据吗?好笑
http://topic.csdn.net/u/20120420/11/795f7914-1899-4161-9cc3-3e4d6feeac96.html