用python发送十六进制串行

用python发送十六进制串行

本文介绍了用python发送十六进制串行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个周末我会做一个小项目。有一个太阳能电池逆变器(丹佛斯ULX 3600i),我将尝试连接到我的Linux机器,看我是否可以从中获取数据,创建多少能量,例如统计数据。有一个RJ45接口的输入,但带有RS485。



通过RS485转换器将电缆连接到PC的USB端口,电脑和逆变器。

然后我写了一个小的python代码来提出请求。但是我不知道如何正确地发送数据。

  import serial 
import struct

ser = serial.Serial(
port ='/ dev / ttyUSB0',
baudrate = 19200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS


print(ser.isOpen())
thestring =7E FF 03 00 01 00 02 0A 01 C8 04 D0 01 02 80 00 00 00 00 8E E7 7E
data = struct.pack(hex(thestring))
#data = struct.pack(hex,0x7E,0xFF,0x03,0x00,0x01,0x00,0x02 ,0x0A,0x01,0xC8,0x04,0xD0,0x01,0x02,0x80,0x00,0x00,0x00,0x00,0x8e,0xE7,0x7E)

ser.write(data)
s = ser.read(1)
print(s)
ser.close()

变频器使用Danfoss ComLynx协议(位于是我试图发送的数据):

编辑:
我现在可以发送请求,因为Adam 4520 RS485转换器上的LED灯闪烁一次,但是没有数据返回,但是出现此错误当我在终端执行CTRL + C时:

  dontommy @ dtbeast:〜/ workspace / python_scripting / src $ ./sollar。 py 
True
^ CTraceback(最近一次调用的最后一个):
在< module>文件中的./sollar.py,第30行。
s = ser.readline()。decode('utf-8')
文件/usr/local/lib/python3.2/dist-packages/serial/serialposix.py,第446行,在
准备好后,_,_ = select.select([self.fd],[],[],self._timeout)
KeyboardInterrupt
pre

解决方案

将thestring重写为

  thestring =\x7E\xFF\x03\x00\x01\x00\x02\x0A\x01\xC8\x04\xD0\x01\x02 \x80\x00\x00\x00\x00\x8E\xE7\z7E

你不需要打包它,你可以说 data = thestring 并发送它。这只会在文档中的各种id完全匹配您设备上的内容时才起作用。您需要弄清python struct >工作,如何编码二进制以及如何将两个4位值放入一个8位字节:线索,请参阅>>和运算符和结构包B格式


This weekend I am going to make a little project. Got a solarcell inverter (Danfoss ULX 3600i) which I will try to connect to my linux machine, to see if I can grab the data from it, how much energy created eg for stats. There is an input for RJ45 connection on it, but with RS485.

I got the cables to connect it through my usb port in the pc with an RS485 converter in between the pc and the inverter.

I am then writing a small python code to make request. However I cant figure out how to send the data correctly.

import serial
import struct

ser = serial.Serial(
    port='/dev/ttyUSB0',
    baudrate=19200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)

print(ser.isOpen())
thestring = "7E FF 03 00 01 00 02 0A 01 C8 04 D0 01 02 80 00 00 00 00 8E E7 7E"
data = struct.pack(hex(thestring))
#data = struct.pack(hex, 0x7E, 0xFF, 0x03, 0x00, 0x01, 0x00, 0x02, 0x0A, 0x01, 0xC8,      0x04, 0xD0, 0x01, 0x02, 0x80, 0x00, 0x00, 0x00, 0x00, 0x8E, 0xE7, 0x7E)

ser.write(data)
s = ser.read(1)
print(s)
ser.close()

The inverter is using the Danfoss ComLynx protocol (on page 26 is the data I am trying to send):

EDIT:I now can send a request as the LED light on the Adam 4520 RS485 converter is blinking once, however no data back, but get this error when I do a CTRL+C in terminal:

dontommy@dtbeast:~/workspace/python_scripting/src$ ./sollar.py
True
^CTraceback (most recent call last):
  File "./sollar.py", line 30, in <module>
    s = ser.readline().decode('utf-8')
  File "/usr/local/lib/python3.2/dist-packages/serial/serialposix.py", line 446, in read
    ready,_,_ = select.select([self.fd],[],[], self._timeout)
KeyboardInterrupt
解决方案

Rewrite "thestring" as

thestring = "\x7E\xFF\x03\x00\x01\x00\x02\x0A\x01\xC8\x04\xD0\x01\x02\x80\x00\x00\x00\x00\x8E\xE7\z7E"

You won't need to pack it, you can say data=thestring and send it. This will only work if the various ids in the document match exactly what is on your equipment

You need to figure out how the python "struct" works, how to encode binary and how to put two 4 bit values into one 8 bit byte: clue, see the >> and << operators and the struct pack "B" format

这篇关于用python发送十六进制串行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 06:41