キーエンス KV5000 KV-LE21Vとラズベリーパイの
イーサネットMCプロトコル通信

 

KV-LE21V  マニュアル参照

HEADER

FORMAT

IO

RES

RES3 

下記サンプルコードのパラメーター

サブヘッダ                      subheader='5000'
ネットワーク番号             net='00'
PC番号                       pc='FF'
要求先ユニットIO番号       unitio='03FF'
要求先ユニット局番号       unitno='00'
要求データ長                 nlen='0018'
CPU監視タイマー             cputimer='0010'
コマンド                         command='0401'
サブコマンド                    subcommand='0000'
要求デバイス                  device='D*000000'
要求点数                       number='0002'
IPアドレス      ユニットエディタで決めておく                host='192.168.1.4'
ポート番号   ユニットエディタで決めておく  port=5000


#KV5000MCプロトコル  PYTHON
import socket
class MySocket:
    def __init__(self, sock=None):
        print('init')
        if sock is None:
            self.sock = socket.socket(
                            socket.AF_INET, socket.SOCK_STREAM)
        else:
            self.sock = sock
    def connect(self, host, port):
       print('connect')
       try:
           self.sock.connect((host, port))
           return 0
       except socket.error:
           print('コネクトエラーです')
           return -1
    def mysend(self, msg):
        print('mysend')
        self.sock.send(msg.encode('utf-8'))
        
    def myreceive(self):
        print('myreceive')
        data=[]
        data=(self.sock.recv(1024))
        
#        print(data)
        return data
#-----------------------------------------------------
def main():
 
    host='192.168.1.4'
    port=5000
    myapp = MySocket()
    connect_status=myapp.connect(host,port)
#ワードの読み出し 16ビット符号有り
    if connect_status==0:
        subheader='5000'
        net='00'
        pc='FF'
        unitio='03FF'
        unitno='00'
        nlen='0018'
        cputimer='0010'
        command='0401'
        subcommand='0000'
        device='D*000000'
        number='0002'
        
        cmd=subheader+net+pc+unitio+unitno+nlen+cputimer+command+subcommand+device+number
        myapp.mysend(cmd)
        
        count=2
        ret=[]
        anser=[]
        ret=myapp.myreceive().decode('utf-8')
        datanum=[]
        anser=ret[18:22]
        if anser=='0000':
            for i in range(0,count):
                start=4*i+22
                end=4*i+4+22
                data=(int(ret[start:end],16))
                if data>=32767:
                    data=data-65536
                datanum.append(data)
                print(str(datanum[i]))
            
#ワードの読み出し 16ビット符号無し
    if connect_status==0:
        subheader='5000'
        net='00'
        pc='FF'
        unitio='03FF'
        unitno='00'
        nlen='0018'
        cputimer='0010'
        command='0401'
        subcommand='0000'
        device='D*000000'
        number='0002'
        cmd=subheader+net+pc+unitio+unitno+nlen+cputimer+command+subcommand+device+number
        myapp.mysend(cmd)
        ret=[]
        anser=[]
        count=2
 
        ret=myapp.myreceive().decode('utf-8')
        datanum=[]
        anser=ret[18:22]
        if anser=='0000':
            for i in range(0,count):
                start=4*i+22
                end=4*i+4+22
                data=(int(ret[start:end],16))
                datanum.append(data)
                print(str(datanum[i]))
#ワードの読み出し 32ビット符号無し
    if connect_status==0:
        subheader='5000'
        net='00'
        pc='FF'
        unitio='03FF'
        unitno='00'
        nlen='0018'
        cputimer='0010'
        command='0401'
        subcommand='0000'
        device='D*000000'
        number='0004'
        cmd=subheader+net+pc+unitio+unitno+nlen+cputimer+command+subcommand+device+number
        myapp.mysend(cmd)
        ret=[]
        anser=[]
        count=2*2
        ret=myapp.myreceive().decode('utf-8')
        datanum=[]
        anser=ret[18:22]
        if anser=='0000':
            for i in range(0,count,2):
                start=4*i+22
                end=4*i+4+22
                hstart=4*i+26
                hend=4*i+26+4
                datalow=(int(ret[start:end],16))
                datahigh=(int(ret[hstart:hend],16))
                data=datahigh*65536+datalow
                datanum.append(data)
                print(str(data))
#ビットの読み出し
    if connect_status==0:
        subheader='5000'
        net='00'
        pc='FF'
        unitio='03FF'
        unitno='00'
        nlen='0018'
        cputimer='0010'
        command='0401'
        subcommand='0001'
        device='M*000000'
        number='0004'
        cmd=subheader+net+pc+unitio+unitno+nlen+cputimer+command+subcommand+device+number
        myapp.mysend(cmd)
        count=4
        ret=[]
        anser=[]
 
        ret=myapp.myreceive().decode('utf-8')
        datanum=[]         
        anser=ret[18:22]
        if anser=='0000':
            for i in range(0,count):
                start=i+22
                data=(int(ret[start],10))
                datanum.append(data)
                print(str(data))
if __name__ == '__main__':
  main()

 

戻る