I am working with Pyserial and have a question regarding best practices surrounding flushing the input and output buffers. Currently I reset the output buffer before sending a command and reset the input before reading the reply. It seems I occasionally miss the start of a reply so I am considering modifying my program to reset the input buffer before I send my command. I am also considering linking my send and receive functions so that the send always calls the receive and hopefully tightens up the loop.
def send_cmd(self, cmd_to_send):
self.ser.reset_output_buffer()
self.ser.write(cmd_to_send)
def receive_cmd(self):
self.ser.reset_input_buffer()
# Read logic below
Considering transitioning to something like this
def send_cmd(self, cmd_to_send):
self.ser.reset_output_buffer()
self.ser.reset_input_buffer()
self.ser.write(cmd_to_send)
self.receive_cmd()
def receive_cmd(self):
# Read logic below
In the port configuration, the key timeout let you wait some time in each communication with the port.
I usually use this parameter to prevent problems with the port buffer. Try this and maybe it is unnecessary to flush.
This post gets decent traffic so I just want to let everyone know I never found the answer to this question. I tried a variety of configurations but ultimately flushing seems to rely more on the hardware itself than the software. I did end up transitioning to the code I mentioned and it seemed to help, albeit slightly. So I would recommend something like this:
def send_cmd(self, cmd_to_send):
self.ser.reset_output_buffer()
self.ser.reset_input_buffer()
self.ser.write(cmd_to_send)
self.receive_cmd()
def receive_cmd(self):
# Read logic below
If anyone can supply a better answer, I would still appreciate it :)
Edit: I also was reading bytes individually, as I needed to. You may have more success using readlines() as Eduardo mentioned in the other answer.
def send_cmd(self, cmd_to_send):
self.ser.reset_output_buffer()
self.ser.reset_input_buffer()
self.ser.time.sleep(0.1)
self.ser.write(cmd_to_send)
self.ser.time.sleep(0.1)
self.receive_cmd()
def receive_cmd(self):
# Read logic below
Related
I am trying to simulate a communication protocol where I am following a pattern, so I constantly loop though looking for the same set of characters to reply information. I'm using an RS-232 adapter and the protocol I am simulating is asynchronous and half-duplex where the rx/tx lines are tied together by design and that causes a sort of echo when reading after writing.
That said, I need to be able to clear the input buffer after every write I send out in order to avoid reading what I just wrote. So whenever I use reset_input_buffer() it does not clear the last message I sent out. I have tried to fix this using a couple of methods, such as: using reset_output_buffer() together with reset_input_buffer(), using reset_input_buffer() twice, and using flush(). None of these methods make any difference, the only other method that works to clear the buffer is closing and immediately opening the port but this causes a delay that messes with the timing as it is critical at certain times.
I'm open to any suggestions, please help!
I was wondering if there is a way I can tell python to wait until it gets a response from a server to continue running.
I am writing a turn based game. I make the first move and it sends the move to the server and then the server to the other computer. The problem comes here. As it is no longer my turn I want my game to wait until it gets a response from the server (wait until the other player makes a move). But my line:
data=self.sock.recv(1024)
hangs because (I think) it's no getting something immediately. So I want know how can I make it wait for something to happen and then keep going.
Thanks in advance.
The socket programming howto is relevant to this question, specifically this part:
Now we come to the major stumbling block of sockets - send and recv operate on the
network buffers. They do not necessarily handle all the bytes you hand them (or expect
from them), because their major focus is handling the network buffers. In general, they
return when the associated network buffers have been filled (send) or emptied (recv).
They then tell you how many bytes they handled. It is your responsibility to call them
again until your message has been completely dealt with.
...
One complication to be aware of: if your conversational protocol allows multiple
messages to be sent back to back (without some kind of reply), and you pass recv an
arbitrary chunk size, you may end up reading the start of a following message. You’ll
need to put that aside >and hold onto it, until it’s needed.
Prefixing the message with it’s length (say, as 5 numeric characters) gets more complex,
because (believe it or not), you may not get all 5 characters in one recv. In playing
around, you’ll get away with it; but in high network loads, your code will very quickly
break unless you use two recv loops - the first to determine the length, the second to
get the data part of the message. Nasty. This is also when you’ll discover that send
does not always manage to get rid of everything in one pass. And despite having read
this, you will eventually get bit by it!
The main takeaways from this are:
you'll need to establish either a FIXED message size, OR you'll need to send the the size of the message at the beginning of the message
when calling socket.recv, pass number of bytes you actually want (and I'm guessing you don't actually want 1024 bytes). Then use LOOPs because you are not guaranteed to get all you want in a single call.
That line, sock.recv(1024), blocks until 1024 bytes have been received or the OS detects a socket error. You need some way to know the message size -- this is why HTTP messages include the Content-Length.
You can set a timeout with socket.settimeout to abort reading entirely if the expected number of bytes doesn't arrive before a timeout.
You can also explore Python's non-blocking sockets using setblocking(0).
I have to filter and modify network traffic using Linux kernel libnetfilter_queue (precisely the python binding) and dpkt, and i'm trying to implement delayed packet forward.
Normal filtering works really well, but if i try to delay packets with function like this
def setVerdict(pkt, nf_payload):
nf_payload.set_verdict_modified(nfqueue.NF_ACCEPT, str(pkt), len(pkt))
t = threading.Timer(10, setVerdict, [pkt, nf_payload])
t.start()
It crashs throwing no exception (surely is a low level crash). Can i implement delay using directly libnetfilter like this or I must copy pkt, drop it and send the copy using standard socket.socket.send()?
Thank you
Sorry for the late reply, but I needed to do something like this, although slightly more complicated. I used the C-version of the library and I copied packets to a buffer inside my program, and then issued a DROP verdict. After a timeout relating to your delay, I reinject the packet using a raw socket. This works fine, and seems quite efficient.
I think the reason for your crash was due to the fact that you didnt issue a verdict fast enough.
I can't answer your question, but why not use the "netem" traffic-queue module on the outgoing interface to delay the packet?
It is possible to configure tc queues to apply different policies to packets which are "marked" in some way; the normal way to mark such packets is with a netfilter module (e.g. iptables or nfqueue).
I'm trying to implement a threading functionality for this answer :
Scanning a Class C network Python
So far i have something like this:
...[snip]..
m = re.search("/", str(host))
if m :
net,_,mask = host.partition('/')
mask = int(mask)
net = atod(net)
for host in (dtoa(net+n) for n in range(0, 1<<32-mask)):
try:
mycustomsocket(host)
except:
print host+" is down"
pass
else:
mycustomsocket(host)
What I'm looking for, would be to open 255 thread to scan all hosts parsed with mycustomsocket() at once, for speed issues.
Any help would be greatly appreciated !
I think he did give you the answer, go and read the docs and then come back when you have specific questions on implementing the threading code... If you read the article on devshed already mentioned you can see how you create your own thread class and pass the ip address you want to work with into the thread and put your working code there with some sort of threadsafe queue where the thread can put back whatever information you are after.
I once wrote a multi-threaded port scanner. Feel free to use it for some ideas on improving performance. Over time, it has been improved and refactored such that it doesn't provide a concise example, but instead implements a more robust implementation with re-usable components. I hope the core ideas aren't masked by the abstraction.
This question is not very specific. It sounds like: "I need threading support for my code, please do the work for me."
Please read the docs about threading in Python and related topics like the Queue class. If you have a more specifc question, come back and ask again.
I'm writing some python and are stuck at the moment.
I think this "Nagle algoritm" is the problem since my packages are delayed some time for some reason to the client.
I've tried this on both client and server but it doesn't seems to work (or there's another problem causing it):
socketobj.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
Any ideas?
EDIT: A full explanation of my problem can be found here:
http://www.gamedev.net/community/forums/topic.asp?topic_id=554172&whichpage=1�
I'm not familiar with Python's sockets, but does it have a flush method? Even with Nagle's disabled, most socket implementations will buffer if you don't write X number of bytes. However, if you call flush, the bytes should be sent immediately.