Serial connection from python to arduino Error!

Hi,

I´m trying to set up a serial connection (USB) between a python programm and my arduino nano. I need to send two unsigned shorts like this {0x123,0x345} to get my galvos to work. This is my code:

Arduino side:

#include "Drawing.h"
#include "Objects.h"


unsigned short serialData[] = {0x000,0x000};
char hi, lo;


void setup()
{
  Serial.begin(9600);
}


void Serial_Obj()
{
   if (Serial.available()>0){
    delay(50);
    
    //First byte for x-pos
    hi = Serial.read();
    lo = Serial.read();

    serialData[0] = (hi << 8) | lo;
   

    //Second byte for y-pos
    hi = Serial.read();
    lo = Serial.read();
    serialData[1] = (hi << 8) | lo;
       


    Drawing::drawObject(serialData, sizeof(draw_island)/4); //this is where I need to send coordinates


  }


void loop() {
   Serial_Obj();
}

And the python side:

import numpy as np
import time
import serial

arduinoData = serial.Serial('com6',9600, timeout=None)      #timeout weg?
x = 0
y = 160
x_ser = 0
y_ser = 0
beam_distance = 5   #distance in pixel
speed = 1          # speed in ms
resolution = 1280.
max_dez = 32767.
pixel = max_dez/resolution

array_object = np.random.randint(2, size=(1280, 960))

while y<=960:

    while x<=1280:
        if array_object[x,y] == 1:

            x_ser = round(x*pixel)
            y_ser = round((y+160)*pixel)
            
            arduinoData.write(x_ser.to_bytes(2, byteorder='big'))

            arduinoData.write(y_ser.to_bytes(2, byteorder='big'))
            
            time.sleep(speed)

        x=x+1
    x = 0
    y = y+beam_distance

y=0

Thanks a lot!!!

You left out something very very important.

You forgot to tell what the problem is.

What type of Arduino? Your variable sizes don’t match up with the normal UNO.

This Simple Python - Arduino demo should help get you started.

...R

Hi,

wow I really forgot that, sry:D

The scanner I send the data to, is supposted to scan one dot out of my random array for every delay (speed in ms). Instead every 5 seconds it scans 5 dots without the delay(delay of 5 seconds gives the same result). It´s really fast so that you see a rectangle combined with a triangle kind of shape (not what I want).

Robin2:
This Simple Python - Arduino demo should help get you started.

I think the idea of sending a start and end sign could fix my problem.

Ghostrider5:
I think the idea of sending a start and end sign could fix my problem.

I forgot to include the following link which may also help
Serial Input Basics

...R