def read_coils(self, start_coil_address, number_of_coils):
        """ Modbus command : Read coil data(s) (function code = 01)

            @Argument :
            start_coil_address (int16) : Start coil address where to read a coil data
            number_of_coils (int)      : number of coil(s) to read

            @Return :
            response : Coil data (Byte)
                        The coils in the response message are packed as one coil per bit of the
                        data field. Status is indicated as 1= ON and 0= OFF.
        """
        response = None

        # Generate modbus TCP message
        try:
            message = modbus_tcp.read_coils(
                slave_id=self.device_id,
                starting_address=start_coil_address,
                quantity=number_of_coils)
        except Exception as e:
            print("Error during generate modbus message.")

        # Send message via TCP socket
        try:
            self._connect()
            response = modbus_tcp.send_message(message, self.tcp_socket)
            print("response={}".format(response))
            self._disconnect()
        except Exception as e:
            print("Error during send modbus message.")
            print(e)

        return response
예제 #2
0
    def thread_poll(self):

        startaddr = self.input_startaddr.value()
        endaddr = self.input_endaddr.value()

        while self.swjk < 10:

            if self.swjk > 1:
                print('break ok')
                break

            else:

                print('polling start')

                holdingRegisters_list = []

                for addrlist in range(startaddr, endaddr + 1):

                    try:
                        if self.fncode == 1:
                            msg_adu = tcp.read_coils(1, addrlist, 1)
                            holdingRegisters = tcp.send_message(
                                msg_adu, self.sock)

                        elif self.fncode == 10001:
                            msg_adu = tcp.read_discrete_inputs(1, addrlist, 1)
                            holdingRegisters = tcp.send_message(
                                msg_adu, self.sock)

                        elif self.fncode == 40001:
                            msg_adu = tcp.read_holding_registers(
                                1, addrlist, 1)
                            holdingRegisters = tcp.send_message(
                                msg_adu, self.sock)

                        elif self.fncode == 30001:
                            msg_adu = tcp.read_input_registers(1, addrlist, 1)
                            holdingRegisters = tcp.send_message(
                                msg_adu, self.sock)

                        holdingRegisters_list.append(holdingRegisters[0])
                    except AttributeError:
                        print('Register Addr Over')
                        holdingRegisters_list.append('None')

                    print("Register " + str(addrlist) + " poll.")
                print(holdingRegisters_list)

                items_list = []
                for registersData in holdingRegisters_list:
                    item_holdingRegisters = QTableWidgetItem()
                    item_holdingRegisters.setText(str(registersData))
                    items_list.append(item_holdingRegisters)

                for item_num in range(0, len(items_list)):
                    self.swjTableSignal.emit(item_num, 2, items_list[item_num])

            time.sleep(1)
예제 #3
0
 def readBits(self, address, length):
     message = tcp.read_coils(self.slaveNbr, address, length)
     response = self.__doRequest(message)
     return response
예제 #4
0
def button(func, state):

	state = not state
	ret_word = ""
	ret_state = False

	conf.SIGNED_VALUES = True
	plc_ip = '192.168.10.35'
	port_id = 502
	sl_id = 0
	writing_value = 1

	# socket
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
	# 502 is the port  
	sock.connect((plc_ip, port_id))

	if (func == 0):
		message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
		response = tcp.send_message(message, sock)

		if (response[0]):
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
			ret_word = "Failed to flush all"
			return ret_state,ret_word
		else:
			row_Entry_message = tcp.read_coils(slave_id = 1, starting_address = 1, quantity = 1)
			row_Entry_response = tcp.send_message(row_Entry_message, sock)
			row_Entry_state = row_Entry_response[0]
			if (row_Entry_state):
				message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
				response = tcp.send_message(message, sock)

				message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
				response = tcp.send_message(message, sock)
				if(response[0]):
					ret_word = "Flush all once"
					ret_state = True
					time.sleep(1)
					message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
					response = tcp.send_message(message, sock)
					return ret_state,ret_word
				else:
					ret_word = "Failed to flush all once"
					return ret_state,ret_word
			else:
				ret_word = "Harvester not on row_Entry state"
				return ret_state,ret_word

	elif (func == 1):

		# Make sure it is not shutdown
		message = tcp.read_holding_registers(slave_id=1, starting_address=0, quantity=1)
		response = tcp.send_message(message, sock)
		if(response[0]):
			# Make sure row_Out state is 0
			message = tcp.write_single_coil(slave_id = 1, address = 2, value = 0)
			response = tcp.send_message(message, sock)
			message = tcp.read_coils(slave_id = 1, starting_address = 2, quantity = 1)
			response = tcp.send_message(message, sock)

			if (not response[0]):
				# Make sure clean state is 0
				message = tcp.write_single_coil(slave_id = 1, address = 3, value = 0)
				response = tcp.send_message(message, sock)

				message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
				response = tcp.send_message(message, sock)
		
				message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
				response = tcp.send_message(message, sock)

				if(response[0]):
					ret_state = True
					ret_word = "Standby/row_entry on"
					return ret_state,ret_word
				else:
					ret_word = "Failed to standby"
					return ret_state,ret_word
		
		else:
			ret_word = "You are in shutdown mode"
			return ret_state, ret_word

	elif (func == 2):

		message = tcp.read_holding_registers(slave_id=1, starting_address=0, quantity=1)
		response = tcp.send_message(message, sock)
		if(response[0]):
			# Make sure row_Entry state is 0
			message = tcp.write_single_coil(slave_id = 1, address = 1, value = 0)
			response = tcp.send_message(message, sock)
			message = tcp.read_coils(slave_id = 1, starting_address = 2, quantity = 1)
			response = tcp.send_message(message, sock)
			if (not response[0]):
				message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
				response = tcp.send_message(message, sock)
				
				message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
				response = tcp.send_message(message, sock)
				if(response[0]):
					ret_state = True
					ret_word = "Navigation/Row_Out on"
					return ret_state,ret_word
				else:
					ret_word = "Failed to set Navigation/Row_Out mode"
					return ret_state,ret_word
		else:
			ret_word = "You are in shutdown mode"
			return ret_state, ret_word		

	elif (func == 3):
		message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
		response = tcp.send_message(message, sock)
		if (response[0]):
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
		else:
			message = tcp.read_coils(slave_id = 1, starting_address = 2, quantity = 1)
			response = tcp.send_message(message, sock)
			if (response[0] != 1):
				ret_word = "row_Out is not on, make sure row_Out is on first"
				return ret_state,ret_word

			else:
				message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
				response = tcp.send_message(message, sock)

				message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
				response = tcp.send_message(message, sock)
				if(response[0]):
					ret_state = True
					ret_word = "clean on"
					return ret_state,ret_word
				else:
					ret_word = "failed to set clean on"
					return ret_state,ret_word
		
	elif (func == 4):
		message = tcp.write_single_register(slave_id = 1, address = 0, value = 16)
		response = tcp.send_message(message, sock)

		message = tcp.read_holding_registers(slave_id=1, starting_address=0, quantity=1)
		response = tcp.send_message(message, sock)

		if response[0] == 16:
			ret_word = "Startup"
			ret_state = True
			return ret_state,ret_word
		else:
			ret_word = "Unable to startup"
			return ret_state,ret_word

	elif (func == 5):
		message = tcp.read_coils(slave_id = 1, starting_address = 1, quantity = 1)
		response = tcp.send_message(message, sock)
		if response[0]:
			message = tcp.write_single_coil(slave_id=1, address=func,value=1)
			response = tcp.send_message(message,sock)
			time.sleep(1)
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
			ret_word = "Trough 1 flush once"
			return ret_state,ret_word

		else:
			ret_word = "Not in Standby/Row_Entry state"
			return ret_state, ret_word

	elif (func == 6):
		message = tcp.read_coils(slave_id = 1, starting_address = 1, quantity = 1)
		response = tcp.send_message(message, sock)
		if response[0]:
			message = tcp.write_single_coil(slave_id=1, address=func,value=1)
			response = tcp.send_message(message,sock)
			time.sleep(1)
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
			ret_word = "Trough 2 flush once"
			return ret_state,ret_word

		else:
			ret_word = "Not in Standby/Row_Entry state"
			return ret_state, ret_word

	elif (func == 7):
		message = tcp.read_coils(slave_id = 1, starting_address = 1, quantity = 1)
		response = tcp.send_message(message, sock)
		if response[0]:
			message = tcp.write_single_coil(slave_id=1, address=func,value=1)
			response = tcp.send_message(message,sock)
			time.sleep(1)
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
			ret_word = "Trough 3 flush once"
			return ret_state,ret_word

		else:
			ret_word = "Not in Standby/Row_Entry state"
			return ret_state, ret_word

	elif (func == 8):
		message = tcp.read_coils(slave_id = 1, starting_address = 1, quantity = 1)
		response = tcp.send_message(message, sock)
		if response[0]:
			message = tcp.write_single_coil(slave_id=1, address=func,value=1)
			response = tcp.send_message(message,sock)
			time.sleep(1)
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
			ret_word = "Trough 4 flush once"
			return ret_state,ret_word

		else:
			ret_word = "Not in Standby/Row_Entry state"
			return ret_state, ret_word

	elif (func == 9):
		message = tcp.read_coils(slave_id = 1, starting_address = 1, quantity = 1)
		response = tcp.send_message(message, sock)
		if response[0]:
			message = tcp.write_single_coil(slave_id=1, address=func,value=1)
			response = tcp.send_message(message,sock)
			time.sleep(1)
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
			ret_word = "Trough 5 flush once"
			return ret_state,ret_word

		else:
			ret_word = "Not in Standby/Row_Entry state"
			return ret_state, ret_word

	if (func == 10):
		message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
		response = tcp.send_message(message, sock)

		if (response[0]):
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
		else:
			row_Entry_message = tcp.read_coils(slave_id = 1, starting_address = 1, quantity = 1)
			row_Entry_response = tcp.send_message(row_Entry_message, sock)
			row_Entry_state = row_Entry_response[0]
			if (row_Entry_state):
				message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
				response = tcp.send_message(message, sock)

				message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
				response = tcp.send_message(message, sock)
				if(response[0]):
					ret_state = True
					ret_word = "Flush_with_delay once"
					time.sleep(1)
					message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
					response = tcp.send_message(message, sock)
					return ret_state,ret_word
				else:
					ret_word = "Failed to delay flush once"
					return ret_state,ret_word
			else:
				ret_word = "Harvester not on Standby/Row_Entry state"
				return ret_state,ret_word


	elif (func == -1):
		message = tcp.write_single_register(slave_id = 1, address = 0, value = 0)
		response = tcp.send_message(message, sock) 

		message = tcp.read_holding_registers(slave_id=1, starting_address=0, quantity=1)
		response = tcp.send_message(message, sock)
		if response[0] == 0:
			ret_state = True
			ret_word = "Close all"
			return ret_state,ret_word

	return ret_state,ret_word
	sock.close()
 def read_coils(self, address, quantity):
     with self.__lock:
         message = tcp.read_coils(slave_id=1,
                                  starting_address=address - 1,
                                  quantity=quantity)
         return self.__send_modbus_message(message)
예제 #6
0
def button(func, state):

	state = not state
	ret_word = ""
	ret_state = False

	conf.SIGNED_VALUES = True
	plc_ip = '192.168.10.35'
	port_id = 502
	sl_id = 0
	writing_value = 1

	# socket
	sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
	# 502 is the port  
	sock.connect((plc_ip, port_id))

	if (func == 0):
		message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
		response = tcp.send_message(message, sock)

		if (response[0]):
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
			ret_word = "Failed to flush all"
			return ret_state,ret_word
		else:
			row_Entry_message = tcp.read_coils(slave_id = 1, starting_address = 1, quantity = 1)
			row_Entry_response = tcp.send_message(row_Entry_message, sock)
			row_Entry_state = row_Entry_response[0]
			if (row_Entry_state):
				message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
				response = tcp.send_message(message, sock)

				message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
				response = tcp.send_message(message, sock)
				if(response[0]):
					ret_word = "flush for 1 sec"
					ret_state = True
					time.sleep(1)
					message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
					response = tcp.send_message(message, sock)
					return ret_state,ret_word
				else:
					ret_word = "failed to set flush on"
					return ret_state,ret_word
			else:
				ret_word = "Harvester not on row_Entry state, make sure it is on row_Entry state first to flush"
				return ret_state,ret_word

	elif (func == 1):
		# Make sure row_Out state is 0
		message = tcp.write_single_coil(slave_id = 1, address = 2, value = 0)
		response = tcp.send_message(message, sock)

		# Make sure clean state is 0
		message = tcp.write_single_coil(slave_id = 1, address = 3, value = 0)
		response = tcp.send_message(message, sock)

		message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
		response = tcp.send_message(message, sock)
		
		message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
		response = tcp.send_message(message, sock)
		if(response[0]):
			ret_state = True
			ret_word = "row_Entry on"
			return ret_state,ret_word
		else:
			ret_word = "failed to set row_Entry on"
			return ret_state,ret_word

	elif (func == 2):
		# Make sure row_Entry state is 0
		message = tcp.write_single_coil(slave_id = 1, address = 1, value = 0)
		response = tcp.send_message(message, sock)

		#Make sure flush state is 0
		message = tcp.write_single_coil(slave_id = 1, address = 0, value = 0)
		response = tcp.send_message(message, sock)

		message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
		response = tcp.send_message(message, sock)
		
		message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
		response = tcp.send_message(message, sock)
		if(response[0]):
			ret_state = True
			ret_word = "row_Out on"
			return ret_state,ret_word
		else:
			ret_word = "failed to set row_Out on"
			return ret_state,ret_word

	elif (func == 3):
		message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
		response = tcp.send_message(message, sock)
		if (response[0]):
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
		else:
			message = tcp.read_coils(slave_id = 1, starting_address = 2, quantity = 1)
			response = tcp.send_message(message, sock)
			if (response[0] != 1):
				ret_word = "row_Out is not on, make sure row_Out is on first"
				return ret_state,ret_word

			else:
				message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
				response = tcp.send_message(message, sock)

				message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
				response = tcp.send_message(message, sock)
				if(response[0]):
					ret_state = True
					ret_word = "clean on"
					return ret_state,ret_word
				else:
					ret_word = "failed to set clean on"
					return ret_state,ret_word
		
	elif (func == 4):
		message = tcp.write_single_register(slave_id = 1, address = 0, value = 16)
		response = tcp.send_message(message, sock)

	elif (func == 5):
		message = tcp.write_single_coil(slave_id=1, address=func,value=1)
		response = tcp.send_message(message,sock)
		time.sleep(1)
		message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
		response = tcp.send_message(message, sock)
		ret_word = "trough 1"
		return ret_state,ret_word

	elif (func == 6):
		message = tcp.write_single_coil(slave_id=1, address=func,value=1)
		response = tcp.send_message(message,sock)
		time.sleep(1)
		message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
		response = tcp.send_message(message, sock)
		ret_word = "trough 2"
		return ret_state,ret_word

	elif (func == 7):
		message = tcp.write_single_coil(slave_id=1, address=func,value=1)
		response = tcp.send_message(message,sock)
		time.sleep(1)
		message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
		response = tcp.send_message(message, sock)
		ret_word = "trough 3"
		return ret_state,ret_word

	elif (func == 8):
		message = tcp.write_single_coil(slave_id=1, address=func,value=1)
		response = tcp.send_message(message,sock)
		time.sleep(1)
		message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
		response = tcp.send_message(message, sock)
		ret_word = "trough 4"
		return ret_state,ret_word

	elif (func == 9):
		message = tcp.write_single_coil(slave_id=1, address=func,value=1)
		response = tcp.send_message(message,sock)
		time.sleep(1)
		message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
		response = tcp.send_message(message, sock)
		ret_word = "trough 5"
		return ret_state,ret_word

	if (func == 10):
		message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
		response = tcp.send_message(message, sock)

		if (response[0]):
			message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
			response = tcp.send_message(message, sock)
		else:
			row_Entry_message = tcp.read_coils(slave_id = 1, starting_address = 1, quantity = 1)
			row_Entry_response = tcp.send_message(row_Entry_message, sock)
			row_Entry_state = row_Entry_response[0]
			if (row_Entry_state):
				message = tcp.write_single_coil(slave_id = 1, address = func, value = 1)
				response = tcp.send_message(message, sock)

				message = tcp.read_coils(slave_id = 1, starting_address = func, quantity = 1)
				response = tcp.send_message(message, sock)
				if(response[0]):
					ret_state = True
					ret_word = "flush_with_delay on"
					time.sleep(1)
					message = tcp.write_single_coil(slave_id = 1, address = func, value = 0)
					response = tcp.send_message(message, sock)
					return ret_state,ret_word
				else:
					ret_word = "failed to set flush on"
					return ret_state,ret_word
			else:
				ret_word = "Harvester not on row_Entry state, make sure it is on row_Entry state first to flush"
				return ret_state,ret_word


	elif (func == -1):
		message = tcp.write_single_register(slave_id = 1, address = 0, value = 0)
		response = tcp.send_message(message, sock) 
		ret_state = True
		ret_word = "Close all"
		return ret_state,ret_word

	return ret_state,ret_word
	sock.close()
예제 #7
0
def def_test_get_request_pdu(request_handler, mbap_header):
    pdu = read_coils(1, 1, 1)
    assert request_handler.get_request_pdu(mbap_header + pdu) == pdu
def main():
    host, port = '10.8.8.20', 502
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))

    iv = 'nvXIK5RtUZpZQdJD'
    helloMsgClient = 'client'
    helloMsgServer = 'server'
    clientkey = RSA.generate(2048)
    clientpukey = clientkey.publickey().exportKey('PEM').decode('ascii')
    clientprkey = clientkey.exportKey('PEM').decode('ascii')
    print('\nClient Public Key:\n\n', clientpukey,
          '\n\n\nClient Private Key:\n\n', clientprkey)
    sign = signData(helloMsgClient.encode(), clientprkey)
    print("\n\nSign:\n\n", sign)
    message = str(clientpukey) + ";" + str(sign)
    print("\n\nRSA Message:\n\n", message)
    sock.sendall(message.encode())

    rsaResponse = sock.recv(3072).strip()
    rsaMsg = rsaResponse.decode()
    serverpukey = rsaMsg.split(';')[0]
    signRcvd = rsaMsg.split(';')[1]
    verify = verifySign(helloMsgServer.encode(), serverpukey, signRcvd)
    if verify:
        print("\n\n---------Received Server RSA Public Key---------\n")
    else:
        raise Exception(
            "\nClient Authentication Failure. RSA public key and sign don't match"
        )
    print('\nRSA Server Public Key:\n\n', serverpukey)

    print(
        "\n\n****************************RSA Exchange Completes and Authentication Done***************************"
    )
    received = sock.recv(3072).strip()
    #print('\nDH key Received:\n{}'.format(received))
    dh_params = load_pem_parameters(received, default_backend())

    if isinstance(dh_params, dh.DHParameters):
        client_keypair = dh_params.generate_private_key()
        request = b'Client public key:' + client_keypair.public_key(
        ).public_bytes(Encoding.PEM, PublicFormat.SubjectPublicKeyInfo)
        sock.sendall(request)
        #print('\nreq',request)
    else:
        print('Bad response')
        sock.close()
        return

    # this means we are still in the game
    received = sock.recv(3072).strip()
    #print('Received:\n{}'.format(received))
    print(
        "\n\n****************************Received Server's public Key****************************\n"
    )
    # check the format of the message (or rather the beginning)
    if bytearray(received)[0:18] == b'Server public key:':
        # get the server's public key from the binary and its proper index to the end
        server_pubkey = load_pem_public_key(bytes(bytearray(received)[18:]),
                                            default_backend())
        if isinstance(server_pubkey, dh.DHPublicKey):
            # calculate the shared secret
            session_key = client_keypair.exchange(server_pubkey)
            # print the shared secret
            #print('Shared Secret\n{}'.format(ba.hexlify(session_key)))
            print(
                "\n****************************Shared Secret key is generated***************************\n"
            )
            print(
                "\n**********************DH Exchange Completes and Session Established**********************"
            )

    conf.SIGNED_VALUES = True
    obtained_key = hkdf_function(session_key)
    print('\n\nDerived Key:\t\t-------->\t', (obtained_key))
    message = tcp.write_multiple_coils(slave_id=1,
                                       starting_address=1,
                                       values=[0, 0, 1, 1, 1])
    digest = getHash(message)
    #print("\n\ndigest_str: ",digest)
    cipher = aes_cbc_encrypt(message, obtained_key, iv, digest)
    print('\nMessage:\t\t-------->\t', (message), type(message))
    print('\nCipher:\t\t\t-------->\t', (cipher), type(cipher))
    #print('derived-------->',obtained_key)
    sock.sendall(cipher.encode('utf-8'))

    mb_responce = sock.recv(1024)
    mb_responce, digest = aes_decrypt(mb_responce, obtained_key, iv)
    localDigest = getHash(mb_responce)
    verify = verifyHash(digest, localDigest)
    if verify == True:
        print("\nResponse:\t\t-------->\t", (mb_responce))
        responce = tcp.parse_response_adu(mb_responce, message)
        print('\nParsed Response:\t-------->\t', (responce))

        message = tcp.read_coils(slave_id=1, starting_address=1, quantity=5)
        print('\n\n\nSecond Message:\t\t-------->\t', (message))
        print('\nMessage(Hexa format):\t-------->\t', (ba.b2a_hex(message)))
        digest = getHash(message)
        cipher = aes_cbc_encrypt(message, obtained_key, iv, digest)
        print('\nCipher:\t\t\t-------->\t', (cipher))

        sock.sendall(cipher.encode('utf-8'))
        mb_responce = sock.recv(1024)
        mb_responce, digest = aes_decrypt(mb_responce, obtained_key, iv)
        localDigest = getHash(mb_responce)
        verify = verifyHash(digest, localDigest)
        if verify == True:
            print("\nResponse:\t\t-------->\t", (mb_responce))
            responce = tcp.parse_response_adu(mb_responce, message)
            print('\nParsed Response:\t-------->\t', (responce))

    #iv11 = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for x in range(16))
    #print('iv11',iv11)

    sock.close()
예제 #9
0
    if hash_generated != hash_received:
        print("hash mismatch! time to abort...")
        return None
    else:
        print("hash matched")
        return (dt)


sock.sendall(enc_msg(message))
print("message sent")
resp = sock.recv(1024)
resp_dec = dec_msg(resp)

# print("message sent. server response:", ba.hexlify(resp))
print("parsed server response:", tcp.parse_response_adu(resp_dec, message))

print("\n...\nreading data pts")
message = tcp.read_coils(slave_id=1, starting_address=1, quantity=len(data))

sock.sendall(enc_msg(message))

recv = sock.recv(1024)
print("message received!")

recv_dec = dec_msg(recv)
print("message data parsed:")
print(tcp.parse_response_adu(recv_dec, message))

# print(responce)
sock.close()
예제 #10
0
def def_test_get_request_pdu(request_handler, mbap_header):
    pdu = read_coils(1, 1, 1)
    assert request_handler.get_request_pdu(mbap_header + pdu) == pdu