2006-09-30

C#结合串口通信类实现串口通信源代码

来源: 本站收集整理 作者:佚名 评论 0 条
 
//
this.button5.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.button5.Location = new System.Drawing.Point(440, 504);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(64, 23);
this.button5.TabIndex = 9;
this.button5.Text = "关闭串口";
this.button5.Click = new System.EventHandler(this.button5_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(512, 533);
this.Controls.Add(this.button5);
this.Controls.Add(this.textBox8);
this.Controls.Add(this.label7);
this.Controls.Add(this.button4);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.t_send);
this.Controls.Add(this.msg);
this.Controls.Add(this.label2);
this.Controls.Add(this.button3);
this.Name = "Form1";
this.Text = "串口通讯(小y设计)";
this.Closing = new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.Load = new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//程序开启,串口初始化
private void Form1_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("串口初始化成功……\r\n");
else
msg.AppendText("串口初始化失败!\r\n");
}
//显示包信息
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();
int sendnumb=0;
try
{
sendnumb=mycom1.Write(temp1);
msg.AppendText("\r\n发送数据(" sendnumb "):" dis_package(temp1));
共6页: 上一页 [1] [2] [3] [4] 5 [6] 下一页

(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:C# 2.0中泛型编程思想分析  
下一篇:Ruby的基础
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号