从Socket接收一个字符串并用“,”分成3个整数

时间:2015-02-19 12:48:18

标签: python string sockets split

我在RPi中使用此代码:

import serial, time, sys, threading, socket
import RPi.GPIO as GPIO
import serial
from pyfirmata import Arduino, util

### pyFirmata ###########################
# PINS: Digital outputs por PWM para simular um sinal analogico
#########################################
serialPort = '/dev/ttyAMA0' #use dmesg to check
BATTERY_LEVEL_PIN = 'a:5:i' #18?? 4 nao e analog
CAMERA_PIN = 'd:11:s'
LEFT_MOTOR_PIN = 'd:10:s'
RIGHT_MOTOR_PIN = 'd:5:s'
FLASH_LIGHT_PIN = 'd:2:o'
MOODLIGHT_RED_PIN = 'd:7:o'
MOODLIGHT_GREEN_PIN = 'd:8:o'
MOODLIGHT_BLUE_PIN = 'd:9:o'
PWM_MASTER_PIN = 'd:6:p'
MOTOR_MAX_VALUE = 170
MOTOR_MIN_VALUE = 8
CAMERA_MAX_VALUE = 135
CAMERA_MIN_VALUE = 30 
BATTERY_MIN = 0.5
BATTERY_MAX = 0.65
#########################################


#print 'setup done'
dim = False
step = 0.05
value = 0
cameraState = None
previousCameraState = None
cameraStateChanged = False
batteryCritical = False
behaviourRunning = False

board = None
moodPinR = None
moodPinG = None
moodPinB = None
pwmMaster = None
batteryPin = None
camPin = None
leftMotorPin = None
rightMotorPin = None
flashPin = None



def init(serial):
    print 'I WAS CALLED!!'
    global batteryPin, camPin, leftMotorPin, rightMotorPin, flashPin, moodPinR, moodPinG, moodPinB, pwmMaster, board
    board = Arduino(serial)
    it = util.Iterator(board, 0.05) #object board, interval
    it.start()
    time.sleep(0.01)
    batteryPin = board.get_pin(BATTERY_LEVEL_PIN)
    camPin = board.get_pin(CAMERA_PIN)
    leftMotorPin = board.get_pin(LEFT_MOTOR_PIN)
    rightMotorPin = board.get_pin(RIGHT_MOTOR_PIN)
    flashPin = board.get_pin(FLASH_LIGHT_PIN)
    moodPinR = board.get_pin(MOODLIGHT_RED_PIN)
    moodPinG = board.get_pin(MOODLIGHT_GREEN_PIN)
    moodPinB = board.get_pin(MOODLIGHT_BLUE_PIN)
    pwmMaster = board.get_pin(PWM_MASTER_PIN)
    # using iterator declared before, to prevent serial from overflowing
    batteryPin.enable_reporting()
    print 'JOB DONE!!'

# prints serial port information (not all settigs are displayed, only the ones modifiable by the user)
def printSerial(serial):
    print '------------------------------------------------------'
    print 'Opened port ' + serial.name+' with settings:'
    print 'Baudrate: ' + str(serial.getBaudrate())
    print 'Bytesize: ' + str(serial.getByteSize())
    print 'Read timeout: ' + str(serial.getTimeout())
    print 'XonXoff: ' + str(serial.getXonXoff())
    print 'Parity: ' + str(serial.getParity())
    print '------------------------------------------------------'


# custom funtion to clamp motor values
def clampMotor(value):
    return max(MOTOR_MIN_VALUE,min(value,MOTOR_MAX_VALUE))

# custom funtion to clamp camera values
def clampCamera(value):
    return max(CAMERA_MIN_VALUE,min(value,CAMERA_MAX_VALUE))


def writeValues(cam, mot1, mot2):
    camPin.write(clampCamera(cam))
    leftMotorPin.write(clampMotor(mot1))
    rightMotorPin.write(clampMotor(mot2))



init(serialPort)

# Sockets:

host = ''
porti = 1976
backlog = 5

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host,porti))
s.listen(backlog)


# While port is open
while True:
#   ser.write("0")
    print "Waiting for a conection"
    client, address = s.accept()
    print "conected " + address[0] 
    while True:
        vals = client.recv(10)
        print "Received from socket " + vals
        if vals == '':
            break

        else:
                        val1, val2, val3 = vals.split(",")
                        val1 = int(val1)
                        val2 = int(val2)
                        val3 = int(val3)
            writeValues(val1,val2,val3)
            time.sleep(0.5)

通过计算机上的处理程序,我连接到套接字并发送由逗号分隔的三个数字的字符串:" 90,90,90"例如。

  

等待连接   conected 192.168.1.196从套接字90,90,90收到

     

90,90,90

     

从套接字90,90,90收到

     

90,90,90

     

从套接字90,90,90收到

     

90,90,90

     

从套接字90,90,90收到

     

90,90,90

     

从套接字90,90,90收到

     

90,90,90

     

从套接字90,90,90收到

     

90,90,90

     

从套接字90,90,90收到

     

90,90,90

     

从套接字90,90,90收到

     

90,90,90

     

从套接字90,90,90收到

     

90,90,90

     

从套接字90,125,125收到   90125125   从套接字

收到      

Traceback(最近一次调用最后一次):文件   " ClientForComputerFirmata2.py",第126行,in       val1,val2,val3 = vals.split(",")ValueError:需要多于1个值才能解压缩

由于某种原因,当我更改字符串数字的值时,它表示它可以被分割,因为它只有一个值,我不知道为什么......你能帮我吗? ?

非常感谢你!

0 个答案:

没有答案