mrpt.comms
mrpt-comms Python API — TCP sockets and RS-232 / USB serial ports.
- Classes:
CClientTCPSocket: connect to a TCP server, send/receive bytes
CSerialPort: open and configure a serial port (RS-232 / FTDI / USB-serial)
Both classes support Python context managers (with statement).
Example — TCP:
from mrpt.comms import CClientTCPSocket
with CClientTCPSocket() as sock:
sock.connect("127.0.0.1", 9000, timeout_ms=3000)
sock.write(b"hello")
data = sock.read(1024)
Example — Serial:
from mrpt.comms import CSerialPort
with CSerialPort("/dev/ttyUSB0", openNow=True) as port:
port.setConfig(baudRate=115200)
port.write(b"AT\r")
response = port.read(64)
Package Contents
- class mrpt.comms.CClientTCPSocket
C++ API: class mrpt::comms::CClientTCPSocket
Bases:
mrpt.io.CStreamA TCP socket that can be connected to a TCP server, implementing MRPT’s CStream interface for passing objects as well as generic read/write methods.
- close() None
Closes the connection.
- connect(remotePartAddress: str, remotePartTCPPort: int, timeout_ms: int = 0) None
Establish a TCP connection to host:port. timeout_ms=0 means no timeout.
- isConnected() bool
Returns true if this objects represents a successfully connected socket.
- read(count: int, timeout_ms: int = -1) bytes
Read up to count bytes from the socket, returns bytes object
- sendString(str: CClientTCPSocket.sendString.str) None
Send a std::string over the TCP connection
- write(data: bytes, timeout_ms: int = -1) int
Write bytes to the socket, returns number of bytes written
- class mrpt.comms.CSerialPort
C++ API: class mrpt::comms::CSerialPort
CSerialPort(portName: str, openNow: bool = True)
Bases:
mrpt.io.CStreamA communications serial port implementing the interface mrpt::io::CStream.
- close() None
Close the port. If is already closed, results in no action.
- isOpen() bool
Returns if port has been correctly open.
- open() None
- open(COM_name: str) None
Open the named port
- purgeBuffers() None
Purge tx and rx buffers.
- read(count: int) bytes
Read up to count bytes from the serial port, returns bytes object
- setConfig(baudRate: int, parity: int = 0, bits: int = 8, nStopBits: int = 1, enableFlowControl: bool = False) None
Configure baud rate and framing (parity: 0=none, 1=odd, 2=even)
- setSerialPortName(portName: str) None
Set the serial port name (e.g. ‘/dev/ttyUSB0’ or ‘COM3’)
- setTimeouts(ReadIntervalTimeout: int, ReadTotalTimeoutMultiplier: int, ReadTotalTimeoutConstant: int, WriteTotalTimeoutMultiplier: int, WriteTotalTimeoutConstant: int) None
Set read/write timeouts in milliseconds
- write(data: bytes) int
Write bytes to the serial port, returns number of bytes written