自动立体车库控制应用系统
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动立体车库控制应用系统相关的知识,希望对你有一定的参考价值。
自动立体车库控制应用系统主要控制界面如下
系统主要包含两个部分:
第一部分是系统与PLC系列进行通信,实时采集现场控制信息,控制提升机及运输小车,完成多个设备之间互锁,保证系统安全有效运行。实时采集现场2000多个点信息,同时保存在数据库。
第二部分是管理所有车位信息。高效规划入出库路径,最大化提高系统高效。
下面是数据库操作部分代码
using System; using System.Text.RegularExpressions; using System.Xml.XPath; using System.Data.SqlClient; using System.Windows.Forms; using System.Collections.Generic; using System.Text; using System.Data; /* * CopyRight (c) 2016 北京软秀科技有限公司 保留所有权利 [email protected] */ namespace Jiankong { class DataAccess { public SqlConnection Conn ; string str; public DataAccess() { string dd = Environment.CurrentDirectory; IniFile ls_IniFile = new IniFile(Environment.CurrentDirectory + "\\log.ini"); string connectString = ls_IniFile.ReadIniValue("Database", "connect"); Conn = new SqlConnection(connectString); } public int getPortnum(string PortName) { int PortNum; str = "select PortNum from PortState where PortName =‘" + PortName + "‘"; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); reader.Read(); PortNum = Convert.ToInt32(reader[0].ToString()); Conn.Close(); return PortNum; } public void addcar(string CarNo,string CarCla,string InTime,string PortNo) { string RegCarNo = "[\u4e00-\u9fa5][A-Z]-[0-9]{5}"; string RegInTime = "([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])"; string RegCarCla = "(大卡|中货|小轿)"; Conn.Open(); str = "select CarNo from CarIn where CarNo=‘" + CarNo + "‘"; SqlCommand command = new SqlCommand(str, Conn); SqlDataReader reader = command.ExecuteReader(); if (!Regex.IsMatch(CarNo, RegCarNo) || CarNo.Length!=8) { MessageBox.Show("无效的车牌号码(正确的格式:京A-12345)!"); } else if (!Regex.IsMatch(InTime, RegInTime) || InTime.Length != 8) { MessageBox.Show("无效的入库时间(正确的格式:12:34:00)!"); } else if (!Regex.IsMatch(CarCla, RegCarCla)) { MessageBox.Show("无效车辆类型(正确的格式:大卡|中货|小轿)!"); } else if (PortNo == "") { MessageBox.Show("本车库已满,下次请早!"); } else if (!reader.Read()) { reader.Close(); str = "insert into CarIn(CarNo,CarCla,InTime,PortNo) values (‘" + CarNo + "‘,‘" + CarCla + "‘,‘" + InTime + "‘,‘" + PortNo.Trim() + "‘)"; SqlCommand Command = new SqlCommand(str, Conn); Command.ExecuteNonQuery(); } else { MessageBox.Show("重复的车牌号码!"); } Conn.Close(); } public string recPortNo(string CarClass) { int NumA, NumB, NumC; string PortNo = ""; NumA = getPortnum("PortA"); NumB = getPortnum("PortB"); NumC = getPortnum("PortC"); Conn.Open(); if (CarClass == "大卡") { for (int i = 1; i < NumA; i++) { str = "select PortNo from CarIn where PortNo = ‘A" + i + "‘"; SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); if (! reader.Read()) { PortNo = "A" + i; break; } reader.Close(); } } else if (CarClass == "中货") { for (int i = 1; i < NumB; i++) { str = "select PortNo from CarIn where PortNo = ‘B" + i + "‘"; SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); if (!reader.Read()) { PortNo = "B" + i; break; } reader.Close(); } } else { for (int i = 1; i < NumC; i++) { str = "select PortNo from CarIn where PortNo = ‘C" + i + "‘"; SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); if (!reader.Read()) { PortNo = "C" + i; break; } reader.Close(); } } Conn.Close(); return PortNo; } //public void getRate(string CarClass, out int Time1, out int Time2, out int Time3, out double Rate1, out double Rate2, out double Rate3) //{ // str = "select * from Rate where CarCla =‘" + CarClass + "‘"; // Conn.Open(); // //SqlCommand command = new SqlCommand(str, Conn); // //SqlDataReader reader = command.ExecuteReader(); // //reader.Read(); // //Time1 = Convert.ToInt32(reader[1]); // //Rate1 = Convert.ToDouble(reader[2].ToString()); // //Time2 = Convert.ToInt32(reader[3]); // //Rate2 = Convert.ToDouble(reader[4].ToString()); // //Time3 = Convert.ToInt32(reader[5]); // //Rate3 = Convert.ToDouble(reader[6].ToString()); // Conn.Close(); //} public void updaterage(string CarClass, int Time1, int Time2, int Time3, string Rate1, string Rate2, string Rate3) { DialogResult MsgBoxResult; MsgBoxResult = MessageBox.Show("确定更新数据?", "请确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (MsgBoxResult == DialogResult.OK) { str = "update Rate set Time1=" + Time1 + ",Time2=" + Time2 + ",Time3=" + Time3 + ",Rate1=" + Rate1 + ",Rate2=" + Rate2 + ",Rate3=" + Rate3 + "where CarCla=‘" + CarClass + "‘"; Conn.Open(); SqlCommand Command = new SqlCommand(str, Conn); Command.ExecuteNonQuery(); Conn.Close(); } } public void getPortused(string PortName,out int PortNum, out int PortUsed) { str = "select PortNum,PortUsed from PortState where PortName =‘" + PortName + "‘"; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); reader.Read(); PortNum = Convert.ToInt32(reader[0].ToString()); PortUsed = Convert.ToInt32(reader[1].ToString()); Conn.Close(); } public int getDataNum(string table) { int DataNum; str = "select COUNT(*) from " + table + ""; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); reader.Read(); DataNum = Convert.ToInt32(reader[0].ToString()); Conn.Close(); return DataNum; } public string[] getCarNo() { int arrayno = getDataNum("CarIn"); string[] Carray = new string[arrayno]; str = "select CarNo from CarIn "; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); int i = 0; while (reader.Read()) { Carray[i] = reader[0].ToString(); i++; } Conn.Close(); return Carray; } public void getCardetail(string CarNo,out string carcla, out string intime, out string portno) { str = "select CarCla,InTime,PortNo from CarIn where CarNo=‘" + CarNo + "‘"; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); reader.Read(); carcla = reader[0].ToString(); intime =reader[1].ToString(); portno =reader[2].ToString(); Conn.Close(); } public void delCar(string CarNo,string money) { DialogResult MsgBoxResult; MsgBoxResult = MessageBox.Show("车牌号码为:"+CarNo +",需要缴纳停车费用"+money+"元!确定离开车库?","请确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (MsgBoxResult == DialogResult.OK) { str = "update CarIn set OutTime =‘" + money + "‘where CarNo =‘" + CarNo + "‘"; Conn.Open(); SqlCommand Command = new SqlCommand(str, Conn); Command.ExecuteNonQuery(); str = "delete from CarIn where CarNo =‘" + CarNo + "‘"; SqlCommand Command2 = new SqlCommand(str, Conn); Command2.ExecuteNonQuery(); Conn.Close(); } } public int[] getPortstate(string Stano,string PortName, out int arrayno) { int temparrayno = getDataNum("CarIn"); int[] temparray = new int[temparrayno]; str = "select PortNo from CarIn where 1=1"; str = str + " and stano =‘" + Stano + "‘"; str = str + " and statu = ‘1‘"; str = str + " and len(carno) <> 0 "; str = str + " order by PortNo"; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); int i = 0; while (reader.Read()) { if (reader[0].ToString().Substring(0, 1) == PortName.Substring(4, 1)) { temparray[i] = Convert.ToInt32(reader[0].ToString().Substring(1, reader[0].ToString().Length - 1)); i++; } } Conn.Close(); arrayno = i; int[] Pnoarray = new int[arrayno]; { for (int j = 0; j < arrayno; j++) { Pnoarray[j] = temparray[j]; } } return Pnoarray; } public void getCardetail2(string PortNo,string InStano,out string CarNo,out string InTime,out string CarCla,out string Stano) { str = "select CarNo,InTime,CarCla,Stano from CarIn where PortNo =‘" + PortNo + "‘"; str = str + " and stano= ‘" + InStano + "‘"; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); reader.Read(); CarNo = reader[0].ToString(); InTime = reader[1].ToString(); CarCla = reader[2].ToString(); Stano = reader[3].ToString(); Conn.Close(); } public int getCarportnum(string CarCla) { int Carnum; str = "select COUNT(*) from CarIn where CarCla = ‘" + CarCla + "‘"; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); reader.Read(); Carnum = Convert.ToInt32(reader[0].ToString()); Conn.Close(); return Carnum; } public string[] getCarportno(string CarCla) { int arrayno = getCarportnum(CarCla); string[] Carray = new string[arrayno]; str = "select PortNo from CarIn where CarCla = ‘" + CarCla + "‘"; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); int i = 0; while (reader.Read()) { Carray[i] = reader[0].ToString(); i++; } Conn.Close(); return Carray; } public void updatestate(string PortName, int PortNum) { DialogResult MsgBoxResult; MsgBoxResult = MessageBox.Show("确定更新数据?", "请确定", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); if (MsgBoxResult == DialogResult.OK) { string RegPortNum = "[0-9]{1,3}"; if (!Regex.IsMatch(PortNum.ToString(), RegPortNum) || PortNum.ToString().Length > 3) { MessageBox.Show("无效的车位数目!"); } else { str = "update PortState set PortNum=" + PortNum + "where PortName=‘" + PortName + "‘"; Conn.Open(); SqlCommand Command = new SqlCommand(str, Conn); Command.ExecuteNonQuery(); Conn.Close(); } } } public DataSet getDataset(string sqlstr) { Conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sqlstr,Conn); DataSet ds = new DataSet(); da.Fill(ds); Conn.Close(); return ds; } public void updatedata(DataSet ds,string sqlstr) { Conn.Open(); SqlDataAdapter da = new SqlDataAdapter(sqlstr, Conn); SqlCommandBuilder cb = new SqlCommandBuilder(da); da.Update(ds); Conn.Close(); } public bool isEmpty(string portno) { bool empty = false; str = "select CarNo from CarIn where PortNo = ‘" + portno + "‘"; Conn.Open(); SqlCommand datacommand = new SqlCommand(str, Conn); SqlDataReader reader = datacommand.ExecuteReader(); if(!reader.Read()) { empty = true; } Conn.Close(); return empty; } public void Dropit(string table) { str = "drop table " + table + ""; Conn.Open(); SqlCommand Command = new SqlCommand(str, Conn); Command.ExecuteNonQuery(); Conn.Close(); } public void Emptyit(string table) { str = "truncate table " + table + ""; Conn.Open(); SqlCommand Command = new SqlCommand(str, Conn); Command.ExecuteNonQuery(); Conn.Close(); } public void CarlogInitial() { str = "insert into Carlog(CarNo,CarCla,InTime,OutTime,PortNo,Action,Actiontime) select CarNo,CarCla,InTime,OutTime,PortNo,‘车库保持‘,CONVERT(varchar(100), GETDATE(), 8) from CarIn"; Conn.Open(); SqlCommand Command = new SqlCommand(str, Conn); Command.ExecuteNonQuery(); Conn.Close(); } public void ResetCarIn() { //str = "insert into CarIn select * from BackUp_CarIn "; int arrayno = getDataNum("BackUp_CarIn"); string[] carno = new string[arrayno]; string[] carcla = new string[arrayno]; string[] intime = new string[arrayno]; string[] portno = new string[arrayno]; str = "select * from BackUp_CarIn"; Conn.Open(); SqlCommand command = new SqlCommand(str, Conn); SqlDataReader reader = command.ExecuteReader(); int i=0; while (reader.Read()) { carno[i] = reader[0].ToString().Trim(); carcla[i] = reader[1].ToString().Trim(); intime[i] = reader[2].ToString().Trim(); portno[i] = reader[4].ToString().Trim(); i++; } Conn.Close(); for (int j = 0; j < arrayno; j++) { addcar(carno[j], carcla[j], intime[j], portno[j]); } } public void ResetPortState() { //str = "insert into PortState(PortName,PortNum,PortUsed) select PortName,PortNum,PortUsed from BackUp_PortState"; str = "select * into PortState from BackUp_PortState"; Conn.Open(); SqlCommand Command = new SqlCommand(str, Conn); Command.ExecuteNonQuery(); Conn.Close(); } public void ResetRate() { //str = "insert into Rate(CarCla,Time1,Rate1,Time2,Rate2,Time3,Rate3) select CarCla,Time1,Rate1,Time2,Rate2,Time3,Rate3 from BackUp_Rate"; str = "select * into Rate from BackUp_Rate"; Conn.Open(); SqlCommand Command = new SqlCommand(str, Conn); Command.ExecuteNonQuery(); Conn.Close(); } } }
下面是与PLC操作代码,通过OPC的方式。
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Threading; using System.Runtime.InteropServices; using System.Data; using System.Text; using OPC.Common; using OPC.Data.Interface; using OPC.Data; using OpcRcw.Comn; using OpcRcw.Da; using System.Net; using System.Net.Sockets; using System.Data.SqlClient; using System.Data.Sql; using System.Data.OleDb; namespace Jiankong { public class Plc : IOPCDataCallback { public IniFile ls_IniFile = new IniFile(Environment.CurrentDirectory + "\\log.ini"); public Data objData = new Data(); public DataSet dsData; public Thread objThread; public Thread objThreadFlash; // Mutex mutex; //private System.ComponentModel.IContainer components; const string serverProgID = "OPC.SimaticNET"; // ProgID of OPC server //const string serverProgID = "Schneider-Aut.OFS"; // ProgID of OPC serve private OpcServer theSrv; private OpcGroup theGrp; //private SERVERSTATUS theStatu; public static int devicenum = 4606; public static int deviceno = 4606; public int devnum; public string[] devicesatu = new string[22] { "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B", "0B","0B","0B" }; public string[] devicetrans = new string[30] { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" }; //public string[] errmsg = new string[1000]; public string[] errmsg = new string[] {" ","F01 运行超限","F02 升降超限","F03 松绳过载","F04 运行停偏","F05 升降停偏", "F06 货偏","F07 货叉位错","F08 升降超速","F09 运行变频器故障","F10 升降变频器故障", "F11 货叉变频器故障","F12 制动空开跳闸","F13 命令非法","F14 作业超时","F15 微升降超时", "F16 微升降超限","F17 伸缩叉超时","F18 货叉超限","F19 空取货","F20 空出库","F21 双重入库", "F22 卸货双重","F23 货未卸","F24 升降未对准","F25 运行速度偏差","F26 升降速度偏差","F27 激光读数超限", "F28 升降读数超限","F29 货叉编码器故障","F30 升降编码器故障","F31 激光ATT","F32 激光TMP","F33 激光LSR", "F34 激光PLB","F35 激光ERR","F36 备用","F37 升降值读取错误","F38 激光值读取错误","F39 备用","F40 备用", "F41 运行站故障","F42 起升站故障","F43 货叉站故障","F44 激光站故障","F45 ET200M站故障","F46 编码器站故障", "F47 货叉与升降同时动作","F48 货叉与运行同时动作",以上是关于自动立体车库控制应用系统的主要内容,如果未能解决你的问题,请参考以下文章