为什么这个python程序在使用raspberry pi时不能在IDLE中工作?

2024-10-01 15:28:22 发布

您现在位置:Python中文网/ 问答频道 /正文

通过串行端口与多个pH模块进行通信。pH模块通过使用8574N芯片的i2c总线通过多路复用器选择。当raspberry pi启动后,程序在终端中正常工作,但是,如果要停止并重新启动程序,则无法正确初始化。另外,这个程序在python空闲(在多路复用器上选择随机设备)下不能正常工作。pi上的i2c或串行通信似乎没有正确初始化。例如,在IDLE中选择一个模块时,它会将不正确的模块调出到您选择的模块中,并抛出不正确的数据。然而,这一切在终端都能正常工作。在

有人有什么想法吗??在

任何帮助都将不胜感激!在

from smbus import SMBus
# from itertools import cycle
import time
import serial

usbport = '/dev/ttyAMA0'
ser = serial.Serial(usbport, 9600, timeout = 2)#
line = ""
data = ""
bus = SMBus(1) # Port 1 used on REV2 
print "Electronics Workshop PH Reader."
print "=========================="
global count
count = 0 #init count to 0
probevalue = ""



def inputsel(count):
   bus.write_byte(0x38, 0x00)
   # count = input(" Select ph unit '0 to 23'") # count now incremented during program
   count = input(" Select ph unit '0 to 23'")
   if (count< 16):
      data_sel = count
      data_sel = data_sel | 32 # or the 2 numbers together

   else:
      data_sel = count - 16
      data_sel = data_sel | 16 # or the 2 numbers together  


   bus.write_byte(0x38, data_sel) # send "count"Varable out to I2c Bus
   print str(data_sel)
   time.sleep (1) 
   data_sel = 0
   print "Reading Channel:" + str(count)

def write_serial(serial_data):
    global ser    
    data = ""
    line = ""
    status = ""
    ser.write(serial_data) # set ph unit to take one reading at a time 
    ser.write("\r") # set ph unit to take one reading at a time
    time.sleep (1)   
    ser.write(serial_data) # set ph unit to take one reading at a time 
    ser.write("\r")
    time.sleep (1)
    while status != "done":
            data = ser.read(1) # gets data from PH module '     
            if(data == "\r"): # check each bite of data to see if its a carriage return expecting "XX.XXr"
                    #carriage return sent diplay message and data 
                    print "Received from sensor:" + line
                    status = "done"
                    probevalue = line
                    line ="     "
                    ser.flushInput()
                    ser.flushOutput()
            else:
                    # all 5 bytes of data have not been received
                    line = line + data # add one to the varable line
                    #if(data == " "):
                     #       write_serial()

    return probevalue

def main():
      global serial_data
      serial_data = " "
      global count
      probevalue = " "
      count = 0
      bus.write_byte(0x38, 0)
      while 1:

         inputsel(count) #select the input based off count variable
         loop = range(0,7) 
         for loopcount in loop:
            if ((loopcount == 0) | (loopcount == 1)): #set command to #?
               serial_data = "#?\r" #set command to request id
               if (loopcount == 0): #if buffer needs clearing
                 print "Clearing Buffer....."                 
               else:  print "Requesting ID....."
               probevalue = write_serial(serial_data) #call write_serial with #? command put value into probevalue


            elif (loopcount >= 2): #set r command once buffer clear and ID obtained
               serial_data = "r\r"  #set command to read value
               if (loopcount == 2): print "Reading pH:" #output reaidng pH when loopcounter at 2
               probevalue = write_serial(serial_data) #call write_serial with r command put value into probevalue

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

Tags: todataiftimecountlineserialcommand

热门问题