Python + Arduino with Mac OS X

18,519

Solution 1

The easiest way to communicate in Python with the Arduino (or any microcontroller with serial) is using pySerial.

Here's an example:

import serial
s = serial.Serial(port='/dev/tty.usbmodemfa141', baudrate=9600)

s.write('text')
s.read()
s.readline()

PS: If you're using Python 3, you should send bytes instead of strings (that is, b'text').

Solution 2

On my side I've solved Serial error on OSX using the sudo command; I think that on OSX you have to get admin rights to communicate throw /dev/cu.usbmodem14101 with Serial after a pip install.

Solution 3

I have done this using Perl under Linux, but have no experience with Python or Mac. I can give you a few pointers to look for.

First, in your Python program you need to put the proper device address for your USB port in serialPath as otherwise your data will not reach the Arduino. In Linux I did a lsusb after I connected the board and found the device name from that.

In your Arduino code change it to be

void loop()
{
   if(Serial.available() > 0)
   {
       d = Serial.read();
       Serial.println(d,BYTE);
   }
}

as otherwise you will be dumping a bunch of -1s if there is no data.

Share:
18,519
danem
Author by

danem

Updated on June 16, 2022

Comments

  • danem
    danem almost 2 years

    I'm having trouble communicating between my Arduino and Python. I have a couple of questions that I hope can be answered, but first and most importantly, I need to simply establish a connection.

    For Windows, apparently the solution is rather convenient, but on Mac OS X, I apparently need to access some system files (which I am not familiar with). The Python documentation points me to the specific post Re: Can Python do serial port stuff?, but I don't think it quite serves my purposes.

    At this point, trying to merely see evidence of communication I've tried this.

    Arduino:

    void setup(){
        Serial.begin(9600);
    }
    
    void loop()
    {
        int d = Serial.read();
        Serial.println(d,BYTE);
    }
    

    Python: (pretty much from the mentioned link...)

     #!usr/bin/python
     import os, fcntl, termios, sys
    
     serialPath = '/dev/tty.usbmodemfa141'
    
     ser= os.open(serialPath, 0)
     [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] = range(7)
     settings = termios.tcgetattr(ser)
     settings[ospeed] = termios.B9600
     settings[ispeed] = termios.B0
     print 2
    

    As evidenced here, I really don't understand what the modules I am importing are doing exactly. While reading the documentation I see no obvious way to send data over serial. So am I right in guessing that whatever the output of this program is it will be sent over automatically?

  • nicolaskruchten
    nicolaskruchten almost 13 years
    this is by far the easiest, +1
  • danem
    danem almost 13 years
    I was under the impression that pyserial won't work on osx... Am i mistaken?
  • JBernardo
    JBernardo almost 13 years
    It works on OSX. I only had problems with pyserial under Windows 64-bit
  • danem
    danem almost 13 years
    Oh, alright. Heh. I've just installed it and seems to be working fine... What is this about then? Just out of curiosity... docs.python.org/faq/… I was initially going to use pyserial, but I thought that this was saying it wouldn't work for osx.... D:
  • JBernardo
    JBernardo almost 13 years
    I don't know, but OSX is a BSD based system, so you don't have to worry about the second url.
  • David Johnston
    David Johnston over 6 years
    It doesn't like me on OSX: AttributeError: 'module' object has no attribute 'Serial'. With a bit more investigation it turned out that I have serial already in my namespace from something else.