I'm workin on a node for blender to control an arduino. I notice it goes really slow after a while so I isolated the problem. It is good in maintaining 60 frames a second, then after around 40 seconds (pretty exact), it drops to a pretty exact 1 fps for some reason.
What could be going on?
import serial
import time
ser = serial.Serial('COM3', 115200)
c = 0
while True:
c+=1
print("ok ", c)
numIn = "100n"
ser.write(numIn.encode('ascii'))
time.sleep(1./60)
arduino code:
// special thx to Nick Gammon
const unsigned int MAX_INPUT = 50;
unsigned long delay_time = 300;
unsigned long last_toggle_time = 0;
int current_state = LOW;
void setup () {
Serial.begin (115200);
pinMode(12, OUTPUT);
}
void process_data (const char * data) {
Serial.println (data);
delay_time = atoi(data);
}
void processIncomingByte (const byte inByte) {
static char input_line [MAX_INPUT];
static unsigned int input_pos = 0;
switch (inByte) {
case 'n': // end of text
input_line [input_pos] = 0; // terminating null byte
process_data (input_line);
input_pos = 0;
break;
case 'r':
break;
default:
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
}
}
void loop() {
while (Serial.available () > 0) {
processIncomingByte (Serial.read ());
}
unsigned long m = millis();
if (m > last_toggle_time + delay_time) {
current_state = current_state == HIGH ? LOW : HIGH;
digitalWrite(12, current_state);
last_toggle_time = m;
}
}
ser.readLine(). Maybe a new topic for the hanging? – clankill3r Apr 07 '16 at 07:28nas a delimiter in a rather unusual way. In my code it was\nand\r(newline and carriage return). When you doSerial.println (data);you are adding two more bytes (println adds a carriage-return and linefeed) so you are printing 6 bytes for every four you receive. It doesn't totally surprise me that something fills up after 40 seconds, however at 115200 baud that shouldn't really be an issue for 60 x 6 bytes. – Nick Gammon Jun 06 '16 at 21:31