Advertisement
pippero

Untitled

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