Modbus Rtu, how to assign UART

Hi, I want to use the SC20100 board as modbusRtu modbuMaster. I think that I have to assign a UART to be used for the transmission but don’t know how this has to be done.
Kind regards

This is working for me on the new boards to init. Obviously use the correct settings for your modBus devices. Also, the UARTs on the board are 3.3V. Usually Modbus uses RS-232 or RS-485 levels, so you need to adapt. Gus pointed my to the RS232 Clicks from Mikroe and they do the trick. I assume their RS485 click works equally as well

        UartController serialPort = UartController.FromName(SC20100.UartPort.Usart1);
        serialPort.SetActiveSettings(BaudRate, DataBits, parity, stopBits, UartHandshake.None);

You can use whatever UART/USART you want on the board. Then just either poll the port for data or use the DataReceived event. Call Send on the serialPort to send data.

1 Like

Thanks @Phil_C for your answer.
So I think that with code like this I’m on the right track

  using GHIElectronics.TinyCLR.Devices.Modbus;
  using GHIElectronics.TinyCLR.Devices.Modbus.Interface;
  using GHIElectronics.TinyCLR.Devices.Uart;

  private static void Main()
        UartController modbusUart = UartController.FromName(SC20100.UartPort.Usart1);
        // The following line had to be included to make it working
        modbusUart.SetActiveSettings(2400, 8, UartParity.None, UartStopBitCount.One,
        ModbusRtuInterface modbusRtu = new ModbusRtuInterface(modbusUart, 2400, 8, UartStopBitCount.One, UartParity.None);
        ModbusMaster modbusMaster = new ModbusMaster(modbusRtu);
        ushort[] modbusRegsArray;
            modbusRegsArray = modbusMaster.ReadInputRegisters(1, 0, 2, 1000);
        catch (Exception ex)
            string mess = ex.Message;

Still I do not get a correct answer because of timeout.
Seems to be time to use oscilloscope / logicanalyzer

Edit: Now it works!!
I had to include this statement to get the Uart working.

 modbusUart.SetActiveSettings(2400, 8, UartParity.None, UartStopBitCount.One,

I now included this line in the Code above.

That’s really kind of easy to get a Modbus Transmission.
Bravo GHI :clap::clap:

Yeah, you used to have to .Open the serial port in NETMF 4.3, but here it looks like .SetActiveSettings automatically “opens” the port as part of the setup process. If you dont call it, the port doesnt open. This makes it simpler to setup by one line of code, but someone somewhere might have a situation where they don’t want to open it right away. Glad its working!

1 Like