C# SerialPort 读写三菱FX系列PLC
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C# SerialPort 读写三菱FX系列PLC,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4594字,纯文字阅读大概需要7分钟。
内容图文
1:串口初始化
com = new SerialPort("COM3", 9600, Parity.Even, 7, StopBits.One);
2:打开关闭串口
1 if (com.IsOpen) 2 { 3 com.Close();//关闭 4 } 5 6 com.Open();//打开
3:C# ASCII转字符及字符转ASCII
1 public static string Chr(int asciiCode) 2 { 3 if (asciiCode >= 0 && asciiCode <= 255) 4 { 5 System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); 6 byte[] byteArray = new byte[] { (byte)asciiCode }; 7 string strCharacter = asciiEncoding.GetString(byteArray); 8 return (strCharacter); 9 } 10 else 11 { 12 throw new Exception("ASCII Code is not valid."); 13 } 14 } 15 16 17 18 public static int Asc(string character) 19 { 20 if (character.Length == 1) 21 { 22 System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); 23 int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0]; 24 return (intAsciiCode); 25 } 26 else 27 { 28 throw new Exception("Character is not valid."); 29 } 30 }
4:写入串口的命令字符串的和校验
1 /// <summary> 2 /// 写入串口的命令字符串的和校验 3 /// </summary> 4 /// <param name="data"></param> 5 /// <returns></returns> 6 public string SumCheck(string data) 7 { 8 int sum = 0; 9 for (int i = 0; i < data.Length; i++) 10 { 11 sum += Asc(data.Substring(i, 1)); 12 } 13 14 string res = sum.ToString("X"); 15 res = res.Substring(res.Length - 2, 2); 16 17 return res; 18 }
5:写入PLC
1 private void btnWrite_Click(object sender, EventArgs e) 2 { 3 string[] write = new string[] { "2","2"}; //将准备写入PLC的值 4 //将要写入的值转换成16进制数,补齐两个字节,注意高低字节需要交换 5 string sWriteData = ""; 6 for (int i = 0; i < write.Length; i++) 7 { 8 int val = Convert.ToInt32(write[i].Length>0?write[i]:"0"); 9 string s = val.ToString("X"); 10 while (s.Length<4) 11 { 12 s = "0" + s; 13 } 14 sWriteData += s.Substring(2,2)+s.Substring(0,2); 15 } 16 17 MessageBox.Show(sWriteData); 18 //写入命令,1表示写入,1194表示D202这个地址的16进制,04表示D202,D203为4个BYTE,1194=(202*2)+4096的16进制数,至于用它表示D202的起始位置,三菱故意要这么麻烦了. 19 sWriteData = "1119404" + sWriteData + Chr(3); 20 //chr(2)和chr(3)是构成命令的标志字符,然后加上校验和,命令组织完成 21 sWriteData = Chr(2) + sWriteData + SumCheck(sWriteData); 22 23 MessageBox.Show(sWriteData); 24 //写入串口 25 com.Write(sWriteData); 26 //byte[] data = Encoding.ASCII.GetBytes(sWriteData); 27 //com.Write(data,0,data.Length); 28 }
6:读PLC
1 private void btnRead_Click(object sender, EventArgs e) 2 { 3 4 this.txtRead0.Clear(); 5 string sReadData = ""; 6 //在读PLC中的数据之前,需要先发个指令给它,让它将数据发送到串口,下面的字符串中,chr(2),chr(3)为PLC命令的格式标志,0119404中,0表示读,1194表示D202的起始地址,04表示读D202,D203两个字,共4个字节,66为0119404和chr(3)的校验和,向串口写入"读"命令,其实和向plc地址中写入数据是一样的,只是没有数据,用0表示读 7 string sReadCmd = Chr(2) + "0119404" + Chr(3) + "66"; 8 com.Write(sReadCmd); 9 //等待1秒钟 10 System.Threading.Thread.Sleep(1000); 11 // 从串口读数据 12 byte[] data = new byte[1024]; 13 com.Read(data, 0, 1024); 14 15 //如果首位为2,则表示数据有效.这里有个问题,在第二次读,第2位才为'2',第三次又是首位为2,需要再测试 16 if (data[0]==2) 17 { 18 string sReceiveData = System.Text.Encoding.ASCII.GetString(data); 19 //MessageBox.Show(sReceiveData); 20 //解析命令,将读到的字符解析成数字,注意高低位的转换 21 for (int i = 1; i < 8; i += 4) 22 { 23 string sLow = sReceiveData.Substring(i,2); 24 string sHigh = sReceiveData.Substring(i + 2, 2); 25 //int res = Convert.ToInt32(sHigh)+ Convert.ToInt32(sLow); 26 int res = Convert.ToInt32(sHigh,16) + Convert.ToInt32(sLow,16); 27 28 this.txtRead0.Text += res.ToString() + ","; 29 } 30 31 }
内容总结
以上是互联网集市为您收集整理的C# SerialPort 读写三菱FX系列PLC全部内容,希望文章能够帮你解决C# SerialPort 读写三菱FX系列PLC所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。