using PLCS7; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace PLCConnector { public partial class FormPLCConf : Form { PLCLinker pl; public FormPLCConf() { InitializeComponent(); pl = (PLCLinker)Monitor.Monitor.PLC; Control.ControlCollection cc = this.Controls; foreach (Control c in cc) { c.Enabled = false; } btn_PLCLink.Enabled = true; } public static void parkingSpaceDBTest(AbstractPLCLinker pl, Random rnd) { if (!pl.isConnected) { pl.PLCConnect(); } else { //读车位表信息 List psList = pl.ReadFromPLC(PLCDataType.parkingSpace, 1); psList.AddRange(pl.ReadFromPLC(PLCDataType.parkingSpace, 2)); foreach (var ps in psList) { Console.WriteLine(((ParkingSpaceStru)ps).ToString()); } //写入车位状态信息 ParkingSpaceStru pss = new ParkingSpaceStru { parkingSpace = (short)rnd.Next(1, 3), spaceStatus = (short)rnd.Next(0, 3) }; pl.WriteToPLC(pss, PLCDataType.central); Console.WriteLine("写入状态值"); } } public static void terminalDBTest(AbstractPLCLinker pl, Random rnd) { if (!pl.isConnected) { pl.PLCConnect(); } else { //读终端块数据 List tList = pl.ReadFromPLC(PLCDataType.terminal, 1); foreach (var term in tList) { Console.WriteLine(((TerminalStru)term).ToString()); } //模拟中控写数据 TerminalStru tsCentral = new TerminalStru { terminalID = 1, paymentStatus = (short)rnd.Next(100, 999), licVerification = -1, parkingFee = (short)rnd.Next(100, 999), userType = (short)rnd.Next(100, 999) }; //模拟终端写数据 TerminalStru tsTerminal = new TerminalStru { terminalID = 1, terminalStatus = (short)rnd.Next(100, 999), btnStatus = (short)rnd.Next(100, 999), licenseCodeA = (short)rnd.Next(100, 999), licenseCodeB = -1, licenseCodeC = (short)rnd.Next(100, 999), receiptNum = (short)rnd.Next(100, 999) }; //pl.WriteToPLC(tsCentral, PLCDataType.central); pl.WriteToPLC(tsTerminal, PLCDataType.terminal); } } private void button1_Click(object sender, EventArgs e) { PLCDataType type = PLCDataType.parkingSpace; int offset = 0; int value = 0; try { offset = Int32.Parse(textBox2.Text); value = Int32.Parse(textBox3.Text); } catch { MessageBox.Show("异常"); return; } switch (textBox4.Text) { case "中控": type = PLCDataType.central; break; case "终端1": type = PLCDataType.terminal; break; case "终端2": type = PLCDataType.terminal; offset += 42; break; case "终端3": type = PLCDataType.terminal; offset += 42 * 2; break; case "车位1": type = PLCDataType.parkingSpace; break; } if (value < 32768) { pl.WriteAccordingToOffset(type, offset, (short)value); } else { pl.WriteAccordingToOffset(type, offset, value); } } private void button2_Click(object sender, EventArgs e) { //PLCDataType type = PLCDataType.parkingSpace; //int id = -1; //switch ((string)textBox5.Text) //{ // case "中控": // type = PLCDataType.central; id = 0; // break; // case "终端1": // type = PLCDataType.terminal; id = 1; // break; // case "终端2": // type = PLCDataType.terminal; id = 2; // break; // case "终端3": // type = PLCDataType.terminal; id = 3; // break; // case "车位1": // type = PLCDataType.parkingSpace; id = 4; // break; //} //string result = ""; //try //{ // if (id == 4) // { // List list = pl.ReadFromPLC(type, 1); // result = ((ParkingSpaceStru)list[0]).ToString(); // } // else if (id > 0) // { // List list = pl.ReadFromPLC(type, id); // result = ((TerminalStru)list[0]).ToString(); // } // else if (id == 0) // { // List list = pl.ReadFromPLC(type, id); // result = ((MainBlockStru)list[0]).ToString(); // } //} //catch { Console.WriteLine("异常"); return; } //textBox1.Text = result.Replace(",", "\r\n"); display(textBox5.Text, textBox1); } private void button3_Click(object sender, EventArgs e) { //PLCDataType type = PLCDataType.parkingSpace; //int id = -1; //switch ((string)textBox6.Text) //{ // case "中控": // type = PLCDataType.central; id = 0; // break; // case "终端1": // type = PLCDataType.terminal; id = 1; // break; // case "终端2": // type = PLCDataType.terminal; id = 2; // break; // case "终端3": // type = PLCDataType.terminal; id = 3; // break; // case "车位1": // type = PLCDataType.parkingSpace; id = 4; // break; //} //string result = ""; //try //{ // if (id == 4) // { // List list = pl.ReadFromPLC(type, 1); // result = ((ParkingSpaceStru)list[0]).ToString(); // } // else if (id > 0) // { // List list = pl.ReadFromPLC(type, id); // result = ((TerminalStru)list[0]).ToString(); // } // else if (id == 0) // { // List list = pl.ReadFromPLC(type, id); // result = ((MainBlockStru)list[0]).ToString(); // } //} //catch { Console.WriteLine("异常"); return; } //textBox7.Text = result.Replace(",", "\r\n"); display(textBox6.Text, textBox7); } private void button4_Click(object sender, EventArgs e) { //PLCDataType type = PLCDataType.parkingSpace; //int id = -1; //switch ((string)textBox8.Text) //{ // case "中控": // type = PLCDataType.central; id = 0; // break; // case "终端1": // type = PLCDataType.terminal; id = 1; // break; // case "终端2": // type = PLCDataType.terminal; id = 2; // break; // case "终端3": // type = PLCDataType.terminal; id = 3; // break; // case "车位1": // type = PLCDataType.parkingSpace; id = 4; // break; //} //string result = ""; //try //{ // if (id == 4) // { // //List list = pl.ReadFromPLC(type, 1); // result = Monitor.Monitor.parkingSpaceInfo[1];//((ParkingSpaceStru)list[0]).ToString(); // } // else if (id > 0) // { // List list = pl.ReadFromPLC(type, id); // result = ((TerminalStru)list[0]).ToString(); // } // else if (id == 0) // { // List list = pl.ReadFromPLC(type, id); // result = ((MainBlockStru)list[0]).ToString(); // } //} //catch { Console.WriteLine("异常"); return; } //textBox9.Text = result.Replace(",", "\r\n"); display(textBox8.Text, textBox9); } private void display(string target, TextBox tb) { if (target == "") return; string result = ""; PLCDataType type = PLCDataType.parkingSpace; int id = -1; int index = 0; try { if (target.Length > 2) index = int.Parse(target.Substring(2, target.Length - 2)); } catch (Exception ex) { index = 0; Console.WriteLine("aaaaa"); } try { string block = target.Substring(0, 2); switch (block) { case "中控": type = PLCDataType.central; id = 1001; break; case "终端": type = PLCDataType.terminal; id = 2000 + (index > 0 ? index - 1 : 0); break; case "车位": type = PLCDataType.parkingSpace; id = 3000 + (index > 0 ? index - 1 : 0); break; case "错误": type = PLCDataType.errorInfo; break; } } catch { tb.Text = result.Replace(",", "\r\n"); return; } try { if (id == 1001) { result = Monitor.Monitor.mainBlockInfo.ToString(); } else if (id >= 3000) { result = Monitor.Monitor.parkingSpaceInfo[id - 3000].ToString(); } else if (id >= 2000) { result = Terminal.Terminal.terminalInfo[id - 2000].ToString(); } else if(type == PLCDataType.errorInfo) { result = Monitor.Monitor.PLCErrorInfo.ToString(); } else { Console.WriteLine("无法识别输入"); return; } } catch { Console.WriteLine("异常"); return; } tb.Text = result.Replace(",", "\r\n"); } private void btn_refresh_Click(object sender, EventArgs e) { Timer timer1 = new Timer(); timer1.Interval = 500; timer1.Tick += new EventHandler(button2_Click); timer1.Start(); Timer timer2 = new Timer(); timer2.Interval = 500; timer2.Tick += new EventHandler(button3_Click); timer2.Start(); Timer timer3 = new Timer(); timer3.Interval = 500; timer3.Tick += new EventHandler(button4_Click); timer3.Start(); } private void btn_clear_Click(object sender, EventArgs e) { TerminalStru tsFromCentral = new TerminalStru { terminalID = 1, parkingFee = (short)32767, paymentStatus = (short)0, licVerification = (short)0, userType = (short)0, }; TerminalStru tsFromTerminal = new TerminalStru { terminalID = 1, btnStatus = (short)0, cmd = (short)0, receiptNum = (short)0, }; pl.WriteToPLC(tsFromCentral, PLCDataType.central); pl.WriteToPLC(tsFromTerminal, PLCDataType.terminal); } /// /// 凭证号写入车位 /// /// /// private void button5_Click(object sender, EventArgs e) { List list = pl.ReadFromPLC(PLCDataType.terminal, 1); int receipt = ((TerminalStru)list[0]).receiptNum; pl.WriteAccordingToOffset(PLCDataType.parkingSpace, 14, receipt); } /// /// 启动地感 /// /// /// private void button7_Click(object sender, EventArgs e) { List list = pl.ReadFromPLC(PLCDataType.terminal, 1); short ground = ((TerminalStru)list[0]).groundStatus; if (ground == (short)1) { pl.WriteAccordingToOffset(PLCDataType.terminal, 40, (short)0); } else { pl.WriteAccordingToOffset(PLCDataType.terminal, 40, (short)1); } } /// /// 启动号牌机 /// /// /// private void button8_Click(object sender, EventArgs e) { pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)1); } /// /// 清空中控db /// /// /// private void button9_Click(object sender, EventArgs e) { try { List list = pl.ReadFromPLC(PLCDataType.central, 0); short complete = ((MainBlockStru)list[0]).processCompleted; if (complete == (short)0) { pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0); pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)1); pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0); } else { pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)0); pl.WriteAccordingToOffset(PLCDataType.central, 16, (short)0); pl.WriteAccordingToOffset(PLCDataType.central, 52, (short)0); } } catch { } } /// /// 切换中控停取模式 /// /// /// private void button10_Click(object sender, EventArgs e) { try { List list = pl.ReadFromPLC(PLCDataType.central, 0); short park = ((MainBlockStru)list[0]).parkingRunning; if (park == (short)0) { pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)1); pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)0); } else { pl.WriteAccordingToOffset(PLCDataType.central, 12, (short)0); pl.WriteAccordingToOffset(PLCDataType.central, 14, (short)1); } } catch { } } /// /// 切换终端停取模式 /// /// /// private void button11_Click(object sender, EventArgs e) { try { List list = pl.ReadFromPLC(PLCDataType.terminal, 1); short termStatus = ((TerminalStru)list[0]).terminalStatus; if (termStatus == (short)1) { pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)2); } else { pl.WriteAccordingToOffset(PLCDataType.terminal, 2, (short)1); } } catch { } } private void btn_PLCLink_Click(object sender, EventArgs e) { if (pl == null) { //pl = new PLCLinker(CpuType.S71500, ip, rack, slot, int.Parse(blockIds[0]), int.Parse(blockIds[1]), int.Parse(blockIds[2]), 6, 200); pl = (PLCLinker)Monitor.Monitor.PLC; } if (pl == null) return; else if (!pl.isConnected) { pl.PLCConnect(); } else { Control.ControlCollection cc = this.Controls; foreach (Control c in cc) { c.Enabled = pl.isConnected; } } } } }