不包含任何usercontrol类型 ArgumentException
public Button button1;public Button button3;
public int iPort = 1; //1,2,3,4
public int iRate = 9600; //1200,2400,4800,9600
public byte bSize = 8; //8 bits
public byte bParity = 0; // 0-4=no,odd,even,mark,space
public byte bStopBits = 1; // 0,1,2 = 1, 1.5, 2
public int iTimeout = 1000;
public mycom mycom1 = new mycom();
public byte[] recb;
public int nub = 0;
private System.Windows.Forms.TextBox t_send;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button13;
private TextBox msg; //readTimeOut
private System.ComponentModel.Container components = null;
public BusApp()
{
InitializeComponent();
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.SuspendLayout();
this.t_send = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.button13 = new System.Windows.Forms.Button();
this.msg = new System.Windows.Forms.TextBox();
// t_send
this.t_send.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.t_send.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
this.t_send.Location = new System.Drawing.Point(88, 127);
this.t_send.Name = "t_send";
this.t_send.Size = new System.Drawing.Size(344, 21);
this.t_send.TabIndex = 2;
// button1
this.button1.Location = new System.Drawing.Point(237, 154);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(40, 23);
this.button1.TabIndex = 3;
this.button1.Text = "发送";
this.button1.Click += new System.EventHandler(this.button1_Click);
// button3
this.button3.Location = new System.Drawing.Point(360, 154);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(40, 23);
this.button3.TabIndex = 3;
this.button3.Text = "清空";
this.button3.Click += new System.EventHandler(this.button3_Click);
this.button5.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));
this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button5.Location = new System.Drawing.Point(237, 338);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(72, 23);
this.button5.TabIndex = 9;
this.button5.Text = "关闭串口";
this.button5.UseVisualStyleBackColor = false;
this.button5.Click += new System.EventHandler(this.button5_Click);
// button13
this.button13.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));
this.button13.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button13.Location = new System.Drawing.Point(360, 338);
this.button13.Name = "button13";
this.button13.Size = new System.Drawing.Size(72, 23);
this.button13.TabIndex = 9;
this.button13.Text = "退 出";
this.button13.UseVisualStyleBackColor = false;
this.button13.Click += new System.EventHandler(this.button13_Click);
// msg
this.msg.ForeColor = System.Drawing.Color.Green;
this.msg.Location = new System.Drawing.Point(0, 0);
this.msg.Multiline = true;
this.msg.Name = "msg";
this.msg.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.msg.Size = new System.Drawing.Size(432, 129);
this.msg.TabIndex = 10;
// Form1
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(441, 369);
this.Controls.Add(this.msg);
this.Controls.Add(this.button5);
this.Controls.Add(this.button1);
this.Controls.Add(this.t_send);
this.Controls.Add(this.button3);
this.Controls.Add(this.button13);
this.Name = "BusApp";
this.Text = "串口通讯";
this.Load += new System.EventHandler(this.BusApp_Load);
this.Closing += new System.ComponentModel.CancelEventHandler(this.BusApp_Closing);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new BusApp());
}
//程序开启,串口初始化
private void BusApp_Load(object sender, System.EventArgs e)
{//初始化串口并开启
mycom1.PortNum = iPort;
mycom1.BaudRate = iRate;
mycom1.ByteSize = bSize;
mycom1.Parity = bParity;
mycom1.StopBits = bStopBits;
mycom1.ReadTimeout = iTimeout;
if (this.OpenCom())
msg.AppendText("串口初始化成功……");
else
msg.AppendText("串口初始化失败!");
}
//显示包信息
public string dis_package(byte[] reb)
{
string temp = "";
foreach (byte b in reb)
temp += b.ToString("X2") + " ";
return temp;
}
//开串口
public bool OpenCom()
{
try
{
if (mycom1.Opened)
{
mycom1.Close();
mycom1.Open(); //打开串口
}
else
{
mycom1.Open();//打开串口
}
return true;
}
catch (Exception e)
{
MessageBox.Show("错误:" + e.Message);
return false;
}
}
//发送按钮
private void button1_Click(object sender, System.EventArgs e)
{
if (t_send.Text == "")
{ MessageBox.Show("发送数据为空!"); return; }
byte[] temp1 = mysendb();
SendPackage(temp1);
}
//发送数据包
public void SendPackage(byte[] bb)
{
int sendnumb = 0;
try
{
sendnumb = mycom1.Write(bb);
Thread.Sleep(50);
msg.AppendText("\r\n发送数据(" + sendnumb + "):" + dis_package(bb));
recb = mycom1.Read(50);
//if(recb.Length!=0)
msg.AppendText("\r\n接收到数据包:" + dis_package(recb));
}
catch
{ msg.AppendText("\r\n发送失败!"); return; }
}
//去掉发送数组中的空格
public string delspace(string putin)
{
string putout = "";
for (int i = 0; i < putin.Length; i++)
{
if (putin[i] != ' ')
putout += putin[i];
}
return putout;
}
//提取数据包
public byte[] mysendb()
{
string temps = delspace(t_send.Text);
byte[] tempb = new byte[50];
int j = 0;
for (int i = 0; i < temps.Length; i = i + 2, j++)
tempb[j] = Convert.ToByte(temps.Substring(i, 2), 16);
byte[] send = new byte[j];
Array.Copy(tempb, send, j);
return send;
}
//清空按钮
private void button3_Click(object sender, System.EventArgs e)
{
msg.Text = string.Empty;
}
//参数设置
//程序关闭,结束串口
private void BusApp_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
mycom1.Close();
}
//开启/关闭串口
private void button5_Click(object sender, System.EventArgs e)
{
if (button5.Text == "关闭串口")
{
mycom1.Close();
button5.Text = "开启串口";
msg.AppendText("\r\n串口被关闭……");
}
else
{
mycom1.Open();
button5.Text = "关闭串口";
msg.AppendText("\r\n串口成功开启……");
}
}
//退出
private void button13_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}网上copy的,用于生成dll做控件 --------------------编程问答-------------------- --------------------编程问答-------------------- 樓主想要乾什麼啊?沒看明白~
补充:.NET技术 , C#