XM540-W270-R with MAX485 and UART

Hi,

I am using 6 Dynamixel XM540-W270-R series servos with a custom designed PCB, controlled by a Raspberry Pi CM5. I integrated the MAX485 chip as laid out here: XM540-W270-T/R . Under the oscilloscope everything seems to work fine however I am unsure about how to control the servos via ROS2 with this set up.

With a U2D2 my code seems to work perfectly fine but I do not know how to adjust the software so that it will work with UART, the RPCM5 and the MAX485 chip.
The exact pinlayout is: RX → GPIO5 ; TX → GPIO4 ; TX_enable → GPIO17 .

Right now (via USB and U2D2) my software looks approximately like this:

// Control table address for X series
#define ADDR_GOAL_POSITION 116
#define ADDR_PRESENT_POSITION 132
// Protocol version
#define PROTOCOL_VERSION 2.0
// Default setting
#define BAUDRATE 57600
#define DEVICE_NAME “/dev/ttyUSB0”
portHandler = dynamixel::PortHandler::getPortHandler(DEVICE_NAME);

packetHandler = dynamixel::PacketHandler::getPacketHandler(PROTOCOL_VERSION);

dxl_comm_result = portHandler->openPort();
dxl_comm_result = portHandler->setBaudRate(BAUDRATE);

dxl_comm_result = packetHandler->read4ByteTxRx(portHandler, id, ADDR_PRESENT_POSITION, &present_position, &dxl_error);

dxl_comm_result = packetHandler->write4ByteTxRx(
portHandler, id, ADDR_GOAL_POSITION, goal_position, &dxl_error);

With the U2D2 this works perfectly fine and I hope someone can help me to adjust this so that it will work with the CM5 and MAX485 chip.