Advertisement
pippero

Untitled

Nov 10th, 2022
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include "supporto.h"
  2.  
  3.  
  4. #include <SoftwareSerial.h>
  5. #define SSerialRX 10
  6. #define SSerialTX 11
  7. #define SSerialTxControl 3
  8. #define RS485Transmit HIGH
  9. #define RS485Receive LOW
  10.  
  11. SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
  12.  
  13. byte ricevuto ;
  14.  
  15. int cont=0;
  16. unsigned long previousMillis = 0;
  17. const long interval = 300;
  18. typedef struct {
  19. // id messaggio
  20. uint32_t idmsg;
  21. // id del destinatario
  22. uint32_t destinatario;
  23. // valori da comunicare al destinatario
  24. uint32_t comando;
  25. uint32_t valore;
  26. } t_dati;
  27. #define DIMDATI (sizeof(t_dati))
  28. typedef struct {
  29. t_dati dati;
  30. uint32_t crc;
  31. } t_pack;
  32. #define DIMPACK (sizeof(t_pack))
  33.  
  34. //ricezione
  35. typedef struct {
  36. // id messaggio
  37. uint32_t idmsg1;
  38. // id del destinatario
  39. uint32_t destinatario1;
  40. // valori da comunicare al destinatario
  41. uint32_t comando1;
  42. uint32_t valore1;
  43. } t_dati1;
  44. #define DIMDATI1 (sizeof(t_dati1))
  45. typedef struct {
  46. t_dati1 dati1;
  47. uint32_t crc;
  48. } t_pack1;
  49. #define DIMPACK1 (sizeof(t_pack1))
  50.  
  51.  
  52. t_pack tx;
  53. t_pack1 rx;
  54. void setup() {
  55. RS485Serial.begin(9600);
  56. Serial.begin(9600);
  57. Serial.println("\nTEST invio struct");
  58. pinMode(SSerialTxControl, OUTPUT);
  59. digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
  60. }
  61.  
  62. void loop() {
  63. if (RS485Serial.available()>0) {
  64. ricevuto = Ricevi(RS485Serial, (unsigned char *)&rx, DIMPACK);
  65. if (ricevuto == RX_OK) {
  66. // i dati sono arrivati
  67. // verifico il CRC
  68. // if (rx.crc == calc_crc32((unsigned char *)&rx.dati, DIMDATI)) {
  69. // i dati sono validi
  70. // verifico che la richiesta sia per questo slave
  71. if (rx.dati1.destinatario1 == 1) {
  72. // faccio quello che devo fare
  73. }
  74. Serial.println(rx.dati1.valore1);
  75.  
  76. }
  77. }
  78.  
  79. if (millis() - previousMillis >= interval) {
  80. previousMillis += interval;
  81.  
  82. cont ++;
  83. tx.dati.idmsg = millis();
  84. tx.dati.destinatario = 1;
  85. tx.dati.comando = 99;
  86. tx.dati.valore =cont;
  87. //tx.crc = calc_crc32((unsigned char *)&tx, DIMDATI);
  88. digitalWrite(SSerialTxControl, RS485Transmit);
  89. Trasmetti(RS485Serial, (unsigned char *)&tx, DIMPACK);
  90. digitalWrite(SSerialTxControl, RS485Receive);
  91. }
  92. }
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement