如何从文本文件中读取值,然后将其传递给函数

时间:2019-04-20 04:53:58

标签: linux python-3.5

我正在尝试读取我称为status.txt的文本文件中的一些值,然后将这些值输入到函数中。由于该函数仅接受字节,因此我需要将其转换为字节,但是我越来越垃圾。这是我指的代码部分:

import time
import datetime
import serial
import os

# public variables
sensors = []  # list of sensor readings
wait = True
sensor_count = 10 # the zero based count of sensors


def pwr_solenoid(solenoid0=solenoid[0], solenoid1=solenoid[1], solenoid2=solenoid[2], solenoid3=solenoid[3]):
    # Defaults are for low signal values

    # compile output
    output = '9{solenoid0}{solenoid1}{solenoid2}{solenoid3}'.encode()

    with serial.Serial('/dev/ttyACM0', baudrate=9600) as ser:
        print("created connection to '/dev/ttyACM0'\n")
        print("going to send:\t'{}'".format(output))
        ser.write(output)
        ser.reset_output_buffer()

        # for testing to console
        print("Value sent to the uC:\t'{}'".format(output.decode()))

        # ***** READ from UART *****

这是主要功能的代码

def main():
    os.system("clear")

    # create the list of sensors (hardwired to 10 at the moment)

    # make sure that the list of sensor values is clear
    sensors.clear()

    for i in range(0, sensor_count):
        sensors.append(SensorReading(sensorID=i, humidity=0))

    print("Enter solenoid values in form of 1/0 (1 == HIGH, 0 == LOW) or 'exit' to exit\n")
    print('\n')
    print("default value will remain '0000' until altered...\n")
    print("Note, after altered values are input, it will wait for input from UART||USB\n")
    print('\n')

    solenoid = '0000'
    solenoid_raw = open("status.txt")
    solenoid = str(solenoid_raw)
    pwr_solenoid(solenoid0=solenoid[0], solenoid1=solenoid[1], solenoid2=solenoid[2], solenoid3=solenoid[3])

我正在尝试从文本文件中读取数字(假设它是字符串),将其传递给函数pwr_solenoid,然后应该将该值附加到9并将其转换为字节并通过串行端口写,这不是写正确的事情,任何帮助将不胜感激。 尼尔。

0 个答案:

没有答案