1
1

More than 3 years have passed since last update.

Panasonic の電力計 kw2g から rs485 でデータを読む

Last updated at Posted at 2019-12-06

RTコマンド を送って、リスポンスを受け取るサンプルです。
プロトコルは MEWTOCOL です。

kw2g.py
#! /usr/bin/python3
#
#       kw2g.py
#
#                                               Dec/07/2019
#
#
# ------------------------------------------------------------------
import  serial
import  time
import  sys

# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=19200,bytesize=8,parity='O',stopbits=1,timeout = 1)
#
data = ['%','0','1','#','R','T','*','*','\r','\n']
data_int = []
for it in range(len(data)):
        uu = ord(data[it])
        data_int.append(uu)
#
ser.write(bytearray(data_int))
#
time.sleep(0.5)
#
response = []
while True:
        try:
                sys.stderr.write("*** reading ***\n")
                bytesAvailable = ser.inWaiting()
                sys.stderr.write("%d bytes available to read\n" % bytesAvailable)
                response = ser.readline()
                break
        except KeyboardInterrupt:
                break
#
str_hex = "hex: "
str_out = "chr: "
for it in range(len(response)):
        str_hex += " %02x" % response[it]
        str_out += "  %c" % response[it]
print(str_hex)
print(str_out)
ser.close()
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------

実行結果

$ ./kw2g.py 
*** 開始 ***
*** reading ***
25 bytes available to read
chr:  25 30 31 24 52 54 39 39 34 30 30 31 30 33 30 31 30 30 30 30 30 30 30 31 0d
hex:   %  0  1  $  R  T  9  9  4  0  0  1  0  3  0  1  0  0  0  0  0  0  0  1  
*** 終了 ***
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1