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

2024-10-02 08:19:06 发布

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

我在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)

通过计算机上的处理程序,我连接到套接字并发送由comas分隔的三个数字组成的字符串:“90,90,90”。你知道吗

Waiting for a conection conected 192.168.1.196 Received from socket 90,90,90

90,90,90

Received from socket 90,90,90

90,90,90

Received from socket 90,90,90

90,90,90

Received from socket 90,90,90

90,90,90

Received from socket 90,90,90

90,90,90

Received from socket 90,90,90

90,90,90

Received from socket 90,90,90

90,90,90

Received from socket 90,90,90

90,90,90

Received from socket 90,90,90

90,90,90

Received from socket 90,125,125 90,125,125 Received from socket

Traceback (most recent call last): File "ClientForComputerFirmata2.py", line 126, in val1, val2, val3 = vals.split(",") ValueError: need more than 1 value to unpack

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

非常感谢!你知道吗


Tags: fromboardnonegetvaluepinserialsocket

热门问题