簡單介紹:
項目上需要與多家公司做接口對接。我們提供接口的有,其他公司提供的接口也有。所有的接口全部對接完了,遇到一個非常棘手的問題,需要獲取甲方船廠設備上的狀態,就給了一個文檔,文檔上寫了IP、端口、協議、一些地址,沒有API文檔,拿到手上一面懵逼,這怎么玩兒。。。。
文檔如下:
百度百科:
Modbus是一種串行通信協議,是Modicon公司(現在的施耐德電氣 Schneider Electric)於1979年為使用可編程邏輯控制器(PLC)通信而發表。Modbus已經成為工業領域通信協議的業界標准(De facto),並且現在是工業電子設備之間常用的連接方式。
看上去好像跟Socket差不多,本身又不是工業領域出身的,大概知道是一種工業領域通用的一套通信標准,下面直接上DEMO示例
第一步:下載類庫
使用的類庫已上傳百度雲盤:
鏈接:https://pan.baidu.com/s/1JtaGC0r17jjnQPMhkMKRJg
提取碼:wagl
第二步:引入類庫
第三步:引入命名空間
1 using HslCommunication.ModBus; 2 using HslCommunication;
第四步:初始化對象
1 初始化方式一、 2 private ModBusTcpClient busTcpClient = new ModBusTcpClient("192.168.1.195", 502, 0xFF); // ip、端口、站號(默認為0xFF) 3 4 初始化方式二、 5 private ModBusTcpClient busTcpClient = new ModBusTcpClient("192.168.1.195"); // 端口號502,站號0
第五步:開啟連接
1 開啟連接: 2 busTcpClient.ConnectServer(); 3 4 關閉連接: 5 busTcpClient.ConnectClose( );
第六步:讀寫操作
1 private void userButton30_Click(object sender, EventArgs e) 2 { 3 // 讀取操作 4 bool coil100 = busTcpClient.ReadCoil("100").Content; // 讀取線圈100的通斷 5 short short100 = busTcpClient.ReadInt16("100").Content; // 讀取寄存器100的short值 6 ushort ushort100 = busTcpClient.ReadUInt16("100").Content; // 讀取寄存器100的ushort值 7 int int100 = busTcpClient.ReadInt32("100").Content; // 讀取寄存器100-101的int值 8 uint uint100 = busTcpClient.ReadUInt32("100").Content; // 讀取寄存器100-101的uint值 9 float float100 = busTcpClient.ReadFloat("100").Content; // 讀取寄存器100-101的float值 10 long long100 = busTcpClient.ReadInt64("100").Content; // 讀取寄存器100-103的long值 11 ulong ulong100 = busTcpClient.ReadUInt64("100").Content; // 讀取寄存器100-103的ulong值 12 double double100 = busTcpClient.ReadDouble("100").Content; // 讀取寄存器100-103的double值 13 string str100 = busTcpClient.ReadString("100", 5).Content;// 讀取100到104共10個字符的字符串 14 15 // 寫入操作 16 busTcpClient.WriteCoil("100", true);// 寫入線圈100為通 17 busTcpClient.Write("100", (short)12345);// 寫入寄存器100為12345 18 busTcpClient.Write("100", (ushort)45678);// 寫入寄存器100為45678 19 busTcpClient.Write("100", 123456789);// 寫入寄存器100-101為123456789 20 busTcpClient.Write("100", (uint)123456778);// 寫入寄存器100-101為123456778 21 busTcpClient.Write("100", 123.456);// 寫入寄存器100-101為123.456 22 busTcpClient.Write("100", 12312312312414L);//寫入寄存器100-103為一個大數據 23 busTcpClient.Write("100", 12634534534543656UL);// 寫入寄存器100-103為一個大數據 24 busTcpClient.Write("100", 123.456d);// 寫入寄存器100-103為一個雙精度的數據 25 busTcpClient.Write("100", "K123456789"); 26 27 }
項目界面:
完整代碼(粗略寫了DEMO,有不規范的地方,大佬莫怪):
1 using System; 2 using System.Windows.Forms; 3 using HslCommunication.ModBus; 4 using HslCommunication; 5 6 namespace ModbusDemo 7 { 8 public partial class Form1 : Form 9 { 10 public Form1() 11 { 12 InitializeComponent(); 13 } 14 /// <summary> 15 /// 初始化類 16 /// </summary> 17 private ModbusTcpNet busTcpClient =null; 18 /// <summary> 19 /// 監聽狀態 20 /// </summary> 21 private bool IsEnable = false; 22 private void Form1_Load(object sender, EventArgs e) 23 { 24 txtPort.Text = "502"; 25 txtIp.Text = "172.30.16.220"; 26 textBox2.Text = "00141"; 27 } 28 /// <summary> 29 /// 開啟服務 30 /// </summary> 31 /// <param name="sender"></param> 32 /// <param name="e"></param> 33 private void Button5_Click(object sender, EventArgs e) 34 { 35 try 36 { 37 if (IsEnable) 38 { 39 MessageBox.Show("請勿重復建立連接!"); 40 return; 41 } 42 string ip = txtIp.Text.Trim(); 43 int port = Convert.ToInt32(txtPort.Text); 44 if (ip==null || ip=="") 45 { 46 MessageBox.Show("ip不能為空!"); 47 return; 48 } 49 busTcpClient = new ModbusTcpNet(ip, port, 0x01); 50 OperateResult res = busTcpClient.ConnectServer(); 51 if (res.IsSuccess==true) //接收狀態返回值 52 { 53 IsEnable = true; 54 MessageBox.Show("開啟連接成功"); 55 } 56 else 57 { 58 MessageBox.Show("開啟連接失敗"); 59 } 60 } 61 catch (Exception ex) 62 { 63 MessageBox.Show("開啟連接失敗!", ex.Message.ToString()); 64 } 65 } 66 private void Button6_Click(object sender, EventArgs e) 67 { 68 try 69 { 70 if (!IsEnable) 71 { 72 MessageBox.Show("尚未建立連接!"); 73 return; 74 } 75 busTcpClient.ConnectClose(); 76 IsEnable = false; 77 MessageBox.Show("關閉連接成功!"); 78 } 79 catch (Exception ex) 80 { 81 MessageBox.Show("關閉連接失敗!", ex.Message.ToString()); 82 } 83 } 84 85 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 86 { 87 Application.Exit(); 88 } 89 90 private void Button3_Click(object sender, EventArgs e) 91 { 92 try 93 { 94 if (!IsEnable) 95 { 96 MessageBox.Show("尚未建立連接!"); 97 return; 98 } 99 if (busTcpClient == null) 100 { 101 MessageBox.Show("尚未初始化對象!"); 102 return; 103 } 104 string txt = textBox2.Text.Trim(); 105 if (txt=="") 106 { 107 MessageBox.Show("地址不能為空!"); 108 return; 109 } 110 bool coil100 = busTcpClient.ReadCoil(txt).Content; // 讀取線圈100的通斷 111 textBox1.Text = ""; 112 MessageBox.Show("監聽成功!"); 113 textBox1.Text = coil100 == true ? "true" : "false"; 114 } 115 catch (Exception ex) 116 { 117 MessageBox.Show(ex.Message.ToString()); 118 } 119 } 120 } 121 }
項目:
鏈接:https://pan.baidu.com/s/1a3pnftQ2QAZFJcoKl9p_qQ
提取碼:rhsl