title: Python: Sending a hex value to an external device over TCP or UDP
body: <p>Ok,
    I hope that I can explain what I am trying to do.  I have an external device that accepts 4 byte commands with a checksum value.  I need to send these to an external ethernet to serial interface and then read back the data recevied from the device.  So far I can do this from a python script to a python script.  WHere I am running into a problem is when sending the data to the device.  I have tried using b'\x00\x00\x00\x00\x00' format, I have tried byte arrays and so on.  I have wiresharked the data going out and see the data payload looking correct.  But, I am not getting a response to the query I am sending.  </p>
  
  <pre><code>import struct
  import sys
  import string
  import select
  import time
  import binascii
  import SocketServer
  
  port = 8010
  # region Read Codes
  
  getSerial = [0x26, 0x0A, 0x00, 0xF0]
  
  #Communication utilities
  
  class CommUtilities():
  
      #Convert to bytearray
      def hexit2(self, inp):
          return bytearray(inp)
  
      def calculateChecksum(self, buf):
          checksum = 0x5A #Starting checksum
          #For each byte, bitwise XOR it with our checksum
          for b in buf:
              checksum ^= b
          return checksum
  
      #Append the one byte checksum to a command
      #returns the complete byte array
      def appendChecksum(self, buff):
          buff.append(self.calculateChecksum(buff))
          return buff
  
  class TCPHandler():
  
      """ Notes here.... TCP Handler """
  
      def recv_timeout(self, the_socket, timeout=2):
           total_data=[]
           data=''
           begin=time.time()
           while l:
               #if you got some data, then break after wait a sec
               if total_data and time.time()-begin&gt;timeout:
                   break
               #if you got not data at all, wait a little longer
               elif time.time()-begin&gt;timeout*2:
                   break
               try:
                   data=the_socket.recv(1024)
                   if data:
                       total_data.append(data)
                       begin=time.time()
                   else:
                       time.sleep(0.1)
               except:
                   pass
           return ''.join(total_data)
  
  def start_server_receive(host):
  
          sock=socket(AF_INET,SOCK_STREAM)
          sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
          sock.bind(('0.0.0.0', port))
          sock.listen(5)
          print 'started on',port
          while True:
              host = '0.0.0.0'
              newsock,host=sock.accept()
              print 'connected'
              result= self.recv_timeout(newsock)
              print 'got', result
          sock.close()
          return result
  
  def send_server(data, host):
          sock=socket(AF_INET,SOCK_STREAM)
          sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
          sock.connect((host, port))
  
          print data
          #Is this strip needed?  Tried both with and without....
          data = data.strip()
  
          sock.sendall(data)
          print 'Sent Data'
          sock.close()
  
  
  def main():
      comm = CommUtilities()
      tcp = TCPHandler()
  
      host = "172.16.2.52"
  
      #get data to send
      data = comm.appendChecksum(getSerial)
      data = comm.hexit2(data)
  
      #Create a socket (SOCK_STREAM means a TCP socket)
  
      while True:
          try:
              #Connect to server and send data
              send_server(data, host)
              #Receive data from the server and shut down
              received = start_server_receive(host)
          finally:
              pass
          print "send:    {}".format(data)
          #print "received:{}".format(received)
          time.sleep(2)
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>So any suggestions?  I have example code that works in C# (which I didn't write since I don't know C# well at all), but how would I import that into python or use it?  Otherwise I need a way to make the device respond...</p>
  
  <p>Any and all suggestions would be appreciated.</p>
  
  <p>Is it because I am not using struct.pack?  If that is it, then how would I create the struct.pack function to make this work?  I am not very well versed in the struct.pack method yet.</p>