1:PLC程序
2:C#程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace _200smart_mosbusTcp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Socket sk;
Socket sk1;
public void skr()
{
while (true)
{
byte[] by = new byte[1024];
int a = sk.Receive(by);
textBox2.Text = BitConverter.ToString(by, 0, a);
if (a== 23)
{
byte[] b3 = new byte[2];
b3[0] = by[10];
b3[1] = by[9];
byte[] b4 = new byte[2];
b4[0] = by[12];
b4[1] = by[11];
byte[] b5 = new byte[2];
b5[0] = by[14];
b5[1] = by[13];
byte[] b6 = new byte[2];
b6[0] = by[16];
b6[1] = by[15];
byte[] b7 = new byte[2];
b7[0] = by[18];
b7[1] = by[17];
byte[] b8 = new byte[4];
b8[0] = by[22];
b8[1] = by[21];
b8[2] = by[20];
b8[3] = by[19];
textBox3.Text = BitConverter.ToInt16(b3, 0).ToString();
textBox4.Text = BitConverter.ToInt16(b4, 0).ToString();
textBox5.Text = BitConverter.ToInt16(b5, 0).ToString();
textBox6.Text = BitConverter.ToInt16(b6, 0).ToString();
textBox7.Text = BitConverter.ToInt16(b7, 0).ToString();
textBox8.Text = BitConverter.ToSingle(b8, 0).ToString();
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sk.Connect(new IPEndPoint(IPAddress.Parse("192.168.0.10"), 502));
sk1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sk1.Connect(new IPEndPoint(IPAddress.Parse("192.168.0.1"), 502));
textBox1.AppendText("Tcp1打开");
textBox1.AppendText("Tcp2打开");
Thread th = new Thread(skr);
th.IsBackground = true;
th.Start();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] bb = { 0x97, 0x79, 0x00, 0x00, 0x00, 0x06, 0x04, 05, 0x00, 0x00, 0xFF,0x00 };
sk.Send(bb);
}
private void button2_Click(object sender, EventArgs e)
{
byte[] bb = { 0x97, 0x79, 0x00, 0x00, 0x00, 0x06, 0x04, 05, 0x00, 0x00, 0x00, 0x00 };
sk.Send(bb);
}
private void timer1_Tick(object sender, EventArgs e)
{
byte[] bb = { 0x97, 0x79, 0x00, 0x00, 0x00, 0x06, 0x04, 03, 0x00, 0x00, 0x00, 0x07};
sk.Send(bb);
}
private void button4_Click(object sender, EventArgs e)
{
byte[] bb = { 0x97, 0x79, 0x00, 0x00, 0x00, 0x06, 0x04, 05, 0x00, 0x01, 0xff, 0x00 };
sk.Send(bb);
}
private void button3_Click(object sender, EventArgs e)
{
byte[] bb = { 0x97, 0x79, 0x00, 0x00, 0x00, 0x06, 0x04, 05, 0x00, 0x01, 0x00, 0x00 };
sk.Send(bb);
}
private void button5_Click(object sender, EventArgs e)
{
ushort A = Convert.ToUInt16(textBox9.Text.Trim());
byte[] AA = BitConverter.GetBytes(A);
ushort B = Convert.ToUInt16(textBox10.Text.Trim());
byte[] BB = BitConverter.GetBytes(B);
ushort C = Convert.ToUInt16(textBox11.Text.Trim());
byte[] CC = BitConverter.GetBytes(C);
ushort D = Convert.ToUInt16(textBox12.Text.Trim());
byte[] DD = BitConverter.GetBytes(D);
ushort E = Convert.ToUInt16(textBox13.Text.Trim());
byte[] EE = BitConverter.GetBytes(E);
Single F = Convert.ToSingle(textBox14.Text.Trim());
byte[] FF = BitConverter.GetBytes(F);
List<byte> Ls = new List<byte>();
//多字写入https://www.cnblogs.com/ioufev/articles/10830028.html Modbus Tcp 协议说明;
byte[] bt = { 0x97, 0x79, 0x00, 0x00, 0x00, 0x15, 0x04,0x10,0x00,0x00,0x00,0x07,0xE }; //多字写入 https://www.cnblogs.com/ioufev/articles/10830028.html
// byte[] bt = { 0x97, 0x79, 0x00, 0x00, 0x00, 0x09, 0x04, 0x10, 0x00, 0x00, 0x00, 0x01, 0x02 }; 单个字写入;
Ls.Add(bt[0]);
Ls.Add(bt[1]);
Ls.Add(bt[2]);
Ls.Add(bt[3]);
Ls.Add(bt[4]);
Ls.Add(bt[5]);
Ls.Add(bt[6]);
Ls.Add(bt[7]);
Ls.Add(bt[8]);
Ls.Add(bt[9]);
Ls.Add(bt[10]);
Ls.Add(bt[11]);
Ls.Add(bt[12]);
Ls.Add(AA[1]);
Ls.Add(AA[0]);
Ls.Add(BB[1]);
Ls.Add(BB[0]);
Ls.Add(CC[1]);
Ls.Add(CC[0]);
Ls.Add(DD[1]);
Ls.Add(DD[0]);
Ls.Add(EE[1]);
Ls.Add(EE[0]);
Ls.Add(FF[3]);
Ls.Add(FF[2]);
Ls.Add(FF[1]);
Ls.Add(FF[0]);
byte[] Lss = Ls.ToArray();
sk.Send(Lss);
sk1.Send(Lss);
}
}
}
三:监视结果
4:资料参考:https://www.cnblogs.com/ioufev/articles/10830028.html