SHARE
TWEET

Untitled

a guest Oct 21st, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import socket
  2. import time
  3.  
  4. """
  5. initialize global variable to function definition
  6. """
  7. s = None
  8. reconnect = True # default is connection once
  9.  
  10. # MUST USE BEETWEEN FORMAT
  11. # CONST DATA FORMAT
  12. HEADER = "\xa6"
  13. FOOTER = "\xa9"
  14.  
  15. """
  16. OUT1ON
  17. OUT1OFF
  18. TRIG1
  19. OUT2ON
  20. OUT2OFF
  21. TRIG2
  22. OUT3ON
  23. OUT3OFF
  24. TRIG3
  25. OUT4ON
  26. OUT4OFF
  27. TRIG4
  28. PING
  29. MS
  30. PRXXXXX
  31. DS*xxxx#
  32. RSR
  33. RSSxxxxx
  34. IN1ONOK
  35. IN1OFFOK
  36. IN2ONOK
  37. IN2OFFOK
  38. IN3ONOK
  39. IN3OFFOK
  40. IN4ONOK
  41. IN4OFFOK
  42. PLAYENDOK
  43. NOTRACKOK
  44. WOK
  45. """
  46. def format_command(cmd):
  47.     return HEADER+cmd+FOOTER
  48.  
  49. """
  50. Function to generate format 0000X
  51. """
  52. def fill_zero(max_zero, data):
  53.     result = str(data)
  54.     for i in range(max_zero-1):
  55.         result = str(0)+result
  56.  
  57.     return result
  58.  
  59. """
  60. Function to connecting socket
  61. """
  62. def connect():
  63.     global s
  64.     result = True
  65.     try :
  66.         s = socket.socket()
  67.         s.connect(('192.168.1.100',5000))
  68.     except Exception as e :
  69.         result = False
  70.         print(e)
  71.  
  72.     return result
  73.  
  74. """
  75. Function disconnecting socket
  76. """
  77. def disconnect():
  78.     global s
  79.     result = True
  80.     try :
  81.         if (s != None):
  82.             s.close()
  83.     except Exception as e :
  84.         result = False
  85.         print(e)
  86.  
  87.     return result
  88.  
  89. """
  90. Function send data
  91. """
  92. def send(data):
  93.     global s
  94.     result = True
  95.     try :
  96.         s.send(data.encode('cp1252'))
  97.     except Exception as e :
  98.         result = False
  99.         print(e)
  100.  
  101.     return result
  102.  
  103. """
  104. Function for receiving data
  105. """
  106. def receive():
  107.     global s
  108.     return s.recv(1024).decode('cp1252')
  109.  
  110. while reconnect :
  111.     try :
  112.         # connection first
  113.         if(connect()):
  114.             # set reconnect to false
  115.             reconnect = False
  116.             print("Connected...")
  117.  
  118.             # sending data
  119.             send(format_command('TRIG1'))
  120.            
  121.             # receive data
  122.             print(receive())
  123.  
  124.             # waiting cycles
  125.             time.sleep(1)
  126.     except Exception as e :
  127.         reconnect = True
  128.        
  129.         # disconnecting error exception
  130.         disconnect()
  131.         print("Closing connection...")
  132.        
  133.         # reconnecting again..
  134.         connect()
  135.         print("Reconnecting...")
  136.  
  137.         time.sleep(1)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top