实例介绍
【实例截图】
【核心代码】
/*
* 由SharpDevelop创建。
* 用户: hp
* 日期: 2015/6/4
* 时间: 15:20
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
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 串口通信程序1
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
public void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
}
public void Button1Click(object sender, EventArgs e)//点击按钮获取串口
{
try {
{
serialPort1.DataBits=7;
serialPort1.BaudRate=Convert.ToInt16( textBox1.Text.ToString());
if (serialPort1.PortName==comboBox1.Text)//判断计算机端口是否等于设定端口
{ serialPort1.Open();
this.button1.Text="关闭端口";
}
else
{serialPort1.Close();
this.button1.Text="打开端口";
}
}
}
catch
{
MessageBox.Show("请设定波特率");
}
}
/////以上是打开端口的程序
private bool timerIsActive= false;//最好初始化
public void MainFormLoad(object sender, EventArgs e)//获取计算机的端口与设定端口
{
string[] ports = SerialPort.GetPortNames();//获取计算的可用串口
foreach(string port in ports)
{this.textBox4.Text=port.ToString();
this .comboBox1.Text=port.ToString();
serialPort1.PortName=textBox4.Text;
}
for (int i=0;i<=20;i )//for循环增加串口
{
comboBox1.Items.Add("COM" i.ToString());//下拉框中出现串口
comboBox1.SelectedItem=1;
}
for (int q=0;q<=20;q )//for循环增加发送的数据
{ string str=Convert.ToString(q,16);
if (str.Length==1)
{str="0" str;
str ="0x" str;
comboBox2.Items.Add(str);//下拉框中出现串口
}
}
}
public static int Asc(string character)//字符串转为ASCII码
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
public void Button3Click(object sender, EventArgs e)//发送数据的按钮
{ byte[]date=new byte[1];
string rtu;
try {
rtu=comboBox2.Text;//发送的数据在下拉框中显示
rtu=rtu.Substring(2,2);
date[0]=Convert.ToByte(rtu,16);}
catch
{
MessageBox.Show ("请使用正确的字符串");
}
date[0]=1;
if(serialPort1.IsOpen)
{
try {
serialPort1.Write(date,0,1);
this.textBox2.Text=date[0] "";
}
catch
{
MessageBox.Show ("端口没有打开,请检查端口");
}
}
else
{
MessageBox.Show ("端口没有打开,请检查端口");
}
}
public void Button4Click(object sender, EventArgs e)//发送代码
{
//02h,30h,30h,30h,38h,30h,39h,32h,03h,35h,44h
//STX,0,0,0,8,0,0,2,ETX,5,D 发送数据
//02,30,34,03,36,37 PLC返回来的数据 30对应的是0 34对应的是4 对应的0000 0100
char[]c={(char)2,(char)48,(char)48,(char)48,(char)56,(char)48,(char)48,(char)50,(char)3,(char)53,(char)68};
//char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
int i=0;
string s="";
for (i=0;i<11;i )
{
s=s c[i].ToString();
}
serialPort1.Write(s);
textBox3.Text=s;
timer1.Enabled=true;
}
public void Timer1Tick(object sender, EventArgs e)
{
string data1="";
bool sc;
SolidBrush bush1 = new SolidBrush(Color.Red);
SolidBrush bush2 = new SolidBrush(Color.Green);
Graphics g1 = this.pictureBox1.CreateGraphics();
Graphics g2 = this.pictureBox2.CreateGraphics();
Graphics g3 = this.pictureBox3.CreateGraphics();
Graphics g4 = this.pictureBox4.CreateGraphics();
Graphics g5 = this.pictureBox5.CreateGraphics();
Graphics g6 = this.pictureBox6.CreateGraphics();
Graphics g7= this.pictureBox7.CreateGraphics();
Graphics g8= this.pictureBox8.CreateGraphics();
data1=serialPort1.ReadExisting();
int b = data1.Length;
if (b==8)
{
byte [] h=strToToHexByte(data1.Substring(1,2));//提取有用字符串转化成十进制数组
byte [] p=strToToHexByte(data1.Substring(3,2));
int oy =Convert.ToInt32(h [0]);//将十进制数组转换成整形整数
int oj=Convert.ToInt32(p [0]);
string er = Convert.ToString(oy, 2);//将整数转化成二进制
er=Fill(er);
//string er1=Parent.ToSByte(er);
string re= Convert.ToString(oj, 2);
textBox6.Text=er ;
textBox7.Text=re ;
timer1.Enabled=false;
if (er!=""&&re!="")
{
sc=true ;
MessageBox.Show("状态读取成功");
}
else
{
sc=false;
MessageBox.Show("状态读取失败");
}
if (sc==true )
{
int? sum =null;
bool ?res=null;
for (int i = 0 ;i <er.Length; i )
{
sum =Convert.ToUInt16(er.Substring(i,1));
switch (sum)
{
case 0: res=false;
break;
case 1: res=true ;
break ;
}
if (i==0&& res ==true )//x07状态
{
g7.FillEllipse(bush1, 0, 0, 20, 20);
}
else if (i==0&&res ==false)
{
g7.FillEllipse(bush2, 0, 0, 20, 20);
}
if (i==1&&res==true)//x06状态
{
g2.FillEllipse(bush1, 0, 0, 20, 20);
}
else if (i==1&&res ==false)
{
g2.FillEllipse(bush2, 0, 0, 20, 20);
}
if (i==2&&res==true )//x05状态
{
g1.FillEllipse(bush1, 0, 0, 20, 20);
}
else if (i==2&&res==false)
{
g1.FillEllipse(bush2, 0, 0, 20, 20);
}
if (i==3&&res==true )//x04状态
{
g3.FillEllipse(bush1, 0, 0, 20, 20);
}
else if (i==3&&res==false)
{
g3.FillEllipse(bush2, 0, 0, 20, 20);
}
if (i==4&&res==true )//x03状态
{
g4.FillEllipse(bush1, 0, 0, 20, 20);
}
else if (i==4&&res ==false)
{
g4.FillEllipse(bush2, 0, 0, 20, 20);
}
if (i==5&&res==true )//x02状态
{
g5.FillEllipse(bush1, 0, 0, 20, 20);
}
else if (i==5&&res==false)
{
g5.FillEllipse(bush2, 0, 0, 20, 20);
}
if (i==6&&res==true )//x01状态
{
g6.FillEllipse(bush1, 0, 0, 20, 20);
}
else if (i==6&&res==false)
{
g6.FillEllipse(bush2, 0, 0, 20, 20);
}
if (i==7&&res ==true )//x0状态
{
g8.FillEllipse(bush1, 0, 0, 20, 20);
}
else if (i==7&&res==false)
{
g8.FillEllipse(bush2, 0, 0, 20, 20);
}
}
}
else{
MessageBox.Show("读取有误");
return;
}
}
}
public static byte[] strToToHexByte(string hexString)//字符串转字节数组
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString = " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i )
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
void PictureBox6Click(object sender, EventArgs e)
{
}
public static string Fill(string pStrSource)//位数不够就补0
{
switch(pStrSource.Length)
{
case 1:return "0000000" pStrSource;
case 2:return "000000" pStrSource;
case 3:return "00000" pStrSource;
case 4:return "0000" pStrSource;
case 5:return "000" pStrSource;
case 6:return "00" pStrSource;
case 7:return "0" pStrSource;
default:return pStrSource;
}
}
void Label19Click(object sender, EventArgs e)
{
}
public void Button2Click(object sender, System.EventArgs e)//启动按钮m0.0
{
char[]c={(char)2,(char)49,(char)48,(char)49,(char)48,(char)48,(char)48,(char)49,(char)48,(char)49,(char)3,(char)66,(char)55};
//char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
int i=0;
string s="";
for (i=0;i<13;i )
{
s=s c[i].ToString();
}
serialPort1.Write(s);
ghu ();
}
public void Button5Click(object sender, EventArgs e)//停止按钮m0.1
{
char[]c={(char)2,(char)49,(char)48,(char)49,(char)48,(char)48,(char)48,(char)49,(char)48,(char)50,(char)3,(char)66,(char)56};
//char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
int i=0;
string s="";
for (i=0;i<13;i )
{
s=s c[i].ToString();
}
serialPort1.Write(s);
ghu();
}
public void ghu()//读取y的状态
{
char[]c={(char)2,(char)48,(char)48,(char)48,(char)65,(char)48,(char)48,(char)49,(char)3,(char)57,(char)53};
//char[] c = { (char)2, (char)48, (char)49, (char)48, (char)48, (char)48, (char)50, (char)48, (char)3, (char)53, (char)54 };
int i=0;
string s="";
for (i=0;i<11;i )
{
s=s c[i].ToString();
}
serialPort1.Write(s);
timer2.Enabled=true;
}
void Timer2Tick(object sender, EventArgs e)
{
string data1="";
bool tr;
SolidBrush bush3 = new SolidBrush(Color.Red);
SolidBrush bush4 = new SolidBrush(Color.Green);
Graphics g9 = this.pictureBox9.CreateGraphics();
Graphics g10 = this.pictureBox10.CreateGraphics();
data1=serialPort1.ReadExisting();
if (data1 != null)
{
int b = data1.Length;
if (b>0)
{
byte [] h=strToToHexByte(data1.Substring(1,2));//提取有用字符串转化成十进制数组
byte [] p=strToToHexByte(data1.Substring(3,2));
int oy =Convert.ToInt32(h [0]);//将十进制数组转换成整形整数
int oj=Convert.ToInt32(p [0]);
string eu=Convert.ToString(oy, 2);//将整数转化成二进制
eu =Fill(eu);
//string er1=Parent.ToSByte(er);
string ru= Convert.ToString(oj, 2);
ru =Fill(ru);
if (eu!=""&&ru!="")
{
tr=true;
}
else{
tr=false;
MessageBox.Show("y的状态读取不成功");
}
if (tr==true )
{
int? sum =null;
bool ?res=null;
for (int l = 0 ;l <eu.Length; l )
{
sum =Convert.ToUInt16(eu.Substring(l,1));
switch (sum)
{
case 0: res=false;
break;
case 1: res=true ;
break ;
}
if(l==8&&res==true)
{
g9.FillEllipse(bush3, 0, 0, 20, 20);
}
else
{
g10.FillEllipse(bush4, 0, 0, 20, 20);
}
if(l==7&&res==true)
{
g10.FillEllipse(bush3, 0, 0, 20, 20);
}
else
{
g10.FillEllipse(bush4, 0, 0, 20, 20);
}
}
}
}
else
{
MessageBox.Show("y的状态读取不成功");
}
}
else{
MessageBox.Show("通信超时");
}
timer2.Enabled=false;
}
}
}
标签: 通讯
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论