using System; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using UAS_PLCDataReader.PublicMethod; using HslCommunication.Profinet; using HslCommunication.Profinet.Melsec; using HslCommunication; using System.Text; namespace UAS_PLCDataReader { public partial class MainWindow : Form { ModeBusTCPServer md = new ModeBusTCPServer(); public MainWindow() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName()); for (int i = 0; i < IpEntry.AddressList.Length; i++) { if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) if(IpEntry.AddressList[i].ToString()=="192.168.127.20") IP.Text = IpEntry.AddressList[i].ToString(); } Encoding1.Text = Encoding1.Items[0].ToString(); } private void OpenServer_Click(object sender, EventArgs e) { md.IP = IP.Text; md.Port = Port.Text; md.Open(); if (md.IsOpen) { OpenServer.Enabled = false; Port.Enabled = false; } } private void CloseServer_Click(object sender, EventArgs e) { md.Close(); if (!md.IsOpen) { OpenServer.Enabled = true; Port.Enabled = true; } } private void SEND_Click(object sender, EventArgs e) { //Console.WriteLine(BaseUtil.ASCIIToString("3A30313033343032314330303030303231374130303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303430DA")); MelsecMcNet melsec_net = new MelsecMcNet("192.168.1.120", 8880); OperateResult read = melsec_net.Read("D200", 5); if (read.IsSuccess) { Console.WriteLine(Encoding.ASCII.GetString(read.Content)); } else { //失败读取 MessageBox.Show(read.ToMessageShowString()); } } public static string HexStringToASCII(string hexstring) { byte[] bt = HexStringToBinary(hexstring); string lin = ""; for (int i = 0; i < bt.Length; i++) { lin = lin + bt[i] + " "; } string[] ss = lin.Trim().Split(new char[] { ' ' }); char[] c = new char[ss.Length]; int a; for (int i = 0; i < c.Length; i++) { a = Convert.ToInt32(ss[i]); c[i] = Convert.ToChar(a); } string b = new string(c); return b; } public static byte[] HexStringToBinary(string hexstring) { string[] tmpary = hexstring.Trim().Split(' '); byte[] buff = new byte[tmpary.Length]; for (int i = 0; i < buff.Length; i++) { buff[i] = Convert.ToByte(tmpary[i], 16); } return buff; } } }