コード例 #1
0
ファイル: TestUtilities.cs プロジェクト: newbienewbie/Sharp7
 public void TestGetCounter(ushort value, int expected)
 {
     S7.GetCounter(value).ShouldBe(expected);
 }
コード例 #2
0
    // Thread to Process PLC Read Queue
    static void ProcessPLC(S7PLC plc)
    {
        do
        {
            try
            {
                System.Threading.Thread.Sleep(5000);
                S7Client client = new S7Client();
                client.SetConnectionType(System.Convert.ToUInt16(plc.connection_type));
                client.SetConnectionParams(plc.ip, System.Convert.ToUInt16(plc.local_tsap), System.Convert.ToUInt16(plc.remote_tsap));
                Console.WriteLine("Try to connect " + plc.server_name);
                int res = client.Connect();
                if (res != 0 || client.Connected == false)
                {
                    continue;
                }

                Console.WriteLine("Connected " + plc.server_name + " IP:" + plc.ip);
                S7Client.S7CpuInfo Info = new S7Client.S7CpuInfo();
                res = client.GetCpuInfo(ref Info);
                if (res == 0)
                {
                    Console.WriteLine(plc.server_name + "  Module Type Name : " + Info.ModuleTypeName);
                    Console.WriteLine(plc.server_name + "  Serial Number    : " + Info.SerialNumber);
                    Console.WriteLine(plc.server_name + "  AS Name          : " + Info.ASName);
                    Console.WriteLine(plc.server_name + "  Module Name      : " + Info.ModuleName);
                }
                ;
                System.Threading.Thread.Sleep(1000);

                while (client.Connected)
                {
                    foreach (S7RW read in plc.reads)
                    {
                        CheckCommand(client, plc);
                        byte[] Buffer    = new byte[1024];
                        int    BytesRead = 0;
                        res = client.ReadArea(
                            System.Convert.ToInt32(read.data_area),
                            System.Convert.ToInt32(read.db),
                            System.Convert.ToInt32(read.offset),
                            System.Convert.ToInt32(read.amount),
                            System.Convert.ToInt32(StringToWordLengthCode(read.data_type)),
                            Buffer,
                            ref BytesRead);
                        if (res == 0)
                        {
                            // HexDump(Buffer, BytesRead);
                            for (int i = 0; i < read.amount; i++)
                            {
                                int    bytecnt;
                                double value = 0;
                                switch (read.data_type)
                                {
                                case "BIT":
                                    bytecnt = i / 8;
                                    value   = System.Convert.ToDouble(S7.GetBitAt(Buffer, bytecnt, i % 8));
                                    break;

                                case "BYTE":
                                    bytecnt = i;
                                    value   = S7.GetByteAt(Buffer, bytecnt);
                                    break;

                                case "CHAR":
                                    bytecnt = i;
                                    value   = S7.GetByteAt(Buffer, bytecnt);
                                    break;

                                case "WORD":
                                    bytecnt = i * 2;
                                    value   = S7.GetWordAt(Buffer, bytecnt);
                                    break;

                                case "DWORD":
                                    bytecnt = i * 4;
                                    value   = S7.GetDWordAt(Buffer, bytecnt);
                                    break;

                                case "INT":
                                    bytecnt = i * 2;
                                    value   = S7.GetIntAt(Buffer, bytecnt);
                                    break;

                                case "DINT":
                                    bytecnt = i * 4;
                                    value   = S7.GetDIntAt(Buffer, bytecnt);
                                    break;

                                case "REAL":
                                    bytecnt = i * 4;
                                    value   = S7.GetRealAt(Buffer, bytecnt);
                                    break;

                                case "COUNTER":
                                    bytecnt = i * 2;
                                    value   = S7.GetCounter((ushort)((Buffer[bytecnt + 1] << 8) + (Buffer[bytecnt])));
                                    break;

                                case "TIMER":
                                    bytecnt = i * 2;
                                    value   = (ushort)((Buffer[bytecnt + 1] << 8) + (Buffer[bytecnt]));
                                    break;

                                default:
                                    Console.WriteLine("Unsupported data type: " + read.data_type);
                                    break;
                                }
                                SendUdp(Encoding.ASCII.GetBytes(
                                            "[{\"point_key\":" + (read.point_number + i) +
                                            ",\"value\":" + value +
                                            ",\"failed\":false" +
                                            "}]"));
                                if (logread)
                                {
                                    Console.WriteLine(plc.server_name + " " + read.name + " OSHMI_POINT_NUMBER=" + (read.point_number + i) + " VALUE=" + value + " " + read.data_type);
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine(plc.server_name + " Error " + read.name);
                        }
                        System.Threading.Thread.Sleep(System.Convert.ToInt32(read.delay_ms));
                    }
                }

                // client.Disconnect();
                Console.WriteLine("Disconnected " + plc.server_name);
            }
            catch (Exception e)
            {
                // EXCEPTION HANDLER
                Console.WriteLine("Exception " + plc.server_name);
                Console.WriteLine(e);
            }
            System.Threading.Thread.Sleep(10000);
        } while (true);
    }