123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- using PLCS7;
- using S7.Net;
- 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;
- namespace PLCConnector
- {
- public partial class Form1 : Form
- {
- PLCLinker pl;
- public Form1()
- {
- InitializeComponent();
- pl = new PLCLinker(CpuType.S71500, "192.168.0.1", 0, 1, 18, 41, 20, 6, 200);
- }
- 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 { MessageBox.Show("异常"); return; }
- textBox1.Text = result.Replace(",","\r\n");
- }
- 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 { MessageBox.Show("异常"); return; }
- textBox7.Text = result.Replace(",", "\r\n");
- }
- 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 = ((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 { MessageBox.Show("异常"); return; }
- textBox9.Text = result.Replace(",", "\r\n");
- }
- private void btn_refresh_Click(object sender, EventArgs e)
- {
- Timer timer1 = new Timer();
- timer1.Interval = 1000;
- timer1.Tick += new EventHandler(button2_Click);
- timer1.Start();
- Timer timer2 = new Timer();
- timer2.Interval = 1000;
- timer2.Tick += new EventHandler(button3_Click);
- timer2.Start();
- Timer timer3 = new Timer();
- timer3.Interval = 1000;
- 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 = -1,
- 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, 36, (short)0);
- }
- else
- {
- pl.WriteAccordingToOffset(PLCDataType.terminal, 36, (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)
- {
- 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);
-
- }
- }
- }
- }
|