FAM3 イーサネット上位通信  ラズベリーパイ

PRO

MM

AB

 

IPアドレス                     host='192.168.1.4'
ポート番号   port=12289

 

横河 FAM3上位通信
#from __future__ import print_function
#import sys
import socket
class MySocket:
    """demonstration class only
      - coded for clarity, not efficiency
    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')
           self.sock.connect((host, port))
           return 0
       except socket.error:
           print('ソケットエラーです')
           return -1
    def mysend(self, msg):
        print('mysend')
        MSGLEN=len(msg)
        self.sock.send(msg.encode('utf-8'))
    def myreceive(self):
        print('myreceive')
        data=(self.sock.recv(1024))
        return data
#--------------------------------------------------------------------
def main():
    host='192.168.1.4'
    port=12289
    myapp = MySocket()
    ret=myapp.connect(host,port)
#1ワードの読み出し 16ビット符号有り
    if ret==0:
        cmd='01WRD'
        myapp.mysend(cmd+'D00001,04'+'\r\n')
        count=4
        ret=[]
        anser=[]
        ret=myapp.myreceive().decode('utf-8')
#        print(ret)
        datanum=[]
        anser=ret[2:4]
        if anser=='OK':
            for i in range(1,count+1):
                start=4*i
                end=4*i+4
                data=(int(ret[start:end],16))
                if data>=32767:
                    data=data-65536
                datanum.append(data)
                print(datanum[i-1])
#1ワードの読み出し 16ビット符号無し
    cmd='01WRD'
    myapp.mysend(cmd+'D00001,04'+'\r\n')
    count=4
    ret=[]
    anser=[]
    ret=myapp.myreceive().decode('utf-8')
    datanum=[]
    anser=ret[2:4]
    if anser=='OK':
        for i in range(1,count+1):
            start=4*i
            end=4*i+4
            data=(int(ret[start:end],16))
            datanum.append(data)
            print(datanum[i-1])
#1ワードの読み出し 32ビット符号無し
    cmd='01WRD'
    myapp.mysend(cmd+'D00001,04'+'\r\n')
    count=4
    ret=[]
    anser=[]
    ret=myapp.myreceive().decode('utf-8')
    datanum=[]
    anser=ret[2:4]
    if anser=='OK':
        for i in range(0,count,2):
            start=4*i+4
            end=4+4*i+4
            hstart=4+4*i+4
            hend=4*i+8+4
            datalow=(int(ret[start:end],16))
            datahigh=(int(ret[hstart:hend],16))
            data=datahigh*65536+datalow
            datanum.append(data)
            print(data)
if __name__ == '__main__':

戻る