【问题标题】:How do I watch a serial port with QSocketNotifier (linux)?如何使用 QSocketNotifier (linux) 观看串口?
【发布时间】:2009-04-25 16:49:45
【问题描述】:

有人可以给我一个示例,说明如何设置 QSocketNotifier 以在 /dev/ttyS0 发生某些事情时触发事件? (最好在 python/pyqt4 中)

【问题讨论】:

    标签: python qt serial-port pyqt4


    【解决方案1】:

    这是一个使用 QSocketNotifier 不断从文件中读取的示例。只需将 'foo.txt' 替换为 '/dev/ttyS0' 就可以了。

    
    import os
    
    from PyQt4.QtCore import QCoreApplication, QSocketNotifier, SIGNAL
    
    
    def readAllData(fd):
            bufferSize = 1024
            while True:
                    data = os.read(fd, bufferSize)
                    if not data:
                            break
                    print 'data read:'
                    print repr(data)
    
    
    a = QCoreApplication([])
    
    fd = os.open('foo.txt', os.O_RDONLY)
    notifier = QSocketNotifier(fd, QSocketNotifier.Read)
    a.connect(notifier, SIGNAL('activated(int)'), readAllData)
    
    a.exec_()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多