Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import time
- """
- initialize global variable to function definition
- """
- s = None
- reconnect = True # default is connection once
- # MUST USE BEETWEEN FORMAT
- # CONST DATA FORMAT
- HEADER = "\xa6"
- FOOTER = "\xa9"
- """
- OUT1ON
- OUT1OFF
- TRIG1
- OUT2ON
- OUT2OFF
- TRIG2
- OUT3ON
- OUT3OFF
- TRIG3
- OUT4ON
- OUT4OFF
- TRIG4
- PING
- MS
- PRXXXXX
- DS*xxxx#
- RSR
- RSSxxxxx
- IN1ONOK
- IN1OFFOK
- IN2ONOK
- IN2OFFOK
- IN3ONOK
- IN3OFFOK
- IN4ONOK
- IN4OFFOK
- PLAYENDOK
- NOTRACKOK
- WOK
- """
- def format_command(cmd):
- return HEADER+cmd+FOOTER
- """
- Function to generate format 0000X
- """
- def fill_zero(max_zero, data):
- result = str(data)
- for i in range(max_zero-1):
- result = str(0)+result
- return result
- """
- Function to connecting socket
- """
- def connect():
- global s
- result = True
- try :
- s = socket.socket()
- s.connect(('192.168.1.100',5000))
- except Exception as e :
- result = False
- print(e)
- return result
- """
- Function disconnecting socket
- """
- def disconnect():
- global s
- result = True
- try :
- if (s != None):
- s.close()
- except Exception as e :
- result = False
- print(e)
- return result
- """
- Function send data
- """
- def send(data):
- global s
- result = True
- try :
- s.send(data.encode('cp1252'))
- except Exception as e :
- result = False
- print(e)
- return result
- """
- Function for receiving data
- """
- def receive():
- global s
- return s.recv(1024).decode('cp1252')
- while reconnect :
- try :
- # connection first
- if(connect()):
- # set reconnect to false
- reconnect = False
- print("Connected...")
- # sending data
- send(format_command('TRIG1'))
- # receive data
- print(receive())
- # waiting cycles
- time.sleep(1)
- except Exception as e :
- reconnect = True
- # disconnecting error exception
- disconnect()
- print("Closing connection...")
- # reconnecting again..
- connect()
- print("Reconnecting...")
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement