KV3000とシリアル通信  PYTHON
キーエンス KV-L2*V マニュアル参照
シリアル-USB変換ケープル使用

 
#KV3000とシリアル通信  PYTHON
# -*- coding: utf-8 -*-
 
 
from serial import Serial
class MyCom:
    def __init__(self, com=None):
          print('init')
          self.com = Serial(
          port='/dev/ttyUSB0',
          baudrate=9600,
          bytesize=8,
          parity='E',
          stopbits=1,
          timeout=0.1,
          xonxoff=0,
          rtscts=0,
          writeTimeout=None,
          dsrdtr=None)
    def mysend(self, msg):
        print('mysend')
#        print(msg)   
        self.com.write(msg.encode('utf-8'))
    def myreceive(self):
        print('myreceive')
        data=self.com.readline()
        if data==b'':
            data=b'E1'
#        print(data)    
        return data
def main():
    myapp = MyCom()
    cmd="CR\r"
    myapp.mysend(cmd)
    ret=myapp.myreceive().decode('utf-8')
    print(ret)
    
#1ワードの読み出し 16ビット符号無し
#    myapp.connect(host,port)
    cmd='RD '
    device='DM0000'
    myapp.mysend(cmd+device+'\r')
    ret=myapp.myreceive().decode('utf-8')
    #print(ret)    
    datanum=[]
    anser=ret[0:2]
    if anser!='E1':
        data=(int(ret,10))
        print(data)
#ワードの読み出し 32ビット符号無し
    cmd='RD '
    device='DM0000'
    myapp.mysend(cmd+device+'\r')
    ret=myapp.myreceive().decode('utf-8')
#    print(ret)    
    datanum=[]
    anser=ret[0:2]
    if anser!='E1':
        datal=(int(ret,10))
        print(data)
    cmd='RD '
    device='DM0001'
    myapp.mysend(cmd+device+'\r')
    ret=myapp.myreceive().decode('utf-8')
#    print(ret)    
    datanum=[]
    anser=ret[0:2]
    if anser!='E1':
        datah=(int(ret,10))
        print(datah)
        data=datah*65536+datal
        print(data)
#ビットの読み出し
    cmd='RD '
    
    myapp.mysend(cmd+'0000'+'\r')
    ret=myapp.myreceive().decode('utf-8')
#    print(ret)    
    datanum=[]
    anser=ret[0:2]
    if anser!='E1':
        data=(int(ret,10))
        print(data)
        
if __name__ == '__main__':
  main()


戻る