所以我问了一个关于从一个seial流运行多个正则表达式的问题。 python regular expression for multiple string values
现在我不得不转而从直接串行连接的Telnet流中获取数据
它有点工作,并且能够将结果发布到我的数据库,并使两个可用的正则表达式起作用。
问题在于它缺少部分,所以有时Telent Stream会输出7个新的更新行,我编写的代码会先接收并错过其余部分,看起来像挂在某物上或者它只是它无法处理Telnet流并足够快地发布到数据库。
到目前为止,这是代码。 (出于安全原因,我已经取出了telnet地址,但如果需要,将通过电子邮件提供)我正在使用python 2.7
import serial, string, MySQLdb, re, pdb, telnetlib
db = MySQLdb.connect(host="localhost", user="root", passwd="", db="walnut_farm")
cur = db.cursor()
HOST = "this is the ip or the telnet server i have removed"
PORT = "7001"
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*)(.*)') # Wind Speed and Temp
p2 = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)D(\d*)(.*)') # Digital Output
def Push_Results_To_Database():
# 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)
if not digital_out:
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()
if digital_out:
Sensor_Digital_Input = Serial_Results.group(10)
Checksum = Serial_Results.group(11)
# 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_Digital_Input, Checksum) VALUES ("'+Base_ID+'", "'+Base_Time_Stamp+'", "'+Base_Signal+'", "'+Repeater_ID+'", "'+Repeater_Time_Stamp+'", "'+Repeater_Signal+'", "'+Sensor_ID+'", "'+Sensor_Sequence+'", "'+Sensor_Input_Voltage+'", "'+Sensor_Digital_Input+'", "'+Checksum+'")')
db.commit()
try:
tn = telnetlib.Telnet(HOST,PORT)
except:
print('Port Error!')
else:
while True:
#pdb.set_trace()
tn.write('ls \r\n')
data = ''
while data.find('#') == -1:
data = tn.read_very_eager()
ardString = data
print data
Serial_Output = ardString
#pdb.set_trace()
#for Serial_Output in Serial_Output.splitlines():
if re.match(p, Serial_Output):
print "Match For Wind Speed and Temperature"
digital_out = False
Serial_Results = re.match(p, Serial_Output)
Push_Results_To_Database()
continue
elif re.match(p2, Serial_Output):
print "Match for Digital Output"
digital_out = True
Serial_Results = re.match(p2, Serial_Output)
Push_Results_To_Database()
continue
else:
continue
#[1] `ATC001` = Base_ID
#[2] `1412010472` = Base_Time_Stamp (EPOC TIME)
#[3] `77` = Base_Signal
#[4] `ATC005` = Repeater_ID
#[5] `1412010460` = Repeater_Time_Stamp
#[6] `70` = Repeater_Signal
#[7] `SU0003` = Sensor_ID
#[8] `6` = Sensor_Sequence
#[9] `8.9` = Sensor_Input_Voltage
#[10] `0` = Sensor_Wind_Speed
#[11] `11.5` = Sensor_Temperature
#[12] `*xx` = Checksum
# http://regex101.com/r/dP6fE1/1
以下是Telnet输出的示例。
BASE_RAD: ATC004 has no more samples
BASE_RAD: NEW,ATC001#T1414254671R-77,ATC003:T1414254671R-70,SU0014;Q6V13.9D00*xx
BASE_RAD: ATC005 has no more samples
BASE_RAD: NEW,ATC001#T1414254676R-103,ATC004:T1414254655R-70,SU0017;Q5V13.9D00*x
x
BASE_RAD: NEW,ATC001#T1414254691R-77,ATC003:T1414254688R-80,SU001D;Q6V13.1D00*xx
BASE_RAD: ATC005 has no more samples
BASE_RAD: NEW,ATC001#T1414254696R-103,ATC004:T1414254692R-70,SU0016;Q0V13.5D00*x
x
BASE_RAD: ATC003 has no more samples
BASE_RAD: ATC005 has no more samples
BASE_RAD: ATC004 has no more samples
BASE_RAD: NEW,ATC001#T1414254731R-77,ATC003:T1414254713R-72,SU0015;Q8V13.4D00*xx
BASE_RAD: ATC005 has no more samples
BASE_RAD: ATC004 has no more samples
BASE_RAD: NEW,ATC001#T1414254741R-77,ATC003:T1414254728R-71,SU001F;Q6V13.1D00*xx
BASE_RAD: ATC005 has no more samples
BASE_RAD: ATC004 has no more samples
BASE_RAD: NEW,ATC001#T1414254761R-77,ATC003:T1414254745R-70,SU001H;Q9V13.1D00*xx
BASE_RAD: ATC005 has no more samples
BASE_RAD: ATC004 has no more samples
BASE_RAD: NEW,ATC001#T1414254781R-77,ATC003:T1414254765R-72,SU0013;Q7V13.4D00*xx
BASE_RAD: NEW,ATC001#T1414254791R-77,ATC003:T1414254770R-70,SU001G;Q6V13.1D00*xx
BASE_RAD: ATC005 has no more samples
BASE_RAD: ATC004 has no more samples
BASE_RAD: ATC003 has no more samples
BASE_RAD: ATC005 has no more samples
BASE_RAD: NEW,ATC001#T1414254816R-103,ATC004:T1414254802R-70,SU0002;Q9V12.9S3C13
.0*xx
BASE_RAD: NEW,ATC001#T1414254831R-77,ATC003:T1414254830R-81,SU0019;Q7V13.6D00*xx
BASE_RAD: ATC005 has no more samples
BASE_RAD: ATC004 has no more samples
BASE_RAD: ATC003 has no more samples
BASE_RAD: NEW,ATC001#T1414254852R-93,ATC005:T1414254843R-70,SU001C;Q0V13.3D00*xx
BASE_RAD: ATC004 has no more samples
BASE_RAD: ATC003 has no more samples
BASE_RAD: ATC005 has no more samples
BASE_RAD: ATC004 has no more samples
BASE_RAD: ATC003 has no more samples
BASE_RAD: NEW,ATC001#T1414254892R-93,ATC005:T1414254880R-70,SU0003;Q3V13.2S5C11.
1*xx
BASE_RAD: NEW,ATC001#T1414254896R-103,ATC004:T1414254885R-70,SU0018;Q8V13.2D00*x
x
BASE_RAD: ATC003 has no more samples
BASE_RAD: ATC005 has no more samples
BASE_RAD: ATC004 has no more samples
所以只是想知道是否有人能够发现我可能出错的地方以及为什么会挂起/丢失telnet结果。
感谢您的任何建议或提示。
答案 0 :(得分:0)
因此,在发布此消息之后,我已经解决了我的问题,我会留下来,以防万一它可以帮助其他人。
以下是上面的代码块
tn.write('ls \r\n')
data = ''
while data.find('#') == -1:
data = tn.read_very_eager()
ardString = data
print data
Serial_Output = ardString
现在我在telnet读取行下面添加了tn.write('ls \ r \ n')。
data = tn.read_very_eager() tn.write('ls \ r \ n')
所以不是我需要返回一个新行,因为来自telnet会话的输出已经这样做但是它会停止读取telnet行,所以它不会挂起。
希望这有意义,并帮助一些人。 如果我犯了错误或需要类似的帮助,请给我发消息。