title: Problem with Serial.read() and Struct.pack / serial communication between Arduino and Python (3.x)
body: <p>I have a problem while trying to send some values from <code>Python 3.x</code> to <code>Arduino</code> using <strong>Serial Communication</strong>.<br>
  It working fine when the value is smaller than 255, but when it's greater than 255, error will happen.</p>
  
  <p>I'm using <code>Struct.pack</code> in Python and <code>Serial.read()</code> in Arduino</p>
  
  <hr>
  
  <p>Python code:</p>
  
  <pre><code>import cv2
  import numpy as np
  from serial import Serial
  import struct
  
  arduinoData = Serial('com6', 9600)
  
  cap = cv2.VideoCapture(0)
  hand_cascade = cv2.CascadeClassifier('hand.xml')
  
  while(True):
      ret, frame = cap.read()
      gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
      handdetect = hand_cascade.detectMultiScale(gray, 1.6, 3)
  
      for (x, y, w, h) in handdetect:   
          cv2.rectangle(frame, (x, y), (x + w, y + h), (127, 127, 0), 2)
  
          xcenter = int(x + w/2)
          ycenter = int(y + h/2)
  
          #This is where i send values to serial port
          arduinoData.write(struct.pack('&gt;II',xcenter,ycenter))
  
      cv2.imshow('Webcam', frame)
      k = cv2.waitKey(25) &amp; 0xff
      if k == 27:
          break
  
  cap.release()
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>Arduino code:</p>
  
  <pre><code>int SerialData[8];
  const int led = 7;
  int xcenter;
  int ycenter;
  
  void setup(){
    Serial.begin(9600);
    pinMode(led, OUTPUT);
  }
  
  void loop(){
    if (Serial.available() &gt;= 8){
      for (int i = 0; i &lt; 8; i++){
        SerialData[i] = Serial.read();
      }
      xcenter = (SerialData[0]*1000) + (SerialData[1]*100) + (SerialData[2]*10) + SerialData[3];
      ycenter = (SerialData[4]*1000) + (SerialData[5]*100) + (SerialData[6]*10) + SerialData[7];
  
      if (xcenter &lt;= 200){
        digitalWrite(led, LOW);
      }
      else if(xcenter &gt; 200){
        digitalWrite(led, HIGH);
      }
     //Serial.flush();
    }
  }
  </code></pre>
  
  <p>Like I said at the beginning of this topic, when <code>xcenter &gt; 200 &amp;&amp; xcenter &lt;= 255</code>, the <strong>LED is turned ON</strong> (it's mean the code working fine).<br>
  But when <code>xcenter &gt; 255</code>, the <strong>LED is OFF</strong> (something wrong here).<br>
  I think I already read all 8 bytes in Arduino code and used unsigned int <code>&gt;II</code> in <code>struct.pack</code>, so what and where is my false?<br>
  I'm appreciate for all help! Thank you!</p>
  
  <hr>
  
  <p>EDIT and FIXED.<br>
  "It doesn't pack int into digits (0-9), it packs them into bytes (0-255)"_</p>
  
  <p>So here is the false:  </p>
  
  <pre><code>xcenter = (SerialData[0]*1000) + (SerialData[1]*100) + (SerialData[2]*10) + SerialData[3];  
  ycenter = (SerialData[4]*1000) + (SerialData[5]*100) + (SerialData[6]*10) + SerialData[7];  
  </code></pre>
  
  <p>Changed to this (for the large values):  </p>
  
  <pre><code>long result = long((unsigned long(unsigned char(SerialData[0])) &lt;&lt; 24) | (unsigned long(unsigned char(SerialData[1])) &lt;&lt; 16)
              | (unsigned long(unsigned char(SerialData[2])) &lt;&lt; 8) | unsigned char(SerialData[3])); 
  </code></pre>
  
  <p>Or changed to this (for the small values):  </p>
  
  <pre><code>xcenter = (SerialData[2]*256) + SerialData[3];
  ycenter = (SerialData[6]*256) + SerialData[7]; 
  </code></pre>
  
  <p>Or this (for the small values too):  </p>
  
  <pre><code>int result = int((unsigned int(unsigned char(SerialData[2])) &lt;&lt; 8) | unsigned char(SerialData[3]));
  </code></pre>
  
  <p>And the code gonna work perfectly!</p>