[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyQt] QTcpSocket connections
From:       Jorge Tornero - Listas <jtorlistas () gmail ! com>
Date:       2013-07-23 10:24:39
Message-ID: 51EE673D.40903 () gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Hello all,

Thank you for your answers.

Finally I've managed to make it work, but a strange behavior (at least 
for newbies like me) arose. When I use readyRead signal, I can't get 
more than a single byte to read.

Finally I've had to do this: Make a sort of loop in reading() function 
to read one by one and append to a string. I don't get any results with 
readline and friends, I guess because the device I am communicating with 
uses \r instead of \n as line end. So read(1) or readAll() do the trick.

http://pastebin.com/zQ2iFRie


Again, thank you very much for your help

Jorge Tornero




And it works. Maybe the problem is related to the fact that my scale 
uses just \r (chr 13) as line ending character, so readLine and folks 
don't work as expected.


El 23/07/13 07:43, Phil Thompson escribió:
> On Tue, 23 Jul 2013 00:20:48 +0200, Jorge Tornero <jtorlistas@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@riverbankcomputing.com>
>>
>>> On Mon, 22 Jul 2013 16:31:55 +0200, Jorge Tornero - Listas
>>> <jtorlistas@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
>>>


[Attachment #5 (text/html)]

<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">Hello all,<br>
      <br>
      Thank you for your answers.<br>
      <br>
      Finally I've managed to make it work, but a strange behavior (at
      least for newbies like me) arose. When I use readyRead signal, I
      can't get more than a single byte to read.<br>
      <br>
      Finally I've had to do this: Make a sort of loop in reading()
      function to read one by one and append to a string. I don't get
      any results with readline and friends, I guess because the device
      I am communicating with uses \r instead of \n as line end. So
      read(1) or readAll() do the trick.<br>
      <br>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <a href="http://pastebin.com/zQ2iFRie">http://pastebin.com/zQ2iFRie</a><br>
      <br>
      <br>
      Again, thank you very much for your help<br>
      <br>
      Jorge Tornero<br>
      <br>
      <br>
      <br>
      <br>
      And it works. Maybe the problem is related to the fact that my
      scale uses just \r (chr 13) as line ending character, so readLine
      and folks don't work as expected.<br>
      <br>
      <br>
      El 23/07/13 07:43, Phil Thompson escribió:<br>
    </div>
    <blockquote cite="mid:cae164d932df60f471f2a71a170e99b2@localhost"
      type="cite">
      <pre wrap="">On Tue, 23 Jul 2013 00:20:48 +0200, Jorge Tornero <a \
class="moz-txt-link-rfc2396E" \
href="mailto:jtorlistas@gmail.com">&lt;jtorlistas@gmail.com&gt;</a> wrote:
</pre>
      <blockquote type="cite">
        <pre wrap="">Hello again, and thanks for your answer:

Well I tried what you suggested (I guess) but got no luck. I know that I
</pre>
      </blockquote>
      <pre wrap="">am
</pre>
      <blockquote type="cite">
        <pre wrap="">able to send the data but can't receive anything... seems that \
readAll </pre>
      </blockquote>
      <pre wrap="">or
</pre>
      <blockquote type="cite">
        <pre wrap="">read....() don't work at all

I've drop some lines on pastebin
<a class="moz-txt-link-freetext" \
href="http://pastebin.com/mpYC0pPt">http://pastebin.com/mpYC0pPt</a><pre wrap=""> \
</pre>

with an tiny application where the problem is shown

I hope someone can put some light on this... Thank you very much
</pre>
      </blockquote>
      <pre wrap="">
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


</pre>
      <blockquote type="cite">
        <pre wrap="">2013/7/22 Phil Thompson <a class="moz-txt-link-rfc2396E" \
href="mailto:phil@riverbankcomputing.com">&lt;phil@riverbankcomputing.com&gt;</a>

</pre>
        <blockquote type="cite">
          <pre wrap="">On Mon, 22 Jul 2013 16:31:55 +0200, Jorge Tornero - Listas
<a class="moz-txt-link-rfc2396E" \
href="mailto:jtorlistas@gmail.com">&lt;jtorlistas@gmail.com&gt;</a> wrote: </pre>
          <blockquote type="cite">
            <pre wrap="">Hello everybody,

I'm trying to share a digital scale through the network, making it
</pre>
          </blockquote>
        </blockquote>
      </blockquote>
      <pre wrap="">able
</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <blockquote type="cite">
            <pre wrap="">to receive tare/send weight commands.
The scale is connected to a remote computer (a raspberry pi, by the
way)
</pre>
          </blockquote>
          <pre wrap="">
</pre>
          <blockquote type="cite">
            <pre wrap="">with a serial converter an the raspi is executing remserial, \
thus </pre>
          </blockquote>
        </blockquote>
      </blockquote>
      <pre wrap="">makin
</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <blockquote type="cite">
            <pre wrap="">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
</pre>
          </blockquote>
        </blockquote>
      </blockquote>
      <pre wrap="">pretty
</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <blockquote type="cite">
            <pre wrap="">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?
</pre>
          </blockquote>
          <pre wrap="">
QTcpSocket is asynchronous, so you need an event loop. After calling
connectToHost() you shouldn't try to read or write until the
</pre>
        </blockquote>
      </blockquote>
      <pre wrap="">connected()
</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <pre wrap="">signal has been emitted.

Phil

</pre>
        </blockquote>
      </blockquote>
    </blockquote>
    <br>
  </body>
</html>



_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic