多字符串值的python正则表达式

2024-05-13 18:31:40 发布

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

所以我的问题是,我从一个串行端口接收值。 这些值可以是任何一个流。你知道吗

底座半径:新,ATC001#T1412010472R-77,ATC005:T1412010460R-70,SU0003;Q6V8.9S0C11.5*xx 底座半径:新,ATC001#T1413824282R-102,ATC003:T1413824274R-98,SU001G;Q0V14.0D00*x

它们的输出变化很小,但最大的区别是第二行的值是D00而不是S0

因此,这个串行输出将更新传感器的变化我和D00是数字输出,但S0是风扇转速。你知道吗

所以我的问题是我已经写了一个正则表达式,如果我收到第一个串行输出,它有S0值,但是如果我收到D00,正则表达式将中断。你知道吗

我想能够写它,所以如果它没有S0值,它会寻找D00值代替。你知道吗

感谢您的帮助或建议。我不知道我该往哪里看,也不知道该往哪个方向走。你知道吗

下面的代码检查串行输出,然后运行正则表达式,如果找到匹配项,则将其插入数据库。你知道吗

下面的代码是PYTHON

导入serial,string,MySQLdb,re

db = MySQLdb.connect(host="localhost", user="root", passwd="", db="walnut_farm")
cur = db.cursor() 

serialPort = 'COM4' # BAUD Rate is 9600 as default
ser = serial.Serial()
ser.setPort(serialPort)
#ser.setBaudrate(115200) Enable if BAUD is not deault value 

try:
    ser.open()
except: 
    print('Port Error!')

else:
    while True:
        try:
            ardString = ser.readline()
            Serial_Output = ardString

            p = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)S(\d*)C(\d*\.?\d*)(.*)') # here is the regular expressions i created from this link http://regex101.com/r/dP6fE1/1

            Serial_Results = re.match(p, Serial_Output)


            # Assigning variables to the array values
            Base_ID = Serial_Results.group(1)
            Base_Time_Stamp = Serial_Results.group(2)
            Base_Signal = Serial_Results.group(3)
            Repeater_ID = Serial_Results.group(4)
            Repeater_Time_Stamp = Serial_Results.group(5)
            Repeater_Signal = Serial_Results.group(6)
            Sensor_ID = Serial_Results.group(7)
            Sensor_Sequence = Serial_Results.group(8)
            Sensor_Input_Voltage = Serial_Results.group(9)
            Sensor_Wind_Speed = Serial_Results.group(10)
            Sensor_Temperature = Serial_Results.group(11)
            Checksum = Serial_Results.group(12)

            # Execute the SQL query to INSERT the above variables into the Database
            cur.execute('INSERT INTO serial_values (Base_ID, Base_Time_Stamp, Base_Signal, Repeater_ID, Repeater_Time_Stamp, Repeater_Signal, Sensor_ID, Sensor_Sequence,  Sensor_Input_Voltage, Sensor_Wind_Speed, Sensor_Temperature, Checksum) VALUES ("'+Base_ID+'", "'+Base_Time_Stamp+'", "'+Base_Signal+'", "'+Repeater_ID+'", "'+Repeater_Time_Stamp+'", "'+Repeater_Signal+'", "'+Sensor_ID+'", "'+Sensor_Sequence+'",  "'+Sensor_Input_Voltage+'", "'+Sensor_Wind_Speed+'", "'+Sensor_Temperature+'", "'+Checksum+'")')

            db.commit()
            #ser.close()

        except Exception:
            pass

Tags: theiddbbasesignaltimestampserial
1条回答
网友
1楼 · 发布于 2024-05-13 18:31:40

看看这个,如果我的解释正确的话。这是一个起点,然后必须将mysql插入到数据库中。你知道吗

import re

def get_output_parameters(serial_output):
    p = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)S(\d*)C(\d*\.?\d*)(.*)') # here is the regular expressions i created from this link http://regex101.com/r/dP6fE1/1
    p2 = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)D(\d*)(.*)')

    Serial_Results = re.match(p, serial_output)
    digital_out = False
    if not Serial_Results:
        Serial_Results = re.match(p2, serial_output)
        digital_out = True

    # Assigning variables to the array values
    Base_ID = Serial_Results.group(1)
    Base_Time_Stamp = Serial_Results.group(2)
    Base_Signal = Serial_Results.group(3)
    Repeater_ID = Serial_Results.group(4)
    Repeater_Time_Stamp = Serial_Results.group(5)
    Repeater_Signal = Serial_Results.group(6)
    Sensor_ID = Serial_Results.group(7)
    Sensor_Sequence = Serial_Results.group(8)
    Sensor_Input_Voltage = Serial_Results.group(9)
    Sensor_Wind_Speed = Serial_Results.group(10)
    Sensor_Temperature = Serial_Results.group(11)
    if not digital_out:
        Checksum = Serial_Results.group(12)

    print Sensor_Temperature


Serial_Output = "BASE_RAD: NEW,ATC001#T1412010472R-77,ATC005:T1412010460R-70,SU0003;Q6V8.9S0C11.5*xx"
Serial_Output2 = "BASE_RAD: NEW,ATC001#T1413824282R-102,ATC003:T1413824274R-98,SU001G;Q0V14.0D00*x"

get_output_parameters(Serial_Output)
get_output_parameters(Serial_Output2)

相关问题 更多 >