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!!!