Page 1 of 1
Modbus RS485
Posted: Wed Jun 07, 2023 10:04 am
by Alexanders
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?
Re: Modbus RS485
Posted: Thu Jun 08, 2023 12:04 am
by TomKerekes
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.
Re: Modbus RS485
Posted: Thu Jun 08, 2023 10:26 am
by Alexanders
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.
Re: Modbus RS485
Posted: Fri Jun 09, 2023 9:57 am
by Alexanders
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?
Re: Modbus RS485
Posted: Fri Jun 09, 2023 6:55 pm
by TomKerekes
Delete the duplicate strncpy function as it is now included in KFLOP's library.
Re: Modbus RS485
Posted: Thu Jun 15, 2023 11:16 pm
by Alexanders
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?
Re: Modbus RS485
Posted: Sun Jun 18, 2023 12:58 am
by TomKerekes
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;
}
Re: Modbus RS485
Posted: Tue Jun 20, 2023 11:22 am
by IvanSiberia
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