def checkErr(* flag): for index in range(len(flag)): if flag[index] != 0: SerialManager.read_existing() #clear rx_buffer if SerialManager.write('O000'+'\r'): relayStatus=SerialManager.read_to('\r') print 'relay status is:',relayStatus
def serialProcessor(): global quitFlag RelayStatus = 0 ''' process serial command and send response. 'V' // UART-command for receiving 'T' //UART-command for read temperature 'F' //UART-command for read flag 'R' //UART-command for read total voltage 'I' //UART-command for read total current 'C' //UART-command for close the relay 'O' //UART-command for open the relay 'P' //UART-command for receiving the PWM rate 'J' //UART-command for read time 'K' //UART-command for kill the simulation ''' while True: try: chars = SerialManager.read_to('\r') if len(chars) > 0: print('Processing Command:' + chars) stackNum = int(chars[1]) cellNum = int(chars[2:4]) #cellNum=int(chars[2])*10 + int(chars[3]) index = stackNum * tm.NumberOfCells + cellNum #print "stackNum %d, cellNum %d, index %d" %(stackNum,cellNum,index) if chars[0] == 'V': SerialManager.write( str(tm.status['voltageList'][index]) + '\r') elif chars[0] == 'B': tm.bg[index].balanceRate = 1.0 * int(chars[4:]) / 100.0 #print 'balcenRate is:', tm.bg[index].balanceRate elif chars[0] == 'T': SerialManager.write(str(0) + '\r') elif chars[0] == 'R': totalVoltage = sum(tm.status['voltageList'][:]) SerialManager.write(str(totalVoltage) + '\r') elif chars[0] == 'I': totalCurrent = sum(tm.status['voltageList'][:]) / tm.load SerialManager.write(str(totalCurrent) + '\r') elif chars[0] == 'C': RelayStatus = 1 SerialManager.write(str(RelayStatus) + '\r') elif chars[0] == 'O': RelayStatus = 0 SerialManager.write(str(RelayStatus) + '\r') elif chars[0] == 'J': SerialManager.write(str(tm.clock) + '\r') elif chars[0] == 'K': quitFlag = True else: print 'Command Not Defined' SerialManager.write('Command Not Defined' + '\r') time.sleep(0.08) except: print("error when processing command") time.sleep(0.1)
def serialProcessor(): global quitFlag RelayStatus=0 ''' process serial command and send response. 'V' // UART-command for receiving 'T' //UART-command for read temperature 'F' //UART-command for read flag 'R' //UART-command for read total voltage 'I' //UART-command for read total current 'C' //UART-command for close the relay 'O' //UART-command for open the relay 'P' //UART-command for receiving the PWM rate 'J' //UART-command for read time 'K' //UART-command for kill the simulation ''' while True: try: chars = SerialManager.read_to('\r') if len(chars) > 0: print('Processing Command:' + chars) stackNum = int(chars[1]) cellNum = int(chars[2:4]) #cellNum=int(chars[2])*10 + int(chars[3]) index = stackNum*tm.NumberOfCells+cellNum #print "stackNum %d, cellNum %d, index %d" %(stackNum,cellNum,index) if chars[0] == 'V': SerialManager.write(str(tm.status['voltageList'][index])+ '\r') elif chars[0] == 'B': tm.bg[index].balanceRate = 1.0 * int(chars[4:])/100.0 #print 'balcenRate is:', tm.bg[index].balanceRate elif chars[0] == 'T': SerialManager.write(str(0)+ '\r') elif chars[0] == 'R': totalVoltage=sum(tm.status['voltageList'][:]) SerialManager.write(str(totalVoltage)+ '\r') elif chars[0] == 'I': totalCurrent=sum(tm.status['voltageList'][:])/tm.load SerialManager.write(str(totalCurrent)+ '\r') elif chars[0] == 'C': RelayStatus=1 SerialManager.write(str(RelayStatus)+ '\r') elif chars[0] == 'O': RelayStatus=0 SerialManager.write(str(RelayStatus)+ '\r') elif chars[0] == 'J': SerialManager.write(str(tm.clock)+ '\r') elif chars[0] == 'K': quitFlag = True else: print 'Command Not Defined' SerialManager.write('Command Not Defined'+ '\r') time.sleep(0.08) except: print("error when processing command") time.sleep(0.1)
def sendCommand(command): #clear rx_buffer SerialManager.read_existing() if SerialManager.write(command + '\r'): return SerialManager.read_to('\r') else: return 'error'
def stopBalancing(stackNum,cellNum): balanceCommand='S'+str(stackNum)+str(cellNum) SerialManager.read_existing() #clear rx_buffer if SerialManager.write( balanceCommand +'\r'): balanceStopTime=SerialManager.read_to('\r') print 'balance stop time is:',balanceStopTime
print 'balance stop time is:',balanceStopTime if __name__ == '__main__': #if not SERIAL_PORT: # SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) commandGenerator(); flag=[] voltage=[] temperature=[] relayStatus=0 SerialManager.connect(SERIAL_PORT, BITSPERSECOND) #while SerialManager.is_connected(): SerialManager.read_existing() #clear rx_buffer if SerialManager.write('C000'+'\r'): relayStatus=SerialManager.read_to('\r') print 'relay status is:',relayStatus SerialManager.read_existing() #clear rx_buffer if SerialManager.write('R000'+'\r'): totalVoltage=SerialManager.read_to('\r') print 'total voltage is:',totalVoltage SerialManager.read_existing() #clear rx_buffer if SerialManager.write('I000'+'\r'): totalCurrent=SerialManager.read_to('\r') print 'total current is:', totalCurrent SerialManager.read_existing() #clear rx_buffer if SerialManager.write('F800'+'\r'): readCellFlagStatus=SerialManager.read_to('\r')
SERIAL_PORT = args.port print "Using serial device '" + SERIAL_PORT + "' from command line." else: print 'Please select a serialport via args' if os.name == 'nt': #sys.platform == 'win32': GUESS_PREFIX = "Arduino" elif os.name == 'posix': if sys.platform == "linux" or sys.platform == "linux2": GUESS_PREFIX = "2341" # match by arduino VID else: GUESS_PREFIX = "tty.usbmodem" else: GUESS_PREFIX = "no prefix" if __name__ == '__main__': #if not SERIAL_PORT: # SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) while SerialManager.is_connected(): command = raw_input("Input your command>>>") if len(command) != 0: SerialManager.read_existing() #clear rx_buffer if SerialManager.write(command + '\r'): str = SerialManager.read_to('\r') print(str) else: print("write error!") break print("Port closed!")