Pages: [1]   Go Down
Author Topic: PySerial not stable  (Read 1835 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys I am trying to control some servos from a website I have running. Everything seems to work okay but the python script seems to crash after a certain number of iterations. I either get.

Traceback (most recent call last):
  File "maincontrol1.py", line 76, in <module>
    val3 = ser.readline()
  File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 453, in read
    buf = os.read(self.fd, size-len(read))
OSError: [Errno 11] Resource temporarily unavailable


or a similar error that says "device reports readiness to read but returned no data".



EDIT: There is also code for the uno that controls the servos. Let me know if that code is needed.

Here is some of the python code:

Code:
import serial
import nltk  
from urllib import urlopen
import time
import os

serialport = "/dev/ttyACM6"
ser = serial.Serial(serialport, 115200)
time.sleep(1)
print "we are connected"

while (1):



#***************************Servo***************************************
# URL for servo position
url = "server url"  
html = urlopen(url).read()     # Read HTML
raw = nltk.clean_html(html) # Gets conent only

#url1 = "server url"
#html1 = urlopen(url1).read()
#html2 = urlopen(url2).read()
#print("while statement")

if(raw != 'off'): # Only want 3 digits
# Send position to arduino
#print("Move to:")
#print(raw)
ser.write(raw)
#time.sleep(.005)
elif(raw == 'off'):
print("Elif statement")
url = "server url"  
html = urlopen(url).read()     # Read HTML
raw = nltk.clean_html(html) # Clean HTML
else:
print("Position out of range or invalid")
#time.sleep(.005)

#print("after if")
#******************************Camera***********************************
# Get state of command
url = "server url"  
html = urlopen(url).read()    # Get HTML
raw = nltk.clean_html(html) # Clean HTML

if (raw == '1'):
print("**Taking Photo**") # Does user want to take pictures
x = 'streamer -c /dev/video1 -o to directory'
y = 'convert image'
z = 'scp file to server'
os.system(x)
os.system(y)
os.system(z)

elif (raw == '0'):
print("**Camera Off**")
#else :
#print("Invalid Camera Value")

#****************Read Temp*************
#***************************Read Values***************************************
alertVal = ser.read()
val1 = ser.read()
val2 = ser.read()
val3 = ser.readline()
#*************************Analyze**************************************
if(alertVal == '1'):
x = 'scp file to server'
os.system(x)
print("alert")
    
#else :
#print(alertVal)

finVal = val1 + val2 + val3
#print(finVal)

#time.sleep(.1)
#print(alertVal)
#print (val1)
#print (val2)
#print (val3)
try:
# This will create a new file or **overwrite an existing file**.
f = open("temp.txt", "w")
try:
f.write(finVal) # Write a string to a file
#f.writelines(finVal) # Write a sequence of strings to a file
finally:
f.close()
except IOError:
pass
#Send the file to the server
x = 'scp file to server'
os.system(x)    

print "goodbye"

***************************************
Here is the arduino code

Code:
#include <Servo.h>
//Must display the string value of the digits produced
//Check for the stars
Servo horServo;  // create servo object to control a servo
Servo verServo;
//*********************THIS does servo, ping, and temp
boolean lastVal;
boolean curVal;
char sendVal[3];
char change;
//char testVal[2];
const int pingPin = 53;
int pos           = 0;    // variable to store the servo position
int i             = 0;
int j             = 0;
int switchPin     = 47;
int tempC         = 0;
int len;

void setup() {

  // initialize serial communication:
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial.println("We are Connected");
  //verServo.attach(9);  // attaches the servo on pin 9 to the servo
  //horServo.attach(10);
  
  //verServo.write(90);
  //horServo.write(90);
  
//  attachInterrupt(0, alert, RISING); //Arduino interrupt library
}

void loop()
{

//***********************Ping Distance Sensor**********************
  long duration, inches, cm;
    // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  

//  Serial.print(inches);
//  Serial.print("in, ");
//  Serial.print(cm);
//  Serial.print("cm");
//  Serial.println();
//*************************************************************************  
  
  
  
  // int incomingByte=0;
  //int outByte[3];
  // int outSum;
  char inByte;

//******************Read Direction Command**************************
 if (Serial.available() > 0) {
      
  // Read Servo Value
    inByte = Serial.read();
    Serial1.println(inByte);
 }
  
//*************Testing Button Read*******************
//  curVal = debounce(lastVal);
//  if (curVal == HIGH){
//    count++;
//  }
  int a = analogRead(A5);
  tempC = convert(a);
 /* Serial.print("Temp: ");
  Serial.println(tempC);
  Serial.print("Reading: ");
  Serial.println(a); */
  
  
 
  if (length(tempC) == 2){
    if (tempC > 90){
      sendVal[0] = '0';
      sendVal[1] = '9';
      tempC = tempC - 90;
    }
    else if (tempC >= 80){
      sendVal[0] = '0';
      sendVal[1] = '8';  
      tempC = tempC - 80;
    }
    else if (tempC >= 70){
      sendVal[0] = '0';
      sendVal[1] = '7';
      tempC = tempC - 70;
    }
    else if (tempC >= 60){
      sendVal[0] = '0';
      sendVal[1] = '6';
      tempC = tempC - 60;  
    }
    else if (tempC >= 50){
      sendVal[0] = '0';
      sendVal[1] = '5';
      tempC = tempC - 50;        
    }
    else if (tempC >= 40){
      sendVal[0] = '0';
      sendVal[1] = '4';
      tempC = tempC - 40;
    }
    else if (tempC >= 30){
      sendVal[0] = '0';
      sendVal[1] = '3';
      tempC = tempC - 30;
    }
    else if (tempC >= 20){
      sendVal[0] = '0';
      sendVal[1] = '2';
      tempC = tempC - 20;
    }
   else if (tempC >= 10){
      sendVal[0] = '0';
      sendVal[1] = '1';
      tempC = tempC - 10;
      
   }
  
  }
  

   else if (length(tempC) == 1){
     sendVal[0] = '0';
     sendVal[1] = '0';
    
   }
  tempC = tempC + '0';
  sendVal[2] = tempC;
  
  if (cm > 10){
    Serial.print("1");
  }
  else Serial.print(0);
  
  
  if (length(tempC) < 3){
    //Serial.println("length 2");
    Serial.print(sendVal[0]);
    Serial.print(sendVal[1]);
    Serial.println(sendVal[2]);
  
  }
  
  else if (length(tempC) == 3){
    Serial.println("Length 3");
    Serial.println(tempC);
  }
  /*Serial.print("Temp: ");
  Serial.println(tempC); */
  //Serial.println(sendVal);
  lastVal = curVal;
  delay(1);
}

/*void alert(){
  Serial.println("1");
  while (Serial.available() == 0);
  int ackVar = Serial.read();
    if (ackVar == '1'){
       Serial.println("1");
    }  
}
*/
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
  delay(5);
  current = digitalRead(switchPin);
}
return current;
}

int length(int someValue){
int valLen = 0;

if(someValue > 9999)
   valLen = 5;
else if(someValue > 999)
   valLen = 4;
else if(someValue > 99)
   valLen = 3;
else if(someValue > 9)
   valLen = 2;
else
   valLen = 1;
  
return valLen;
  
}

int convert(int reading){
  float calc = (reading*205)/307;
  int rCalc = round(calc);
  return rCalc;
}

long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}




Let me know if there is anything else you need or if I need to reformat my code for the forum just let me know how to do it.
« Last Edit: November 10, 2012, 08:43:42 pm by SlapYourBaby » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Let me know if there is anything else you need or if I need to reformat my code for the forum just let me know how to do it.
Yes, you need to post your code correctly. Modify your post, select your code, select the # icon, and save the changes.

Post the Arduino code, to. After all, this IS an Arduino forum, not a python forum. You don't post your Arduino issues there, do you?

The messages seem to indicate some issue with sending and receiving serial data.

Quote
Here is some of the python code:
Snippets-r-us is down the internet a ways. Closer to the python forum. Here, we get to see ALL of your code, or your backside on the way out the door. All of your code is preferable.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The only thing I didn't include in the python code was the passwords and url for my website.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#***************************Read Values***************************************
alertVal = ser.read()
val1 = ser.read()
val2 = ser.read()
val3 = ser.readline()
How do you know that there is anything to read?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The python code reads the alert file off the server which always will exist. The Arduino code is printing parts of the string "sendVal" with the following code:
 
Code:
  if (length(tempC) < 3){
    //Serial.println("length 2");
    Serial.print(sendVal[0]);
    Serial.print(sendVal[1]);
    Serial.println(sendVal[2]);
   
  }
 
  else if (length(tempC) == 3){
    Serial.println("Length 3");
    Serial.println(tempC);
  }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The python code reads the alert file off the server which always will exist. The Arduino code is printing parts of the string "sendVal" with the following code:
But, you assume that the serial data will immediately be available to python to read. I'm afraid that you don't understand how ssslllooowwww serial data transmission can be, and the fact that it is not synchronous.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What options do I have to check if the data is there to be read?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What options do I have to check if the data is there to be read?
In C++, on the Arduino, you can use Serial.available().

In Python, on a PC? Beats me. This isn't a Windows OR Python forum.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using Ubuntu 12.02. Just to be clear, I am aware this is not 100% arduino and that's why I posted in this section. I am trying to communicate with Python which is software on my computer. If I am In the wrong by posting here I would like a moderator to message me.
Logged

Finland
Offline Offline
Jr. Member
**
Karma: 0
Posts: 96
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...Just to be clear, I am aware this is not 100% arduino and that's why I posted in this section. I am trying to communicate with Python which is software on my computer.
...
I am very interested in your problem, I have similar issues. If strictly only Arduino-talk is allowed here, how would we solve problems in connecting Arduino to ANYTHING? If python forum people tell us that we are not allowed to talk about python code connecting to Arduino, where would we find a forum devoted to "Python-Arduino connections"?

The main idea for Arduino's success in my opinion has been the easy communication between the host computer and arduino for every beginner. Let us not kill that idea.
Logged

Pages: [1]   Go Up
Jump to: