[PyQt] QTcpSocket connections

Phil Thompson phil at riverbankcomputing.com
Tue Jul 23 06:43:58 BST 2013


On Tue, 23 Jul 2013 00:20:48 +0200, Jorge Tornero <jtorlistas at gmail.com>
wrote:
> Hello again, and thanks for your answer:
> 
> Well I tried what you suggested (I guess) but got no luck. I know that I
am
> able to send the data but can't receive anything... seems that readAll
or
> read....() don't work at all
> 
> I've drop some lines on pastebin
> 
> http://pastebin.com/mpYC0pPt
> 
> with an tiny application where the problem is shown
> 
> I hope someone can put some light on this... Thank you very much

readAll() returns all data that has been read from the device since the
last read. It won't wait for data to arrive because it doesn't know how
much is expected. Use the readyRead() signal.

Phil


> 2013/7/22 Phil Thompson <phil at riverbankcomputing.com>
> 
>> On Mon, 22 Jul 2013 16:31:55 +0200, Jorge Tornero - Listas
>> <jtorlistas at gmail.com> wrote:
>> > Hello everybody,
>> >
>> > I'm trying to share a digital scale through the network, making it
able
>> > to receive tare/send weight commands.
>> > The scale is connected to a remote computer (a raspberry pi, by the
>> > way)
>>
>> > with a serial converter an the raspi is executing remserial, thus
makin
>> > avalilable the serial port to the network through port 23000.
>> >
>> > Because I am making my application with PyQt4, I have chosen to use
>> > QTcpSocket to do the communications between the computers. It is
pretty
>> > easy and I am able to get what i want (sending command and receiving
>> > weights) just by:
>> >
>> > soc=QtNetwork.QTcpSocket()
>> >
>> >
>> > soc.connectToHost('174.33.22.11',23000)
>> >
>> > soc.writeData('01ST@')
>> > print soc.readAll()
>> > soc.disconnectFromHost()
>> >
>> >
>> > And of course I get the weight and I am able to repeat the cycle
>> > connect-poll-receive-disonnect
>> >
>> > The problem is whe I put that into a function like:
>> >
>> > def captura():
>> >
>> >    soc=QtNetwork.QTcpSocket(app)
>> >    comandoCaptura=chr(6)+'01S@'+chr(13)
>> >    soc.connectToHost('172.23.2.25',23000)
>> >    soc.writeData(comandoCaptura)
>> >    a=soc.readAll()
>> >    print a
>> >    soc.disconnectFromHost()
>> >    return a
>> >
>> >
>> > Ther is no wai to make it works. I guess is a basic python issue, but
>> > can anyone help?
>>
>> QTcpSocket is asynchronous, so you need an event loop. After calling
>> connectToHost() you shouldn't try to read or write until the
connected()
>> signal has been emitted.
>>
>> Phil
>>


More information about the PyQt mailing list