123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- using OmronFinsTCP.Net;
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace PLCTool.Common
- {
- public class PLCCom
- {
- private static string plcIp = ConfigurationManager.AppSettings["PLCIp"];
- private static string plcPort = ConfigurationManager.AppSettings["PLCPort"];
- private EtherNetPLC ENT;//plc连接
- private static PLCCom instance;
- private bool isConnect = false;
- private PLCCom()
- {
- ConnectPLC();
- }
- public static PLCCom GetInstance()
- {
- if (instance == null)
- {
- instance = new PLCCom();
- }
- return instance;
- }
- private bool ConnectPLC()
- {
- ENT = new EtherNetPLC();
- short re = ENT.Link(plcIp, short.Parse(plcPort), 500);
- if (re == 0)
- {
- //_logger.Info("PLC连接成功!");
- return true;
- }
- else
- {
- //_logger.Info("PLC连接失败!");
- return false;
- }
- }
- public string ReadPlcObject(string address, VarType valueType)
- {
- string value = string.Empty; ;
- short rb;
- short reSuc = -1;
- if (isConnect== false)
- {
- return string.Empty;
- }
-
- switch (valueType)
- {
- case VarType.Bit:
-
- reSuc = ENT.GetBitState(PlcMemory.DM, address, out rb);
- //读取成功
- if (reSuc == 0)
- {
- value = rb.ToString();
- }
- break;
- case VarType.Byte:
- break;
- case VarType.Word:
- reSuc = ENT.ReadWord(PlcMemory.DM, short.Parse(address), out rb);
- //读取成功
- if(reSuc==0)
- {
- value = rb.ToString();
- }
- break;
- case VarType.DWord:
- break;
- case VarType.Int:
- break;
- case VarType.DInt:
- break;
- case VarType.Real:
- break;
- default:
- return null;
- }
- return value;
- }
- public bool WritePlcObject(string address, VarType valueType,string writeValue)
- {
- bool isSuccess=false;
-
- short reSuc = -1;
- if (isConnect == false)
- {
- return false;
- }
- switch (valueType)
- {
- case VarType.Bit:
- BitState bit= BitState.OFF;
- if(writeValue.Trim()=="1")
- {
- bit = BitState.ON;
- }
- else
- {
- if (writeValue.Trim() == "0")
- {
- bit = BitState.OFF;
- }
- }
- reSuc = ENT.SetBitState(PlcMemory.DM, address,bit);
- //写成功
- if (reSuc == 0)
- {
- isSuccess = true;
- }
- break;
- case VarType.Byte:
- break;
- case VarType.Word:
- short wValue = Convert.ToInt16(writeValue);
- reSuc = ENT.WriteWord(PlcMemory.DM, short.Parse(address), wValue);
- //写成功
- if (reSuc == 0)
- {
- isSuccess = true;
- }
- break;
- case VarType.DWord:
- break;
- case VarType.Int:
- break;
- case VarType.DInt:
- break;
- case VarType.Real:
- break;
- }
- return isSuccess;
- }
- private static object GetVariableValue(VarType varType, object var)
- {
- switch (varType)
- {
- case VarType.Bit:
- return (bool)var;
- case VarType.Byte:
- return (byte)var;
- case VarType.Word:
- return (ushort)var;
- case VarType.DWord:
- return (uint)var;
- case VarType.Int:
- return (short)var;
- case VarType.DInt:
- return (int)var;
- case VarType.Real:
- return (float)var;
-
- default:
- return null;
- }
- }
- }
- public enum VarType
- {
- /// <summary>
- /// Bit variable type (bool)
- /// </summary>
- Bit,
- /// <summary>
- /// Byte variable type (8 bits)
- /// </summary>
- Byte,
- /// <summary>
- /// Word variable type (16 bits, 2 bytes)
- /// </summary>
- Word,
- /// <summary>
- ///
- /// </summary>
- DWord,
- /// <summary>
- /// Int variable type (16 bits, 2 bytes)
- /// </summary>
- Int,
- /// <summary>
- /// DInt variable type (32 bits, 4 bytes)
- /// </summary>
- DInt,
- /// <summary>
- /// Real variable type (32 bits, 4 bytes)
- /// </summary>
- Real,
-
- /// <summary>
- /// Char Array / C-String variable type (variable)
- /// </summary>
- String,
-
- }
- }
|