-1
from digi.xbee.devices import XBeeDevice, RemoteXBeeDevice, XBee64BitAddress
import serial
import logging

local_device = XBeeDevice("COM7", 9600)
data_send = ("\x33\x03\x75\x30\x00\x24\x5b\xc0")
try:
    local_device.open()
    remote_device = RemoteXBeeDevice(local_device, XBee64BitAddress.from_hex_string("0013A20041513885"))    
    local_device.flush_queues()
    print("Sending data asynchronously to %s >> %s " % (remote_device.get_64bit_addr(),data_send))#printing MAC add.
    local_device.send_data_async(remote_device, data_send)
    print("Success")

    print("Waiting for data...\n")
    xbee_message = local_device.read_data()
    if xbee_message is not None:
        print("From %s >> %s" % (xbee_message.remote_device.get_64bit_addr(),
                                         xbee_message.data.decode()))



finally:
    if local_device is not None and local_device.is_open():
        local_device.close()

I am trying to send modbus hex command (33 04 75 30 00 24 5b c0) through python for zigbee module. But, instead off sending command in hex its sending in ascii. Can anyone solve this problem. I am using digi-xbee module in python which uses pyserial for serial comm. here is my code which i am trying to send. data_send = ("\x33\x03\x75\x30\x00\x24\x5b\xc0")

enter image description here

Jacob
  • 1,182
  • 12
  • 18
Ayush
  • 37
  • 6
  • Please add all relevant code to the question as code-formatted text. – Klaus D. Sep 17 '18 at 17:42
  • Please **edit your question** and add the code there! – Klaus D. Sep 17 '18 at 17:54
  • What do you mean when you say it's "sending in ASCII" versus "sending in hex"? What do you see on the remote end, and what are you hoping to see? Your debug print command ("Sending data...") looks like you've encoded the data as bytes for sending. You could use `data_send = b"\x33\x03\x75\x30\x00\x24\x5b\xc0"` so it's clear that you're sending a bytes object (leading `b` before the string literal). Also note that the parens aren't necessary (might create a tuple instead of just a string/bytes), and could actually cause problems with the send. – tomlogic Sep 17 '18 at 23:01
  • The difference of hexadecimal data and ASCII data (actually it is not ASCII but ISO 8859-1) is just the representation. Different display for the same data. – Klaus D. Sep 18 '18 at 03:04
  • @tomlogic Actually i am having a device which has xbee module and rs-485 port in it. so my requirement is to send modbus command to the device through xbee connected to pc so that device xbee can receive command and send back the modbus data. Device is configured like if the modbus command(33 03 75 30 00 24 5b c0) will get to it then it will send back the modbus data.But instead of sending command in hex its sending in different representation like ascii so the device is receiving command: 00 00 00 00 00 00 00 00. Its working fine with XCTU app as we can check both ascii and hex there. – Ayush Sep 18 '18 at 04:09
  • @klaus D But, i dont want to send data with different representation, should be in hex format only. – Ayush Sep 18 '18 at 04:10
  • It **is** the same data. It is just displayed differently. – Klaus D. Sep 18 '18 at 04:14
  • @ Klaus D Okay. But still i need to find a way to communicate with the device i am having. and Thanks for you time and support. – Ayush Sep 18 '18 at 04:17
  • Problem resolved..Thank you all. – Ayush Sep 18 '18 at 05:00
  • @Ayush, you might want to add an answer to your question explaining what the solution was (so it might help someone with a similar problem and finding your question via Google), or remove the question if it was something specific to your setup and unrelated to an error in your code (in which case other people can't learn from the mistake). – tomlogic Sep 19 '18 at 01:08
  • @tomlogic Done !!! – Ayush Sep 21 '18 at 10:24

1 Answers1

1

Use Latin-1 encoding. If You are using UTF-8 encoding, it may not be correct every time. so, better to use Latin-1 encoding.

For eg:

data_send = ("\x00\x01\x01\x01\x01\x00\x03\x08\x00\x00\x00\x14\x04\x00\x00\x00\x44\xf2\xfc").encode('Latin-1')
Ayush
  • 37
  • 6