123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- 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<object> 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<object> 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<object> list = pl.ReadFromPLC(type, 1);
- // result = ((ParkingSpaceStru)list[0]).ToString();
- // }
- // else if (id > 0)
- // {
- // List<object> list = pl.ReadFromPLC(type, id);
- // result = ((TerminalStru)list[0]).ToString();
- // }
- // else if (id == 0)
- // {
- // List<object> 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<object> list = pl.ReadFromPLC(type, 1);
- // result = ((ParkingSpaceStru)list[0]).ToString();
- // }
- // else if (id > 0)
- // {
- // List<object> list = pl.ReadFromPLC(type, id);
- // result = ((TerminalStru)list[0]).ToString();
- // }
- // else if (id == 0)
- // {
- // List<object> 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<object> list = pl.ReadFromPLC(type, 1);
- // result = Monitor.Monitor.parkingSpaceInfo[1];//((ParkingSpaceStru)list[0]).ToString();
- // }
- // else if (id > 0)
- // {
- // List<object> list = pl.ReadFromPLC(type, id);
- // result = ((TerminalStru)list[0]).ToString();
- // }
- // else if (id == 0)
- // {
- // List<object> 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;
- case "扫描":
- type = PLCDataType.scanInfo;
- 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 if(type == PLCDataType.scanInfo)
- {
- result = Monitor.Monitor.PLCScanInfo.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);
- }
- /// <summary>
- /// 凭证号写入车位
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button5_Click(object sender, EventArgs e)
- {
- List<object> list = pl.ReadFromPLC(PLCDataType.terminal, 1);
- int receipt = ((TerminalStru)list[0]).receiptNum;
- pl.WriteAccordingToOffset(PLCDataType.parkingSpace, 14, receipt);
- }
- /// <summary>
- /// 启动地感
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button7_Click(object sender, EventArgs e)
- {
- List<object> 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);
- }
- }
- /// <summary>
- /// 启动号牌机
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button8_Click(object sender, EventArgs e)
- {
- pl.WriteAccordingToOffset(PLCDataType.central, 2, (short)1);
- }
- /// <summary>
- /// 清空中控db
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button9_Click(object sender, EventArgs e)
- {
- try
- {
- List<object> 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 { }
- }
- /// <summary>
- /// 切换中控停取模式
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button10_Click(object sender, EventArgs e)
- {
- try
- {
- List<object> 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 { }
- }
- /// <summary>
- /// 切换终端停取模式
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void button11_Click(object sender, EventArgs e)
- {
- try
- {
- List<object> 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;
- }
- }
- }
- }
- }
|