Modbus RS485

Moderators: TomKerekes, dynomotion

Post Reply
Alexanders
Posts: 39
Joined: Wed May 03, 2023 12:54 am

Modbus RS485

Post by Alexanders » Wed Jun 07, 2023 10:04 am

I'm dealing with an example program ModbusMasterVer1.c
The RS485 interface is half duplex and transceiver chip requires a receive/transmit control input. However, in the example program, I do not see the control of the transceiver pin.Are there any examples of user programs using such control?

User avatar
TomKerekes
Posts: 2676
Joined: Mon Dec 04, 2017 1:49 am

Re: Modbus RS485

Post by TomKerekes » Thu Jun 08, 2023 12:04 am

KFLOP only has a 3.3V UART see here. Kanalog has a converter to change it to RS232. Our Kogna has an RS485 Port. To do RS485 you will most likely need a 3rd party UART to RS485 converter. You might see this Thread and this Thread.
Regards,

Tom Kerekes
Dynomotion, Inc.

Alexanders
Posts: 39
Joined: Wed May 03, 2023 12:54 am

Re: Modbus RS485

Post by Alexanders » Thu Jun 08, 2023 10:26 am

Thanks! I found out that there are 485 converters with automatic detection of the direction of reception / transmission. They do not require an additional control pin from the user's program.

Alexanders
Posts: 39
Joined: Wed May 03, 2023 12:54 am

Re: Modbus RS485

Post by Alexanders » Fri Jun 09, 2023 9:57 am

There was one problem during compilation.

When compiling files ModbusMasterVer1.c and DVP-PLC MODBUS-56IN-46OUT 170102.c from a folder c:\KMotion5.0.7\C Programs\RS232\ModBus\ ,the compiler throws an error :
C:\KMotion5.0.7\DSP_KFLOP\DSPKFLOP.out:1: 'strncpy' defined twice

How to correct this error correctly?

User avatar
TomKerekes
Posts: 2676
Joined: Mon Dec 04, 2017 1:49 am

Re: Modbus RS485

Post by TomKerekes » Fri Jun 09, 2023 6:55 pm

Delete the duplicate strncpy function as it is now included in KFLOP's library.
Regards,

Tom Kerekes
Dynomotion, Inc.

Alexanders
Posts: 39
Joined: Wed May 03, 2023 12:54 am

Re: Modbus RS485

Post by Alexanders » Thu Jun 15, 2023 11:16 pm

In the process of implementation the ModbusMasterVer1.c program, I found that this program only works with Modbus commands 0x03, 0x04 and 0x10.
In the Modbus standard, these commands are defined as working with Holding and Input registers.

When working with my Modbus device, I need standard Modbus commands 0x01, 0x02, 0x0F to work with discrete inputs and coils.
The formats of these commands and responses are slightly different, and simply substituting their contents in ModbusMasterVer1.c does not generate the necessary queries.
Do you have a Modbus master program with a set of basic commands?

User avatar
TomKerekes
Posts: 2676
Joined: Mon Dec 04, 2017 1:49 am

Re: Modbus RS485

Post by TomKerekes » Sun Jun 18, 2023 12:58 am

No I don't think we do. Most PLCs allow Inputs and Coils to be accessed through Holding Registers which is more efficient.

See section Review of Modbus Register Types here.

Do you have the specification on your device? Do you really need those commands?

Otherwise I think this switch statement would need to be modified to add the other command types

Code: Select all

	switch(ModbusMaster_packetBuild[1])
	{
		case 0x10: // RW Write Multiple Regs
			ModbusMaster_RegLoad();
			*chp++=ModbusMaster_packetBuild[5]*2;
			for (x=0;x<ModbusMaster_packetBuild[5];x++)
			{
				*chp++=(MBRegisters[x+ModbusMaster_SentPtr->reg]>>8)&0xFF;
				*chp++=MBRegisters[x+ModbusMaster_SentPtr->reg]&0xFF;
			}
			break;
		case 0x03:	// RO Read
		case 0x04: // RW Read
			break;
		default:
			printf("Unexpected default: ModbusMaster_Send(), Function %d\n",ModbusMaster_packetBuild[1]); //debug
			break;
	}
And this switch statement to handle the returned data

Code: Select all

	switch(Buffer[1])
	{
		case 0x03:
		case 0x04:
			regndx=ModbusMaster_SentPtr->reg;
			cnt=Buffer[2];
			for (x=0;x<cnt;x+=2)
				MBRegisters[regndx++]=((Buffer[3+x]<<8)&0xFF00)|(Buffer[4+x]&0x00FF);
			ModbusMaster_RegUnload();
			break;
		case 0x10:
			// no action on successful write
			break;
		default:
			printf("Unexpected default: Process_Data(), Buffer[1]=%d\n",Buffer[1]);
			break;
	}
Regards,

Tom Kerekes
Dynomotion, Inc.

IvanSiberia
Posts: 30
Joined: Fri Jun 17, 2022 11:21 am

Re: Modbus RS485

Post by IvanSiberia » Tue Jun 20, 2023 11:22 am

Alexanders wrote:
Wed Jun 07, 2023 10:04 am
I'm dealing with an example program ModbusMasterVer1.c
The RS485 interface is half duplex and transceiver chip requires a receive/transmit control input. However, in the example program, I do not see the control of the transceiver pin.Are there any examples of user programs using such control?
You can use the MAX13487/13488 chip with automatic flow switching

Post Reply