Forum: Projekte & Code Software Profibus DP-Slave in C


Announcement: there is an English version of this forum on EmbDev.net. Posts you create there will be displayed on Mikrocontroller.net and EmbDev.net.
von Amigo I. (Firma: hobby) (sony2019)


Bewertung
0 lesenswert
nicht lesenswert
OK I has been done with UNO based on code from Tadej, I think it's same 
with NANO setup.
I only change in serial setup from serial2 to serial0.
I has been followed this topic since November 2013 and beginning to try 
again a few month ago.

Thank you for everything.

: Bearbeitet durch User
von Tadej S. (tadythefish)


Bewertung
0 lesenswert
nicht lesenswert
I'm sorry for the late reply. Did you manage to make it work? If not I 
can also post code for the UNO/Nano version.
I made it work with a Nano (MEGA328) and 5 i2c PWM output expanders. I 
have been testing this slave for 3 months now with no profibus errors. 
It works great but only goes up to 45.45k speed
Anyway those 5 PWM expander provide 48 PWM outputs!

von Amigo I. (Firma: hobby) (sony2019)


Bewertung
0 lesenswert
nicht lesenswert
Hallo,
It is great. I also test with DS18B20 temperature sensor.
Now I still test to ATMega32 based arduino MIGHTYCORE but not success 
yet.
@Tadej if you have code is wellcome.

Regards

von Yuriy (Gast)


Bewertung
0 lesenswert
nicht lesenswert
Michael schrieb:
> Dear mr. Jörg S!
> Many thanks for such an excellent implementation of the Profibus DP
> software slave. With minor changes, on the Cortex-M0 I achieved a stable
> exchange rate of 3Mb / sec. Thanks again for such a wonderful job!
> Best Regards, Michael.

Michael, can you share the code?

von Rico S. (ridchi1980)


Bewertung
0 lesenswert
nicht lesenswert
Tadej S. schrieb:
> Hello!
>
> I would like to share my adaption of your software to Arduino MEGA2560
> Hope it helps someone!

Hello TadyTheFish,

I use a Controllino Mega and would like to use your implementation.
https://www.controllino.biz/wp-content/uploads/2018/10/CONTROLLINO-MEGA-Pinout.pdf

In the Controllino is an SN65HVD08, which should actually work the same 
way.
Your code contains a lot of assembler, which I cannot.

Can you rewrite me your code that I can work with RX3-TX3 instead of 
RX2-TX2.

Thanks.
Ridchi

von Amigo I. (Firma: hobby) (sony2019)


Bewertung
0 lesenswert
nicht lesenswert
If still use the same controller with Arduino mega maybe you just change 
the register related serial3 and also consider the working clock for the 
CPU. In uno / nano I just change the register serial2 to serial0 from 
Tadej's code. You can read the Atmega2560 datasheet to find the 
register.

von Tadej S. (tadythefish)


Angehängte Dateien:

Bewertung
0 lesenswert
nicht lesenswert
Hello

Sorry for the late reply. I have editedd the programs and made 3 
versions.

Mega2560 UART2
Mega2560 UART3
Mega328

I have not tested the UART3 version so please if it works leave reply or 
if it dos not :)

EDIT: Whoops.. both attachements are the same :D

: Bearbeitet durch User
von Amigo I. (Firma: hobby) (sony2019)


Bewertung
0 lesenswert
nicht lesenswert
Hi Tadej,
Do you have try the ATMega16 or ATMega32? Please share the code.

Thank you in advance

von Tadej S. (tadythefish)


Bewertung
0 lesenswert
nicht lesenswert
Sorry I don't. I work with arduino IDE and those chips are not included 
by default and I never had a need for them. You could addopt the code to 
work with those chips but you need to find the registers for UART and 
Timer and change them to suite those chips

von Rico S. (ridchi1980)


Bewertung
0 lesenswert
nicht lesenswert
Thanks Tadej,

i will test it.

Ridchi

von Rico S. (ridchi1980)


Bewertung
0 lesenswert
nicht lesenswert
Tadej S. schrieb:
> Hello
>
> Sorry for the late reply. I have editedd the programs and made 3
> versions.
>
> Mega2560 UART2
> Mega2560 UART3
> Mega328
>
> I have not tested the UART3 version so please if it works leave reply or
> if it dos not :)
>
> EDIT: Whoops.. both attachements are the same :D

Hello Tadej,

i have test the Mega2560 UART3.
You have forgotten to change in row 853:
UCSR2B |= _BV(UDRIE2);  // <<< AVR Version

into
UCSR3B |= _BV(UDRIE3);  // <<< AVR Version

then it works.


But I get a lot of warnings when compiling.
The Problem is the conversion from unsigned char* into char*

for example:
if (checksum(&uart_buffer[1], 3) != FCS_data) break;

you must write for correctly syntax
if (checksum((char*)&uart_buffer[1], 3) != FCS_data) break;

;-)


Currently my Profibus connection works with 93750baud without problems.

Thanks.


PS:  Why did you insert an offset (+5) in uart_buffer?

: Bearbeitet durch User
von Tadej S. (tadythefish)


Bewertung
0 lesenswert
nicht lesenswert
Hmm thank you for your input.. Maybe you could upload the corrected 
version?
I'm sorry so much things to do and so little time. I remember this 
problem the IDE was failing to compile the original version when I was 
"porting" the code.
Anyway the mega328 version is running on my friends test bench for 3 
months and 2 day till today...
Maybe I forgot to change this in the mega2560 code I don't know I didn't 
really gave much attention since it works for my friend
But it works with 45.45k. I can't remember seeing the baudrate you have 
specified in WinCC or TIA portal.. But I am not really the PLC 
programming guy. I just helped my friend that is a Simatic guru 😁

von Tadej S. (tadythefish)


Bewertung
0 lesenswert
nicht lesenswert
I just found out that why the code compiled properly on my IDE..
Compiler warnings were turned off :) thabk you so much.

I have corrected the registers that you have pointed out. Yes I have 
changed on the bottom and I missed the TX routine that needed changing 😜

As I said its been 3 month since I last saw the code 🤣

The +5 is not an offset i expanded the buffer because I had problems and 
I couldn't find what was wrong. The program was crashin so I tought that 
maybe the program goes out of bounds somwhere but in the end there was a 
problem with a poor profibus connection.. You can remove the +5 it 
should work the same.. Or you can leave it there is more than enough 
space

: Bearbeitet durch User
von Amigo I. (Firma: hobby) (sony2019)


Bewertung
0 lesenswert
nicht lesenswert
Tadej S. schrieb:
> Sorry I don't. I work with arduino IDE and those chips are not included
> by default and I never had a need for them. You could addopt the code to
> work with those chips but you need to find the registers for UART and
> Timer and change them to suite those chips

Maybe you can try to add the hardware ATmega16 or 32 from mightycore 
https://github.com/MCUdude/MightyCore

I just have problem in how to compile from your code (Arduino mega) to 
mega32 controller. It's many error that I do not understand. If you can 
compile without error I can try to the my hardware hardware.

von Peter (Gast)


Bewertung
0 lesenswert
nicht lesenswert
Hi Guys.
I saw that there are already M0 ports.
Would be nice if the code were published here.

von M@tti@ (Gast)


Bewertung
0 lesenswert
nicht lesenswert
Hi at all, anybody have an example to read an digital input on the 
Arduino and write in the PLC and viceversa?

Many thanks at all

M@tti@

von Amigo I. (Firma: hobby) (sony2019)


Bewertung
0 lesenswert
nicht lesenswert
M@tti@ schrieb:
> Hi at all, anybody have an example to read an digital input on the
> Arduino and write in the PLC and viceversa?
>
> Many thanks at all
>
> M@tti@

Tadej's code have io access.

von Amigo I. (Firma: hobby) (sony2019)


Bewertung
0 lesenswert
nicht lesenswert
Try to compile with ATMega32 mightycore to many error like below:

########################################################################
Arduino: 1.8.5 (Windows 10), Board: "ATmega32, Standard, 2.7v, Disabled 
(default), 16 MHz external"

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino: In function 'void 
profibus_RX()':

profibus_M32_1.ino:154: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

         if (checksum(&uart_buffer[1], 3) != FCS_data) break;

                                        ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:708:15: note: 
initializing argument 1 of 'unsigned char checksum(char*, unsigned 
char)'

 unsigned char checksum(char *data, unsigned char length)

               ^

profibus_M32_1.ino:171: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

         if (checksum(&uart_buffer[4], PDU_size) != FCS_data) break;

                                               ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:708:15: note: 
initializing argument 1 of 'unsigned char checksum(char*, unsigned 
char)'

 unsigned char checksum(char *data, unsigned char length)

               ^

profibus_M32_1.ino:188: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

         if (checksum(&uart_buffer[1], 8) != FCS_data) break;

                                        ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:708:15: note: 
initializing argument 1 of 'unsigned char checksum(char*, unsigned 
char)'

 unsigned char checksum(char *data, unsigned char length)

               ^

profibus_M32_1.ino:245: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

             profibus_send_CMD(SC, 0, SAP_OFFSET, &uart_buffer[0], 0);

                                                                    ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:596:6: note: 
initializing argument 4 of 'void profibus_send_CMD(unsigned char, 
unsigned char, unsigned char, char*, unsigned char)'

 void profibus_send_CMD (unsigned char type,

      ^

profibus_M32_1.ino:320: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

               profibus_send_CMD(SD2, DATA_LOW, SAP_OFFSET, 
&uart_buffer[7], 8 + EXT_DIAG_DATA_SIZE);

                                                                                                   ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:596:6: note: 
initializing argument 4 of 'void profibus_send_CMD(unsigned char, 
unsigned char, unsigned char, char*, unsigned char)'

 void profibus_send_CMD (unsigned char type,

      ^

profibus_M32_1.ino:349: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

               profibus_send_CMD(SD2, DATA_LOW, SAP_OFFSET, 
&uart_buffer[7], 8 + EXT_DIAG_DATA_SIZE);

                                                                                                   ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:596:6: note: 
initializing argument 4 of 'void profibus_send_CMD(unsigned char, 
unsigned char, unsigned char, char*, unsigned char)'

 void profibus_send_CMD (unsigned char type,

      ^

profibus_M32_1.ino:390: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

               profibus_send_CMD(SC, 0, SAP_OFFSET, &uart_buffer[0], 0);

                                                                      ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:596:6: note: 
initializing argument 4 of 'void profibus_send_CMD(unsigned char, 
unsigned char, unsigned char, char*, unsigned char)'

 void profibus_send_CMD (unsigned char type,

      ^

profibus_M32_1.ino:523: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

             profibus_send_CMD(SC, 0, SAP_OFFSET, &uart_buffer[0], 0);

                                                                    ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:596:6: note: 
initializing argument 4 of 'void profibus_send_CMD(unsigned char, 
unsigned char, unsigned char, char*, unsigned char)'

 void profibus_send_CMD (unsigned char type,

      ^

profibus_M32_1.ino:543: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

         profibus_send_CMD(SD1, FDL_STATUS_OK, 0, &uart_buffer[0], 0);

                                                                    ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:596:6: note: 
initializing argument 4 of 'void profibus_send_CMD(unsigned char, 
unsigned char, unsigned char, char*, unsigned char)'

 void profibus_send_CMD (unsigned char type,

      ^

profibus_M32_1.ino:571: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

           profibus_send_CMD(SD2, DIAGNOSE, 0, &uart_buffer[7], 0);  // 
Request a diagnosis

                                                                 ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:596:6: note: 
initializing argument 4 of 'void profibus_send_CMD(unsigned char, 
unsigned char, unsigned char, char*, unsigned char)'

 void profibus_send_CMD (unsigned char type,

      ^

profibus_M32_1.ino:573: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

           profibus_send_CMD(SD2, DATA_LOW, 0, &uart_buffer[7], 
Input_Data_size);  // send data

                                                                               ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:596:6: note: 
initializing argument 4 of 'void profibus_send_CMD(unsigned char, 
unsigned char, unsigned char, char*, unsigned char)'

 void profibus_send_CMD (unsigned char type,

      ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino: In function 'void 
profibus_send_CMD(unsigned char, unsigned char, unsigned char, char*, 
unsigned char)':

profibus_M32_1.ino:613: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

       uart_buffer[4] = checksum(&uart_buffer[1], 3);

                                                   ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:708:15: note: 
initializing argument 1 of 'unsigned char checksum(char*, unsigned 
char)'

 unsigned char checksum(char *data, unsigned char length)

               ^

profibus_M32_1.ino:632: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

       uart_buffer[7+length_pdu] = checksum(&uart_buffer[4], length_pdu 
+ 3);

                                                                           ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:708:15: note: 
initializing argument 1 of 'unsigned char checksum(char*, unsigned 
char)'

 unsigned char checksum(char *data, unsigned char length)

               ^

profibus_M32_1.ino:648: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

       uart_buffer[9] = checksum(&uart_buffer[4], 8);

                                                   ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:708:15: note: 
initializing argument 1 of 'unsigned char checksum(char*, unsigned 
char)'

 unsigned char checksum(char *data, unsigned char length)

               ^

profibus_M32_1.ino:678: error: invalid conversion from 'unsigned char*' 
to 'char*' [-fpermissive]

   profibus_TX(&uart_buffer[0], length_data);

                                           ^

C:\M32\profibus_M32_1.ino\profibus_M32_1.ino.ino:689:6: note: 
initializing argument 1 of 'void profibus_TX(char*, unsigned char)'

 void profibus_TX (char *data, unsigned char length)

      ^

exit status 1
invalid conversion from 'unsigned char*' to 'char*' [-fpermissive]

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

########################################################################
how to solve?

: Bearbeitet durch User
von Rico S. (ridchi1980)


Bewertung
0 lesenswert
nicht lesenswert
you must write for correctly syntax

if (checksum((char*)&uart_buffer[1], 3) != FCS_data) break;

!!!!!!!!

The same with the other line with Checksum ()

Ridchi

von Amigo I. (Firma: hobby) (sony2019)


Bewertung
0 lesenswert
nicht lesenswert
Hi Ridchi,
The code is compile without error in UNO, NANO and MEGA. What is wrong 
in arduino mightycore?

von Tadej S. (tadythefish)


Bewertung
0 lesenswert
nicht lesenswert
The errors you posted should actually be warnings.
The problem is with registers. Mega32 uses completely different 
registers then a meva328 or 2560 uses. The code needs alot of adapting 
to run on an mega32.

Why don't you upgrade to a different MCU?

von M@tti@ (Gast)


Bewertung
0 lesenswert
nicht lesenswert
Hi @Amigo I., and many thanks for the reply.
I tried the Tadej S. code, but without result, in the PC i don't see the 
state of the digital input (for example Arduino pin 4) change.
How is mapped this digital input (pin 4) of Arduino in the PLC?
In the Hardware Configuration (Step7) i have make for first an Universal 
Module, for second 1 byte Input and for third 1 byte Output. It's ok 
this configuration?
Do you have an example or a screenshot of your project that works?

Thank you very much

M @ TTI @

von Rico S. (ridchi1980)


Bewertung
0 lesenswert
nicht lesenswert
You must Write the state of Pin4 as Bit0 in data_out_register[0] then 
you can used this in PLC I1.0 ect... The input Adress is this who 
configured it in Hardwareconfig.

von Amigo I. (Firma: hobby) (sony2019)


Bewertung
0 lesenswert
nicht lesenswert
Tadej S. schrieb:
> The errors you posted should actually be warnings.
> The problem is with registers. Mega32 uses completely different
> registers then a meva328 or 2560 uses. The code needs alot of adapting
> to run on an mega32.
>

Hi Tadej,
I already change the Mega32 register, no error found related register. 
Only the error: invalid conversion from 'unsigned char*'
to 'char*' [-fpermissive] too many appear. I thing it's not hardware 
setting but about format to write. I not to much familiar with C.

> Why don't you upgrade to a different MCU?
Because I have few of Mega16 / Mega32 :)

thank

von Amigo I. (Firma: hobby) (sony2019)


Angehängte Dateien:

Bewertung
0 lesenswert
nicht lesenswert
M@tti@ schrieb:
> Hi @Amigo I., and many thanks for the reply.
> I tried the Tadej S. code, but without result, in the PC i don't see the
> state of the digital input (for example Arduino pin 4) change.
> How is mapped this digital input (pin 4) of Arduino in the PLC?
> In the Hardware Configuration (Step7) i have make for first an Universal
> Module, for second 1 byte Input and for third 1 byte Output. It's ok
> this configuration?
> Do you have an example or a screenshot of your project that works?
>
> Thank you very much
>
> M @ TTI @

You can play the variable "data_in_register" and "data_out_register" 
related the I/O in "doit" routine.
In Simatic you can see in variable table.

von Vital K. (Firma: UMMC) (vital_k)


Bewertung
0 lesenswert
nicht lesenswert
Michael schrieb:
> Dear mr. Jörg S!
> Many thanks for such an excellent implementation of the Profibus DP
> software slave. With minor changes, on the Cortex-M0 I achieved a stable
> exchange rate of 3Mb / sec. Thanks again for such a wonderful job!
> Best Regards, Michael.


and what RS485-UART chip did you get in contact with at 1.5Mb/s?

von Vital K. (Firma: UMMC) (vital_k)


Bewertung
0 lesenswert
nicht lesenswert
Peter F. schrieb:
> Hello,
>
> I changed code from project AVRSoftBus for atXmega32A4U. It's run on
> 48Mhz. I tested both speed 187500 and 1M5, everything work. In
> profibus.h need change UART_BAUD and DELAY_TBIT. I used USART D0. I
> believe this code is usable for someone.


Hello, and what RS485-UART chip did you get in contact with at 1.5Mb / 
s?

von Bastler (Gast)


Bewertung
0 lesenswert
nicht lesenswert
I have used the MAX1487 for 1,5 MBit/s.

von Piotr (Gast)


Bewertung
0 lesenswert
nicht lesenswert
Hello,
I trying this code with ATmega2560 & 9.6kbps. My PLC always send 0x6D as 
function code.

Received byte: 68 5 5 68 C0 82 6D 3C 3E 29 16
Arduino send: 68 B B 68 FFFFFF82 FFFFFFC0 8 3E 3C 2 5 0 FFFFFFFF 
FFFFFFC0 FFFFFFDE 68 16

Adresses:
PLC 2, Arduino 64.

Please, advise me.

von Piotr (Gast)


Bewertung
0 lesenswert
nicht lesenswert
I found the issue, my transmitter was broken. I test it as Ethernet/IP 
(Devicenet) - Profibus gate. May be  I make a success of my 3 years 
work.

von Jörg S. (joerg-s)


Bewertung
1 lesenswert
nicht lesenswert
It is nice to see the Code is still alive! It gives me a good feeling :)

von Tadej S. (tadythefish)


Bewertung
0 lesenswert
nicht lesenswert
Yes and we have all of you to thank! I ported the code to arduino and 
now i ported it to STM32F405. I got a stable connection at 6Mbits
Only now that i have worked with your code I understand the bus. So 
thank you again

Antwort schreiben

Die Angabe einer E-Mail-Adresse ist freiwillig. Wenn Sie automatisch per E-Mail über Antworten auf Ihren Beitrag informiert werden möchten, melden Sie sich bitte an.

Wichtige Regeln - erst lesen, dann posten!

  • Groß- und Kleinschreibung verwenden
  • Längeren Sourcecode nicht im Text einfügen, sondern als Dateianhang

Formatierung (mehr Informationen...)

  • [c]C-Code[/c]
  • [avrasm]AVR-Assembler-Code[/avrasm]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] - Link zu Artikel
  • Verweis auf anderen Beitrag einfügen: Rechtsklick auf Beitragstitel,
    "Adresse kopieren", und in den Text einfügen




Bild automatisch verkleinern, falls nötig
Bitte das JPG-Format nur für Fotos und Scans verwenden!
Zeichnungen und Screenshots im PNG- oder
GIF-Format hochladen. Siehe Bildformate.

Mit dem Abschicken bestätigst du, die Nutzungsbedingungen anzuerkennen.