python and arduino

All,

I am working on a underwater robotics competition (MATE, ROV) and will be implementing arduino as controls. We will be having 2 sensors, a pressure and temp. I have a simple GUI thing written up in python, here it is:

app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

if name == 'main':import sys
from PyQt4 import QtGui

class Window(QtGui.QMainWindow):
def init(self):
super(Window, self).init()

set central widget

widget = QtGui.QWidget(self)
self.setCentralWidget(widget)

set layout

layout = QtGui.QGridLayout(self)
widget.setLayout(layout)

create temperature stuff

layout.addWidget(QtGui.QLabel('Temperature', widget), 0, 0)
self.lbl_temp = QtGui.QLabel('34F', widget)
layout.addWidget(self.lbl_temp, 0, 1)

create pressure stuff

layout.addWidget(QtGui.QLabel ('Pressure', widget), 1, 0)
self.lbl_pressure = QtGui.QLabel('20psi', widget)
layout.addWidget(self.lbl_pressure, 1, 1)

def main():
main()

#QLineEdit('',parent)
#textUpdated

I just have random numbers for the sensors as of now. Sooooo, my question is how can i get the sensor data from the arduino and to the laptop running the python code? Does anyone have any example codes? Oh, also the arduino will be connected to the computer with ethernet in the end but for now it is just the normal USB connection. Right now i just need for the arduino to communicate with the computer but will aventially need the sensors, so if you have a code for either one please help me out!

KF7QAZ

This Python - Arduino demo should point you in the right direction. Also look at the more recently written Serial Input Basics

You may also be interested in this Python GUI demo

...R

any other tips or codes?

KF7QAZ:
any other tips or codes?

You need to tell me what you need that I have not already covered.

...R

Sorry! that was just general if anyone else has any input, sorry again!

KF7QAZ

So what is wrong with my stuff?

Feedback is always useful. Maybe I can improve something.

...R

Nothing is. oh on the link you gave me, I can open the arduino file but cant open the python file? would different versions of python do this? Would different bit rates do this (I'm running 32 bit)?

KF7QAZ

KF7QAZ:
but cant open the python file?

I presume you mean the file ComArduino2.py?

What do you mean by “can’t open” - please describe what you have tried. I presume you can view the file in your text editor?

Are you getting error messages when you try to run the program. If so please post them.

What operating system are you using?

The Python code was written with Python 2.7. I have not tried it with Python 3.

…R

To be honest I'm not sure what exactly is wrong, im running windows 10 on 32 bit. could you paste the python code hear please?

KF7QAZ:
To be honest I’m not sure what exactly is wrong,

Unless you provide a lot more specific information I cannot provide any useful advice. You have not answered any of my questions in Reply #7.

This is the code for the file ComArduino2.py - if I could download it to include it here I’m sure you could also do so.

# 19 July 2014

# in case any of this upsets Python purists it has been converted from an equivalent JRuby program

# this is designed to work with ... ArduinoPC2.ino ...

# the purpose of this program and the associated Arduino program is to demonstrate a system for sending 
#   and receiving data between a PC and an Arduino.

# The key functions are:
#    sendToArduino(str) which sends the given string to the Arduino. The string may 
#                       contain characters with any of the values 0 to 255
#
#    recvFromArduino()  which returns an array. 
#                         The first element contains the number of bytes that the Arduino said it included in
#                             message. This can be used to check that the full message was received.
#                         The second element contains the message as a string


# the overall process followed by the demo program is as follows
#   open the serial connection to the Arduino - which causes the Arduino to reset
#   wait for a message from the Arduino to give it time to reset
#   loop through a series of test messages
#      send a message and display it on the PC screen
#      wait for a reply and display it on the PC

# to facilitate debugging the Arduino code this program interprets any message from the Arduino
#    with the message length set to 0 as a debug message which is displayed on the PC screen

# the message to be sent to the Arduino starts with < and ends with >
#    the message content comprises a string, an integer and a float
#    the numbers are sent as their ascii equivalents
#    for example <LED1,200,0.2>
#    this means set the flash interval for LED1 to 200 millisecs
#      and move the servo to 20% of its range

# receiving a message from the Arduino involves
#    waiting until the startMarker is detected
#    saving all subsequent bytes until the end marker is detected

# NOTES
#       this program does not include any timeouts to deal with delays in communication
#
#       for simplicity the program does NOT search for the comm port - the user must modify the
#         code to include the correct reference.
#         search for the lines 
#               serPort = "/dev/ttyS80"
#               baudRate = 9600
#               ser = serial.Serial(serPort, baudRate)
#


#=====================================

#  Function Definitions

#=====================================

def sendToArduino(sendStr):
  ser.write(sendStr)


#======================================

def recvFromArduino():
  global startMarker, endMarker
  
  ck = ""
  x = "z" # any value that is not an end- or startMarker
  byteCount = -1 # to allow for the fact that the last increment will be one too many
  
  # wait for the start character
  while  ord(x) != startMarker: 
    x = ser.read()
  
  # save data until the end marker is found
  while ord(x) != endMarker:
    if ord(x) != startMarker:
      ck = ck + x 
      byteCount += 1
    x = ser.read()
  
  return(ck)


#============================

def waitForArduino():

   # wait until the Arduino sends 'Arduino Ready' - allows time for Arduino reset
   # it also ensures that any bytes left over from a previous message are discarded
   
    global startMarker, endMarker
    
    msg = ""
    while msg.find("Arduino is ready") == -1:

      while ser.inWaiting() == 0:
        pass
        
      msg = recvFromArduino()

      print msg
      print
      
#======================================

def runTest(td):
  numLoops = len(td)
  waitingForReply = False

  n = 0
  while n < numLoops:

    teststr = td[n]

    if waitingForReply == False:
      sendToArduino(teststr)
      print "Sent from PC -- LOOP NUM " + str(n) + " TEST STR " + teststr
      waitingForReply = True

    if waitingForReply == True:

      while ser.inWaiting() == 0:
        pass
        
      dataRecvd = recvFromArduino()
      print "Reply Received  " + dataRecvd
      n += 1
      waitingForReply = False

      print "==========="

    time.sleep(5)


#======================================

# THE DEMO PROGRAM STARTS HERE

#======================================

import serial
import time

print
print

# NOTE the user must ensure that the serial port and baudrate are correct
serPort = "/dev/ttyS80"
baudRate = 9600
ser = serial.Serial(serPort, baudRate)
print "Serial port " + serPort + " opened  Baudrate " + str(baudRate)


startMarker = 60
endMarker = 62


waitForArduino()


testData = []
testData.append("<LED1,200,0.2>")
testData.append("<LED1,800,0.7>")
testData.append("<LED2,800,0.5>")
testData.append("<LED2,200,0.2>")
testData.append("<LED1,200,0.7>")

runTest(testData)


ser.close

As you are using Windows you will have to edit the file to put in the correct serial port reference.

…R

but cant open the python file?

Could it be that the python file can't open the COM port?

Instead of:
    serPort = "/dev/ttyS80"
you have to use:
    serPort = "COM1"

Replace COM1 by the used COM port.

/Joe

Sorry been real busy lately, but got everything running! thanks

KF7QAZ:
Sorry been real busy lately, but got everything running! thanks

It would be useful for others if you explain what you did.

...R