educa
Active member
Hi,
I am trying to make a very simple program in python which sends binary data to a teensy 3.6 in chunks of 5 bytes.
I wrote the following program for this in python
Now this code just opens an image (bart.png) , converts it to black and white and for each black pixel in the image it sends 5 bytes.
byte 1 + 2 are the 2 bytes making the X coordinate of the pixel
byte 3 + 4 are the 2 bytes making the Y coordinate of the pixel
the fifth byte is by default always the number 7 (for the time being, since this will also become a variable value later on)
The code does this for each black pixel and then stops.
This is the teensy side of code
So the teensy first sets the dacs to 12 bit resolution and centers them both to value 2048 + it sets A9 to a very very low duty cycle PWM
In the loop I wait until there are 5 or more bytes available on serial, then read these 5 bytes and do some setting with these values.
The loop continuously checks for bytes in serial
This works very nicely. I upload the code to teensy, I start the python script and everything works fine THE FIRST TIME.
If the python program ends and I let the teensy run without altering anything, then when I restart the python program, the system doesn't seem to be synchronised anymore. It is like opening the connection throws in some garbage or whatever, but the values of outByte1, outByte2, ets .... are not received by their corresponding inByte1, inByte2, ....
Instead it looks like the inbyte1 receives data which was send by outByte2,3,4,5.... but that means the data is useless for further processing.
Is there something I can do to make 100% sure at the beginning of my python script that my data is synced ?
I am trying to make a very simple program in python which sends binary data to a teensy 3.6 in chunks of 5 bytes.
I wrote the following program for this in python
Code:
from PIL import Image
import serial
ser = serial.Serial('/dev/cu.usbmodem2641171', 9600)
chip_img = Image.open('bart.png');
size = width, height = chip_img.size;
chip_img = chip_img.convert('1')
for yy in range(0,height):
for xx in range(0,width):
coordinate = x, y = xx, yy
waarde= chip_img.getpixel( coordinate )
if waarde == 0:
print "pixel",xx,",",yy," must burn"
outByte1= (xx>>8) & 0xff
outByte2= xx & 0xff
outByte3= (yy>>8) & 0xff
outByte4= yy & 0xff
ser.write(chr(outByte1))
ser.write(chr(outByte2))
ser.write(chr(outByte3))
ser.write(chr(outByte4))
ser.write(chr(7))
ser.close()
Now this code just opens an image (bart.png) , converts it to black and white and for each black pixel in the image it sends 5 bytes.
byte 1 + 2 are the 2 bytes making the X coordinate of the pixel
byte 3 + 4 are the 2 bytes making the Y coordinate of the pixel
the fifth byte is by default always the number 7 (for the time being, since this will also become a variable value later on)
The code does this for each black pixel and then stops.
This is the teensy side of code
Code:
void setup() {
// put your setup code here, to run once:
analogWriteResolution(12);
analogWrite(A22,2048); //center DAC1
analogWrite(A21,2048); //center DAC0
analogWrite(A9,10); //set a very low pwm duty on pin A9
Serial.begin(9600);
}
void loop() {
if (Serial.available()>=5)
{
byte inByte1 = Serial.read();
byte inByte2 = Serial.read();
byte inByte3 = Serial.read();
byte inByte4 = Serial.read();
byte inByte5 = Serial.read();
analogWrite(A22,4*(256*inByte1+inByte2));
analogWrite(A21,8*(256*inByte3+inByte4));
delay(50);
analogWrite(A9,4096);
delay(inByte5);
analogWrite(A9,10);
}
}
In the loop I wait until there are 5 or more bytes available on serial, then read these 5 bytes and do some setting with these values.
The loop continuously checks for bytes in serial
This works very nicely. I upload the code to teensy, I start the python script and everything works fine THE FIRST TIME.
If the python program ends and I let the teensy run without altering anything, then when I restart the python program, the system doesn't seem to be synchronised anymore. It is like opening the connection throws in some garbage or whatever, but the values of outByte1, outByte2, ets .... are not received by their corresponding inByte1, inByte2, ....
Instead it looks like the inbyte1 receives data which was send by outByte2,3,4,5.... but that means the data is useless for further processing.
Is there something I can do to make 100% sure at the beginning of my python script that my data is synced ?