Serial communication read line C++

638 views Asked by At

I want to read a line from serial with Symbian C++ but to do that I have to continue reading into a buffer until I reach "/n". I have no idea how to do this though. This is actually a Python project but I am trying to create a C++ library with with the neccesary functions ported from PySerial. Al I still have to port is "readline though.

Here is the original Python code for it:

def readline(self, size=None, eol=LF): 
     """read a line which is terminated with end-of-line (eol) character
     ('\n' by default) or until timeout."""
     leneol = len(eol)
     line = bytearray()

     while True: 
         c = self.read(1)
         if c: 
             line += c 
             if line[-leneol:] == eol: 
                 break 
             if size is not None and len(line) >= size: 
                 break 
         else: 
             break
     return bytes(line)

My library is based on PyS60USB which has already implemented the following to read at least one byte:

TUint8* buf = 0;
TPtr8 tmp(buf, 0);
TInt err;
TBool alloc_ok = EFalse;

Py_BEGIN_ALLOW_THREADS;         
TRAP(err,
   buf = (TUint8*) User::AllocLC(1);
   tmp.Set(buf, 0, 1);
   alloc_ok = ETrue;
   self->iConnManager->RecvL(tmp, timeout);
   CleanupStack::Pop(buf);
   );
Py_END_ALLOW_THREADS;

My question though is now, how can I translate the afore mentioned Python code to Symbian C++ code that can add all the read bytes to a buffer and continously check the buffer for eol?

Thanks, all help will be greatly appreciated.

0

There are 0 answers