How to use this code - Raspberry Pi Forums


i have solar inverter , nice person shared code:

code: select all

import urllib2 import serial, time  import sqlite3 import json import urllib import datetime import calendar import os  os_env = os.environ  app_dir = os.path.abspath(os.path.dirname(__file__))  # directory project_root = os.path.abspath(os.path.join(app_dir, os.pardir))  #commands crc cheats qpgs = '\x51\x50\x47\x53\x30\x3f\xda\x0d' qpigs = '\x51\x50\x49\x47\x53\xb7\xa9\x0d' #valid? qmchgcr ='\x51\x4d\x43\x48\x47\x43\x52\xd8\x55\x0d' #? qmuchgcr='\x51\x4d\x55\x43\x48\x47\x43\x52\x26\x34\x0d' #? qpiws = '\x51\x50\x49\x57\x53\xb4\xda\x0d' #valid? pop02 = '\x50\x4f\x50\x30\x32\xe2\x0b\x0d' # set sbu pop00 = '\x50\x4f\x50\x30\x30\xc2\x48\x0d' #set utility  #initialization , open port #possible timeout values: #    1. none: wait forever, block call #    2. 0: non-blocking mode, return #    3. x, x bigger 0, float allowed, timeout block call  ser = serial.serial() #ser.port = "/dev/ttyusb1" ser.port = "com7" #ser.port = "/dev/ttys2" ser.baudrate = 2400 ser.bytesize = serial.eightbits     #number of bits per bytes ser.parity = serial.parity_none     #set parity check: no parity ser.stopbits = serial.stopbits_one  #number of stop bits #ser.timeout = none                 #block read ser.timeout = 1                     #non-block read #ser.timeout = 2                    #timeout block read ser.xonxoff = false                 #disable software flow control ser.rtscts = false                  #disable hardware (rts/cts) flow control ser.dsrdtr = false                  #disable hardware (dsr/dtr) flow control ser.writetimeout = 2                #timeout write   try:      ser.open()  except exception, e:     print "error open serial port: " + str(e)     exit() 		 		 def floortime(dt=none, roundto=60):     """     floor datetime object time laps in seconds     dt : datetime.datetime object, default now.     """     import datetime      if dt none:         dt = datetime.datetime.now()      dt_min = datetime.datetime(datetime.minyear, 1, 1)     dt_min = dt_min.replace(tzinfo=dt.tzinfo)      seconds = (dt - dt_min).seconds     rounding = seconds // roundto * roundto     return dt + datetime.timedelta(0, rounding - seconds, -dt.microsecond)        def get_data():     #get inverter data axpert inverter     dc_1_u = 0     dc_1_i = 0     ac_1_u = 0     ac_1_p = 0     dc_2_u = 0     dc_2_i = 0     ac_2_u = 0     ac_2_p = 0     dc_3_u = 0     dc_3_i = 0     ac_3_u = 0     ac_3_p = 0     current_power = 0     daily_energy = 0     total_energy = 0  	     if ser.isopen():         try:             ser.flushinput()            #flush input buffer, discarding contents             ser.flushoutput()           #flush output buffer, aborting current output , discard in buffer             ser.write(qpgs)             time.sleep(0.1)             #give serial port sometime receive data             response = ser.readline()             nums = response.split(' ', 99)             print nums[0], nums[1],nums[2],nums[3],nums[4],nums[5],nums[6],nums[7],nums[8],nums[9],nums[10],nums[11],nums[12],nums[13],nums[14],nums[15],nums[16],nums[17],nums[18],nums[19],nums[20],nums[21],nums[22],nums[23],nums[24],nums[25], nums[26]             the_parallel_num = nums[0]             serial_number = nums[1]              work_mode = nums[2]              fault_code = nums[3]              grid_voltage = nums[4]             grid_frequency = nums[5]             ac_output_voltage = nums[6]             ac_output_frequency = nums[7]             ac_output_apparent_power = nums[8]             ac_output_active_power = nums[9]             load_percentage = nums[10]             battery_voltage = nums[11]             battery_charging_current = nums[12]             battery_capacity = nums[13]              pv_input_voltage = nums[14]             total_charging_current = nums[15]             total_ac_output_apparent_power = nums[16]              total_output_active_power = nums[17]             total_ac_output_percentage = nums[18]             inverter_status = nums[19]             output_mode = nums[20]             charger_source_priority = nums[21]             charger_source_priority = nums[22]             max_charger_range = nums[23]             max_ac_charger_current = nums[24]             pv_input_current_for_battery = nums[25]             battery_discharge_current = nums[26]             print "--------------" 		         except exception, e1:             print "error communicating...: " + str(e1)             ser.close()     else:         ser.close()         print "cannot open serial port " 	     return[1] #just return      def main():     while true:         time.sleep(1)         data = get_data()               if __name__ == '__main__':     main()  
bought pi , connected inverter via usb. not sure how should use code.
see in code ports must specified, dont know how find them.

when click run shell window opens , says
.....file "/home/pi/serial.py", line 5, in <module>
ser = serial.serial(
attributeerror: 'module' object has no attribute 'serial'
>>>

well, can't on main question how use script have no idea solar inverter or (although i'm sure google can me out there).

however, can fix immediate error: rename file other serial.py (and delete serial.pyc file in /home/pi folder. when "import serial" you're importing own serial script rather python serial module why line "ser = serial.serial()" fails.


raspberrypi



Comments