Page 1 of 1

Uart Parity and Modbus RTU

Posted: Wed Jun 02, 2021 10:05 pm
by SpeedPIC32
Hello all.
I have a problem with the serial interfaces.
PIC32MZ2048EFH100 200MHz
where or how can I set the parity?
I would like to query a modbus sensor.
it has a 485 interface and 19200 baud.
8Data Bits 1 Stop Bit Parity even Modbus Address 247
Modbus RTU
The following Modbus RTU functions are supported:

Code, Name, Description
0x01, Read Coils, Read one or more coils
0x03, Read Holding Registers, reading a continuous holding register block
0x04, Read Input Registers, read one or more consecutive registers
0x05, Write Single Coil, Write one coil

I am curious who knows
thank you

Re: Uart Parity and Modbus RTU

Posted: Thu Jun 03, 2021 8:54 am
by SpeedPIC32
Hello LeighM and dionyzoz
You once wrote some C code for the STM32 to set parity to EVEN.
I need the same for a PI32MZ2048EFH100

for the ST32 the code was

HAL_UART_DeInit(&MX_UART_NUMBER_1);
MX_UART_NUMBER_1.Init.Parity = UART_PARITY_EVEN;
HAL_UART_Init(&MX_UART_NUMBER_1);



Thanks a lot

Re: Uart Parity and Modbus RTU

Posted: Thu Jun 03, 2021 12:30 pm
by BenR
Hello,

This is an interesting problem. I think for the PIC32 device it doesn't auto calculate the parity bit for you and so you would have to calculate it and put into the UART as a 9th data bit.

We can look at adding this into the component so you can enable it via a property and also maybe into the lower level CAL UART so we can potentially add it to any UART comms based system that may require it.

Re: Uart Parity and Modbus RTU

Posted: Thu Jun 03, 2021 5:51 pm
by SpeedPIC32
Hello Ben
I found the following on the page "PIC32 for the hobbyist" and tried a little.

void UART_Init()
{
int pbClk;

pbClk = SYS_FREQ / 2; // Our PBCLK2 divider was set to 1, so PBCLK2 is exactly half the speed of the system clock, or 100Mhz

U5MODE = 0; // Set UART 5 off prior to setting it up
U5MODEbits.BRGH = 0; // We want standard speed mode. Not necessary as we just set U5MODE to 0 so this is just for explanation's sake
U5BRG = pbClk / (16 * 38400) - 1;// This is the formula straight from the datasheet
U5STA = 0; // Disable the TX and RX pins, clear all flags
U5STAbits.UTXEN = 1; // Enable the TX pin
U5STAbits.URXEN = 1; // Enable the RX pin
U5MODEbits.PDSEL = 0; // PDSEL controls how many data bits and how many parity bits we want, this is the default of 8-bit data, no parity bits that most terminals use
U5MODEbits.STSEL = 0; // STSEL controls how many stop bits we use, let's use the default of 1
U5MODEbits.ON = 1; // Turn on the UART 5 peripheral


I use this as a macro

Makro.png
Makro.png (69.26 KiB) Viewed 6906 times
I can use the Modbus commands at the moment.
my sensor also responds.
As I understand it, the individual bytes that my sensor sends in response are automatically stored in the receive buffer.
Can you give me a short example based on my data?
FC) Abfrage.png
FC) Abfrage.png (18.28 KiB) Viewed 6906 times

this is being broadcast 0xF7 0x03(error) 0x07 0xE0 0x00(error) 0x02 0xd0 0x1f



this is received
Antwort.png
Antwort.png (26.42 KiB) Viewed 6906 times
this is received "28.23926" this is °C

how do I get the value from the buffer.
I read it from the PC software of the sensor.

I hope you get it right.

I think the UART settings can be extended.

Thanks a lot

Re: Uart Parity and Modbus RTU

Posted: Wed Nov 22, 2023 5:49 pm
by Alan_37
Hi

I also need to set Even Parity for Atmega 328P

anyone can help please