2006-09-30

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

来源: 本站收集整理 作者:佚名 评论 0 条
 
recb=mycom1.Read(50);
//if(recb.Length!=0)
msg.AppendText("\r\n接收到数据包:" dis_package(recb));
}
catch
{msg.AppendText("\r\n发送失败!");return;}

//OpenCom();
}
//去掉发送数组中的空格
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)
{
t_send.Text=string.Empty;
msg.Text=string.Empty;
}

//参数设置
private void button2_Click(object sender, System.EventArgs e)
{
iPort=Convert.ToInt16(t_port.Text); //1,2,3,4
iRate=Convert.ToInt16(t_rate.Text); //1200,2400,4800,9600
bSize=Convert.ToByte(t_bytesize.Text,10); //8 bits
bParity=Convert.ToByte(t_parity.Text,10); // 0-4=no,odd,even,mark,space
bStopBits=Convert.ToByte(t_stopbyte.Text,10); // 0,1,2 = 1, 1.5, 2
//iTimeout=3;
if(this.OpenCom())
msg.AppendText("串口初始化成功……\r\n");
else
msg.AppendText("串口初始化失败!\r\n");
}
//程序关闭,结束串口
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
mycom1.Close();
}

private void button5_Click(object sender, System.EventArgs e)
{
if(mycom1.Opened)
{
mycom1.Close();
button5.Text="开启串口";
msg.AppendText("\r\n串口被关闭……");
}
else
{
mycom1.Open();
button5.Text="关闭串口";
msg.AppendText("\r\n串口成功开启……");
}
}
}
}

注重:发送数据包的格式是16进制数据如:02 45 66 FA 中间可以有或者没有空格,但要保证有偶数位

http://www.cnblogs.com/tuyile006/archive/2006/09/29/517882.html


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

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