Note that there are some explanatory texts on larger screens.

plurals
  1. POpySerial works fine in Python interpreter, but not standalone
    primarykey
    data
    text
    <p>Good morning! Recently I bought an Arduino board to make sort of "light control" in my room. Here is the code of the firmware I wrote:</p> <pre><code>int control = 0; int pin = 0; void setup() { Serial.begin(9600); for(pin = 0; pin &lt;= 13; pin++) pinMode(pin, OUTPUT); } void loop() { control = Serial.read(); if (control &gt; 0 &amp;&amp; control &lt;= 13) digitalWrite(control, HIGH); if (control &lt; 256 &amp;&amp; control &gt;= (256-13)) digitalWrite((256-control), LOW); } </code></pre> <p>After that, I used pySerial from Python interpreter to control the pins, and everything was working fine. Here is a piece of interpreter output:</p> <pre><code>Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import serial &gt;&gt;&gt; ser = serial.Serial('/dev/ttyUSB0', 9600) &gt;&gt;&gt; ser.write(chr(12)) &gt;&gt;&gt; # The light turned on here ... &gt;&gt;&gt; ser.write(chr(256-12)) &gt;&gt;&gt; # The light turned off here ... </code></pre> <p>Then I decided to write a simple Python script to do the same:</p> <pre><code>#!/usr/bin/env python import serial import time ser = serial.Serial('/dev/ttyUSB0', 9600) ser.write(chr(12)) time.sleep(1) ser.write(chr(256-12)) </code></pre> <p>But it doesn't work at all! The Arduino shows that something was received during the time I launched the script, but nothing happens. Here is a piece of strace output for the script:</p> <pre><code>open("/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NONBLOCK) = 3 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(3, SNDCTL_TMR_START or TCSETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 open("/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NONBLOCK) = 4 ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(4, SNDCTL_TMR_START or TCSETS, {B9600 -opost -isig -icanon -echo ...}) = 0 ioctl(4, SNDCTL_TMR_TIMEBASE or TCGETS, {B9600 -opost -isig -icanon -echo ...}) = 0 write(4, "\f", 1) = 1 close(4) = 0 rt_sigaction(SIGINT, {SIG_DFL, [], SA_RESTORER, 0x7f45cf4c88f0}, {0x4d9820, [], SA_RESTORER, 0x7f45cf4c88f0}, 8) = 0 exit_group(0) = ? </code></pre> <p>It looks like everything should be fine, so I don't know what the problem can be. I would appreciate any help, many thanks in advance!</p> <p>PS When I run the program under PDB, everything works fine. A Heisenbug.</p> <p>UPDATE: I made the controller send me back the data it was receiving and it looks like it isn't receiving anything when I am running the script, but receives everything when I send the data from the interpreter. The code of the firmware now looks like this:</p> <pre><code>int control = 0; int pin = 0; void setup() { Serial.begin(9600); for(pin = 0; pin &lt;= 13; pin++) pinMode(pin, OUTPUT); } void loop() { if (Serial.available() &gt; 0) { control = Serial.read(); if (control &lt;= 13) digitalWrite(control, HIGH); if (control &lt; 256 &amp;&amp; control &gt;= (256-13)) digitalWrite((256-control), LOW); Serial.println(control); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload