简体   繁体   English

无法打开串口,或使用pyserial从串口读取

[英]Having trouble opening serial port, or reading from serial port with pyserial

Background: I am trying to automate the rebooting of a server when PXE obtains an IP address. 背景:我正在尝试在PXE获取IP地址时自动重新启动服务器。 This is to reproduce an issue, and the only way to reproduce is to cold boot it at the exact same time every reboot. 这是为了重现一个问题,重现的唯一方法是在每次重启时完全同时冷启动它。 I'm tired of doing this manually and have spent probably 10 hours on this script and troubleshooting so far.. 我已经厌倦了手动执行此操作,并且已经花费了大约10个小时来编写此脚本并进行故障排除。

I am trying to read lines from a serial console on a server, while looking for a certain string, then issuing a reboot command. 我试图从服务器上的串行控制台读取行,同时查找某个字符串,然后发出重新启动命令。

Right now, the only way I can get this script to echo what is coming on the serial console is to power off the server, start minicom, power on the server, when text starts then I can exit minicom without a reset, then start my script. 现在,我可以让这个脚本回应串口控制台上的内容的唯一方法是关闭服务器电源,启动minicom,打开服务器电源,文本启动然后我可以退出minicom而不重置,然后启动我的脚本。

The first time through, the script works fine, and even the iLO commands at the end work, then it restarts the while loop, but then I don't get any output from the console anymore. 第一次,脚本工作正常,甚至最后的iLO命令工作,然后它重新启动while循环,但后来我不再从控制台获得任何输出。

It seems that I am either not opening the serial port correctly, but I print get_settings and the baudrate, stopbits, etc, are all correct. 似乎我要么没有正确打开串口,但我打印get_settings和波特率,停止位等都是正确的。

I have searched and used snippets of code from many different places to hack together this script, and am really getting frustrated that I can't get this to work on its own. 我搜索并使用了来自许多不同地方的代码片段来破解这个脚本,我真的很沮丧,因为我不能让它自己工作。

[root@localhost ~]# python2 bootorder.py 
{'parity': 'N', 'baudrate': 115200, 'bytesize': 8, 'xonxoff': False, 'rtscts': False, 'timeout': None, 'inter_byte_timeout': None, 'stopbits': 1, 'dsrdtr': False, 'write_timeout': None}

As you can see above, when I have it run, I have the serial port settings printed out, and they match minicom, and the serial console on the server side. 如上所示,当我运行它时,我打印出串口设置,它们与服务器端的minicom和串行控制台相匹配。

So what is minicom doing to open the port that I'm not doing in the script? 那么minicom正在做什么来打开我在脚本中没有做的端口呢? I have followed examples from many sites, and it does work at times, I just can't figure out how to make this work on its own. 我已经跟踪了许多网站的示例,并且它确实有效,我只是无法弄清楚如何独立完成这项工作。

Here is my script: 这是我的脚本:

#!/usr/bin/python

import io
import hpilo
import os
import sys
import time
import serial
from datetime import datetime

outfile='/tmp/bootordercount.txt'
# configure the serial connections (the parameters differs on the device you    are connecting to)
port = '/dev/ttyS0'
ser = serial.Serial(port,115200,timeout=None)
cycles = 1

if ser.isOpen(): ser.close()
ser.open()

if ser.isOpen():

    try:
#        ser.flushInput() #flush input buffer, discarding all its contents
#        ser.flushOutput()#flush output buffer, aborting current output 
                 #and discard all that is in buffer
    print(ser.get_settings())
    with open(outfile, 'a') as f:

       while ser.isOpen():
           line = ser.readline()
           print line
           if "CLIENT IP:" in line: 
                print "Client string seen!"
                ilo = hpilo.Ilo('10.0.8.203', 'administrator', 'password') #configure ilo function
                ilo.cold_boot_server() #cold boot the server
                print cycles
   #            f.write(datetime.utcnow().isoformat() + '\t' + cycles + '\n')
   #            f.flush()
                cycles += 1

    except Exception, e1:
        print "error communicating...: " + str(e1)
        ser.close()

Thanks for your input and help! 感谢您的投入和帮助!

It might save something to do with the other lines in the serial port: The DTR, DSR, etc. Their use is often inconsistent, and they are often used for other purposes than they are intended for. 它可能与串口中的其他行保存有关:DTR,DSR等。它们的使用通常不一致,并且它们通常用于其他目的而非预期用途。

Maybe minicom uses the DTR to initialize the connection. 也许minicom使用DTR初始化连接。 Try adding this after the serial open . 尝试在串行open后添加此项。

s.setDTR(False)
sleep(0.025)
s.setDTR(True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM