USB Serial, Python and a MacBook

Hello everyone,

I’m trying to write a program in Python, that takes the data from the sensors (temperature and luminosity) and write in a .txt file.
But I’m having a huge problem with Python (I’m using PySerial) and a MacBook Pro running OS X 10.11. because Python says that there is an error and as I’m not a pro in Python, and don’t know what I am doing wrong, because I think that the problem is the serial port :confused:

Can someone help me?

The Arduino Code is this one:

const int LDR = 0;
const int LM35 = 1;
int ValorTemp = 0;
float temperatura = 0;
int luminosidade = 0;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL);

}

void loop() {
  luminosidade = analogRead(LDR);
  ValorTemp = analogRead(LM35);
  temperatura = ValorTemp*0.1075268817204301;

  Serial.print(temperatura);
  Serial.print(" , ");
  Serial.print(luminosidade);
  Serial.println();
  delay(1000);

}

And the Python code is this one (not completed yet):

#arduino project for texting data

import serial

serial_speed = 9600
serial_port = '/dev/cu.usbmodem1421'
ser = serial.Serial(serial_port, serial_speed, timeout = 1)


while True:
     data = ser.readline()
     if data:
          print data

When I try to run the program in the Python, it gives me the following error:

Traceback (most recent call last):
File “/Users/***/Desktop/Arduino/Project Sensor/ProgramPython.py”, line 7, in
ser = serial.Serial(serial_port, serial_speed, timeout = 1)
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialutil.py”, line 180, in init
self.open()
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialposix.py”, line 294, in open
raise SerialException(msg.errno, “could not open port %s: %s” % (self._port, msg))
SerialException: [Errno 16] could not open port /dev/cu.usbmodem1421: [Errno 16] Resource busy: ‘/dev/cu.usbmodem1421’

  temperatura = ValorTemp*0.1075268817204301;

A float has 6 or 7 digits of precision. That value is ridiculous.

Do you have the Serial Monitor open at the same time?

This Python - Arduino demo may give you some ideas.

...R

PaulS:

  temperatura = ValorTemp*0.1075268817204301;

A float has 6 or 7 digits of precision. That value is ridiculous.

Do you have the Serial Monitor open at the same time?

Actually I just throw the number from the calculator (500 C degrees divided pr 2^10) but I think that is not the problem...

No, I do not have the serial monitor open when the program is running.

Robin2:
This Python - Arduino demo may give you some ideas.

...R

Thanks, I will take a look =)

This is my approach ( python 2.7 fairly long program ) http://www.instructables.com/id/Python-Terminal-for-Cheap-Pi-Arduino-Connection/

Russ

Had the same problem & it took me ages to find the answer:

Open Terminal and type:

ls /dev/cu.usb*

/dev/cu.usbmodem14101

Then plug in the name of your usbmodem into your Python code:
eg.

import warnings
import serial
import serial.tools.list_ports

print ('\n\nPYSERIAL\n')

sPort = '/dev/cu.usbmodem14101' #On Mac - find this using >ls /dev/cu.usb*

aSerialData = serial.Serial(sPort,9600) #COM port object

while True:

if (aSerialData.inWaiting()>0):
sData = aSerialData.readline()
print( sData )