基本信息
源码名称:三菱串口通讯程序源代码下载
源码大小:0.07M
文件格式:.zip
开发语言:C#
更新时间:2019-04-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

/*
 * 由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;
		}


	}
}