Skip to content

Commit

Permalink
Bugfix: OemGatewaySocketListener fails when receiving two frames
Browse files Browse the repository at this point in the history
The socket listener used to fail if receiving a second data frame before
processing the first.
  • Loading branch information
Jérôme Lafréchoux committed Jul 17, 2013
1 parent 924eb10 commit f96d0d5
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions oemgatewaylistener.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import serial
import time, datetime
import logging
import re
import socket, select

"""class OemGatewayListener
Expand Down Expand Up @@ -110,13 +109,13 @@ def read(self):
self._rx_buf = self._rx_buf + self._ser.readline()

# If line incomplete, exit
if (self._rx_buf == '') or (self._rx_buf[len(self._rx_buf)-1] != '\n'):
if '\r\n' not in self._rx_buf:
return

# Otherwise, process line:

# Remove CR,LF
self._rx_buf = re.sub('\\r\\n', '', self._rx_buf)
self._rx_buf = self._rx_buf[:-2]

# Log data
self._log.info("Serial RX: " + self._rx_buf)
Expand Down Expand Up @@ -271,36 +270,30 @@ def read(self):
ready_to_read, ready_to_write, in_error = \
select.select([self._socket], [], [], 0)

# If nothing received, return
if self._socket not in ready_to_read:
return
# If data received, add it to RX buffer
if self._socket in ready_to_read:

# Otherwise, accept connection
conn, addr = self._socket.accept()

# Read data
self._rx_buf = self._rx_buf + conn.recv(1024)

# Close connection
conn.close()
# Accept connection
conn, addr = self._socket.accept()
# Read data
self._rx_buf = self._rx_buf + conn.recv(1024)
# Close connection
conn.close()

# If line incomplete, exit
if (self._rx_buf == '') or (self._rx_buf[len(self._rx_buf)-1] != '\n'):
# If no complete frame, exit
if '\r\n' not in self._rx_buf:
return

# Otherwise, process line:
# Otherwise, process first frame in buffer:
f, self._rx_buf = self._rx_buf.split('\r\n', 1)

# Remove CR,LF
self._rx_buf = re.sub('\\r\\n', '', self._rx_buf)

# Log data
self._log.info("Serial RX: " + self._rx_buf)
self._log.info("Serial RX: " + f)

# Get an array out of the space separated string
received = self._rx_buf.strip().split(' ')

# Empty serial_rx_buf
self._rx_buf = ''
received = f.strip().split(' ')

# Discard if frame not of the form [node val1 ...]
# with number of elements at least 2
Expand Down

0 comments on commit f96d0d5

Please sign in to comment.