[Twisted-Python] Serial port communication
Hi, I'm trying to send some commands to a modem connected to the serial port, in a sequential way. I can send the first command to the modem but I must wait for the whole response before sending the next command. I haven't got much experience with Twisted so I'm not sure about the best protocol I can use for this. At this moment I'm trying with SerialPort (from twisted.internet.serialport) as my factory, and LineReceiver (from twisted.protocols.basic) as its protocol. I can write to the serial port with the SerialPort's method writeSomeData and it works fine, but the modem returns the response byte to byte, so the dataReceived method (in the LineReceiver protocol) is launched with every byte received, so I have to store the bytes in a string so I can find the end of the response and then I can see the whole response. So I can get the whole response from the modem, but in that moment I don't know how to tell the factory that it can send another writeSomeData (remember that it must be done in a sequential way). So the question is: how can I tell the factory that it can run another writeSomeData once I got the right response from the modem? I know that the main problem is my inexperience with Twisted, so I'm sure there's a better way to implement this. Maybe a more appropiate protocol? Any clue will be very welcome ;-) Thanks in advance, Daniel Marfil
Daniel Marfil wrote:
So I can get the whole response from the modem, but in that moment I don't know how to tell the factory that it can send another writeSomeData (remember that it must be done in a sequential way). So the question is: how can I tell the factory that it can run another writeSomeData once I got the right response from the modem?
I know that the main problem is my inexperience with Twisted, so I'm sure there's a better way to implement this. Maybe a more appropiate protocol?
Any clue will be very welcome ;-)
You will have to have some knowledge of the protocol used by the modem - typically Hayes AT (http://www-dcg.fnal.gov/Net/HYSTRM20.TXT) and then look out for the termination characters (; <CR>) before you call write() again. You will probably want to implement a time out using reactor.callLater() together with a state machine. regards, Eugene Coetzee -- -- =============================================== Reedflute Software Solutions Telephone -> +27 18 293 3236 General information -> info@reedflute.com Project information -> projects@reedflute.com Web -> www.reedflute.com ===============================================
participants (3)
-
Daniel Marfil -
Eugene Coetzee -
Itamar Shtull-Trauring