def printReadResult(result):
    if result.IsSuccess:
        print(result.Content)
    else:
        print("failed   " + result.Message)


def printWriteResult(result):
    if result.IsSuccess:
        print("success")
    else:
        print("falied  " + result.Message)


if __name__ == "__main__":
    print(SoftBasic.GetUniqueStringByGuidAndRandom())
    melsecNet = MelsecA1ENet("192.168.0.100", 5000)
    if melsecNet.ConnectServer().IsSuccess == False:
        print("connect falied  ")
    else:
        # bool read write test
        melsecNet.WriteBool("M200", True)
        printReadResult(melsecNet.ReadBool("M200"))

        # bool array read write test
        melsecNet.WriteBool("M300", [True, False, True, True, False])
        printReadResult(melsecNet.ReadBool("M300", 5))

        # int16 read write test
        melsecNet.WriteInt16("D200", 12358)
        printReadResult(melsecNet.ReadInt16("D200"))
    def testAll(self):
        # 下面是单元测试
        plc = MelsecMcNet("192.168.8.13", 6001)
        if plc.ConnectServer().IsSuccess == False:
            print("无法连接PLC,将跳过单元测试。等待网络正常时,再进行测试")
            return

        # 开始单元测试,从bool类型开始测试
        address = "M200"
        boolTmp = [True, True, False, True, False, True, False]
        self.assertTrue(plc.WriteBool(address, True).IsSuccess)
        self.assertTrue(plc.ReadBool(address).Content == True)
        self.assertTrue(plc.WriteBool(address, boolTmp).IsSuccess)
        readBool = plc.ReadBool(address, len(boolTmp)).Content
        for i in range(len(boolTmp)):
            self.assertTrue(readBool[i] == boolTmp[i])

        address = "D300"
        # short类型
        self.assertTrue(plc.WriteInt16(address, 12345).IsSuccess)
        self.assertTrue(plc.ReadInt16(address).Content == 12345)
        shortTmp = [123, 423, -124, 5313, 2361]
        self.assertTrue(plc.WriteInt16(address, shortTmp).IsSuccess)
        readShort = plc.ReadInt16(address, len(shortTmp)).Content
        for i in range(len(readShort)):
            self.assertTrue(readShort[i] == shortTmp[i])

        # ushort类型
        self.assertTrue(plc.WriteUInt16(address, 51234).IsSuccess)
        self.assertTrue(plc.ReadUInt16(address).Content == 51234)
        ushortTmp = [5, 231, 12354, 5313, 12352]
        self.assertTrue(plc.WriteUInt16(address, ushortTmp).IsSuccess)
        readUShort = plc.ReadUInt16(address, len(ushortTmp)).Content
        for i in range(len(readUShort)):
            self.assertTrue(readUShort[i] == ushortTmp[i])

        # int类型
        self.assertTrue(plc.WriteInt32(address, 12342323).IsSuccess)
        self.assertTrue(plc.ReadInt32(address).Content == 12342323)
        intTmp = [123812512, 123534, 976124, -1286742]
        self.assertTrue(plc.WriteInt32(address, intTmp).IsSuccess)
        readint = plc.ReadInt32(address, len(intTmp)).Content
        for i in range(len(intTmp)):
            self.assertTrue(readint[i] == intTmp[i])

        # uint类型
        self.assertTrue(plc.WriteUInt32(address, 416123237).IsSuccess)
        self.assertTrue(plc.ReadUInt32(address).Content == 416123237)
        uintTmp = [81623123, 91712749, 91273123, 123, 21242, 5324]
        self.assertTrue(plc.WriteUInt32(address, uintTmp).IsSuccess)
        readuint = plc.ReadUInt32(address, len(uintTmp)).Content
        for i in range(len(uintTmp)):
            self.assertTrue(readuint[i] == uintTmp[i])

        # float类型
        self.assertTrue(plc.WriteFloat(address, 123.45).IsSuccess)
        self.assertTrue(round(plc.ReadFloat(address).Content, 2) == 123.45)
        floatTmp = [123, 5343, 1.45, 563.3, 586.2]
        self.assertTrue(plc.WriteFloat(address, floatTmp).IsSuccess)
        readFloat = plc.ReadFloat(address, len(floatTmp)).Content
        for i in range(len(floatTmp)):
            self.assertTrue(floatTmp[i] == round(readFloat[i], 2))

        # double类型
        self.assertTrue(plc.WriteDouble(address, 1234.5434).IsSuccess)
        self.assertTrue(plc.ReadDouble(address).Content == 1234.5434)
        doubleTmp = [1.4213, 1223, 452.5342, 231.3443]
        self.assertTrue(plc.WriteDouble(address, doubleTmp).IsSuccess)
        readDouble = plc.ReadDouble(address, len(doubleTmp)).Content
        for i in range(len(doubleTmp)):
            self.assertTrue(readDouble[i] == doubleTmp[i])

        # long类型
        self.assertTrue(plc.WriteInt64(address, 123617231235123).IsSuccess)
        self.assertTrue(plc.ReadInt64(address).Content == 123617231235123)
        longTmp = [12312313123, 1234, 412323812368, 1237182361238123]
        self.assertTrue(plc.WriteInt64(address, longTmp).IsSuccess)
        readLong = plc.ReadInt64(address, len(longTmp)).Content
        for i in range(len(longTmp)):
            self.assertTrue(readLong[i] == longTmp[i])

        # ulong类型
        self.assertTrue(plc.WriteUInt64(address, 1283823681236123).IsSuccess)
        self.assertTrue(plc.ReadUInt64(address).Content == 1283823681236123)
        ulongTmp = [21316, 1231239127323, 1238612361283123]
        self.assertTrue(plc.WriteUInt64(address, ulongTmp).IsSuccess)
        readULong = plc.ReadUInt64(address, len(ulongTmp)).Content
        for i in range(len(ulongTmp)):
            self.assertTrue(readULong[i] == ulongTmp[i])

        # string类型
        self.assertTrue(plc.WriteString(address, "123123").IsSuccess)
        self.assertTrue(plc.ReadString(address, 3).Content == "123123")

        # byte类型
        byteTmp = bytearray([0x4F, 0x12, 0x72, 0xA7, 0x54, 0xB8])
        self.assertTrue(plc.Write(address, byteTmp).IsSuccess)
        self.assertTrue(
            SoftBasic.IsTwoBytesAllEquel(
                plc.Read(address, 3).Content, byteTmp))
示例#3
0
            m101 = read.Content[1]
            m102 = read.Content[2]
            m103 = read.Content[3]
            m104 = read.Content[4]
            m105 = read.Content[5]
            m106 = read.Content[6]
            m107 = read.Content[7]
            m108 = read.Content[8]
            m109 = read.Content[9]
        else:
            print(read.Message)

        read = siemens.Read("M100", 20)
        if read.IsSuccess:
            count = siemens.byteTransform.TransInt32(read.Content, 0)
            temp = siemens.byteTransform.TransSingle(read.Content, 4)
            name1 = siemens.byteTransform.TransInt16(read.Content, 8)
            barcode = read.Content[10:20].decode('ascii')

        read = siemens.ReadFromCoreServer(
            SoftBasic.HexStringToBytes(
                "03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0E 00 05 05 01 12 0A 10 02 00 01 00 00 83 00 03 20 00 04 00 08 3B"
            ))
        if read.IsSuccess:
            # 显示服务器返回的报文
            print(read.Content)
        else:
            # 读取错误
            print(read.Message)

        siemens.ConnectClose()
        read = siemens.Read("M100",10)
        if read.IsSuccess:
            m100 = read.Content[0]
            m101 = read.Content[1]
            m102 = read.Content[2]
            m103 = read.Content[3]
            m104 = read.Content[4]
            m105 = read.Content[5]
            m106 = read.Content[6]
            m107 = read.Content[7]
            m108 = read.Content[8]
            m109 = read.Content[9]
        else:
            print(read.Message)

        read = siemens.Read("M100",20)
        if read.IsSuccess:
            count = siemens.byteTransform.TransInt32(read.Content,0)
            temp = siemens.byteTransform.TransSingle(read.Content,4)
            name1 = siemens.byteTransform.TransInt16(read.Content,8)
            barcode = read.Content[10:20].decode('ascii')

        read = siemens.ReadFromCoreServer(SoftBasic.HexStringToBytes("03 00 00 24 02 F0 80 32 01 00 00 00 01 00 0E 00 05 05 01 12 0A 10 02 00 01 00 00 83 00 03 20 00 04 00 08 3B"))
        if read.IsSuccess:
            # 显示服务器返回的报文
            print(read.Content)
        else:
            # 读取错误
            print(read.Message)

        siemens.ConnectClose()