SHOW:
|
|
- or go back to the newest paste.
| 1 | - | import sys |
| 1 | + | #!/usr/bin/env python |
| 2 | # Example of working with QTcpSocket and remserial | |
| 3 | # By Jorge Tornero | |
| 4 | - | class test(QtGui.QWidget): |
| 4 | + | # http://imasdemase.com |
| 5 | - | def __init__(self,parent=None): |
| 5 | + | import sys,time |
| 6 | - | QtGui.QWidget.__init__(self,parent) |
| 6 | + | |
| 7 | - | self.layout=QtGui.QVBoxLayout(self) |
| 7 | + | |
| 8 | - | self.toSent=QtGui.QLineEdit(self) |
| 8 | + | class test(QtGui.QDialog): |
| 9 | - | self.received=QtGui.QLineEdit(self) |
| 9 | + | |
| 10 | - | self.button=QtGui.QPushButton('SEND')
|
| 10 | + | def __init__(self, parent = None): |
| 11 | - | self.layout.addWidget(self.toSent) |
| 11 | + | |
| 12 | QtGui.QWidget.__init__(self, parent) | |
| 13 | ||
| 14 | self.layout = QtGui.QVBoxLayout(self) | |
| 15 | self.received = QtGui.QLineEdit(self) | |
| 16 | self.button = QtGui.QPushButton('SEND')
| |
| 17 | self.layout.addWidget(self.received) | |
| 18 | - | self.soc.connected.connect(self.sending) |
| 18 | + | |
| 19 | - | self.soc.bytesWritten.connect(self.reading) |
| 19 | + | |
| 20 | self.capturedWeight = QtCore.QString() | |
| 21 | ||
| 22 | self.soc=QtNetwork.QTcpSocket(self) | |
| 23 | ||
| 24 | self.button.clicked.connect(self.connecting) | |
| 25 | - | self.soc.connectToHost('192.168.1.74',23000)
|
| 25 | + | self.soc.connected.connect(self.requestWeight) |
| 26 | self.soc.readyRead.connect(self.reading) | |
| 27 | - | def sending(self): |
| 27 | + | |
| 28 | - | payload=self.toSent.text() |
| 28 | + | self.getWeightCommand = chr(6)+'01S@'+chr(13) |
| 29 | - | payload.append(chr(13)) |
| 29 | + | |
| 30 | - | print self.soc.writeData(payload) |
| 30 | + | |
| 31 | ||
| 32 | def connecting(self): | |
| 33 | ||
| 34 | self.soc.connectToHost('172.23.2.25', 23000)
| |
| 35 | - | a=str(self.soc.readAll()) |
| 35 | + | |
| 36 | - | print 'READ:',a |
| 36 | + | |
| 37 | - | self.received.setText(a) |
| 37 | + | def requestWeight(self): |
| 38 | ||
| 39 | print self.soc.state() | |
| 40 | print self.soc.writeData(self.getWeightCommand) | |
| 41 | ||
| 42 | - | app=QtGui.QApplication(sys.argv) |
| 42 | + | |
| 43 | - | ins=test() |
| 43 | + | |
| 44 | - | sys.exit(app.exec_()) |
| 44 | + | print '--READING-' |
| 45 | c = self.soc.read(1) | |
| 46 | print c, ord(c) | |
| 47 | if c != chr(13): | |
| 48 | self.capturedWeight.append(c) | |
| 49 | return | |
| 50 | ||
| 51 | ||
| 52 | print 'FINAL->', self.capturedWeight | |
| 53 | ||
| 54 | self.received.setText(self.capturedWeight.split(';')[0])
| |
| 55 | self.soc.disconnectFromHost() | |
| 56 | self.capturedWeight.clear() | |
| 57 | ||
| 58 | if __name__ == "__main__": | |
| 59 | app = QtGui.QApplication(sys.argv) | |
| 60 | ins = test() | |
| 61 | sys.exit(ins.exec_()) |