Prototypes for reading data. Finally got it working
This commit is contained in:
		
							parent
							
								
									364fab188b
								
							
						
					
					
						commit
						70dce23d1d
					
				
							
								
								
									
										67
									
								
								firmware/testing/rs485/dev_a.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								firmware/testing/rs485/dev_a.ino
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,67 @@ | ||||
| #include <ModbusMaster.h> | ||||
| #include <SoftwareSerial.h> | ||||
| 
 | ||||
| // Define software serial pins (use any digital pins if not using hardware serial)
 | ||||
| #define RX_PIN 13 | ||||
| #define TX_PIN 12 | ||||
| #define DE_PIN 3 | ||||
| #define RE_PIN 4 | ||||
| 
 | ||||
| SoftwareSerial s1(RX_PIN, TX_PIN); | ||||
| 
 | ||||
| void setup() { | ||||
|   // Start the software serial port
 | ||||
|   s1.begin(9600 ); | ||||
|    | ||||
| 
 | ||||
|  //pinMode(RX_PIN, INPUT);
 | ||||
|   //pinMode(TX_PIN, OUTPUT);
 | ||||
|   pinMode(DE_PIN, OUTPUT); | ||||
|   pinMode(RE_PIN, OUTPUT); | ||||
| 
 | ||||
|   digitalWrite(DE_PIN, LOW); | ||||
|   digitalWrite(RE_PIN, LOW); | ||||
| 
 | ||||
|   //node.setTimeout(2000);
 | ||||
|   // Optionally, start the Serial monitor for debugging
 | ||||
|   Serial.begin(9600); | ||||
| 
 | ||||
|   while (!s1) { | ||||
|   } | ||||
|   Serial.println("Setup done B"); | ||||
|    | ||||
| } | ||||
| 
 | ||||
| int byteReceived; | ||||
| static unsigned long lastRefreshTime = 0; | ||||
| static unsigned long cnt = 0; | ||||
| 
 | ||||
| void loop() { | ||||
|   cnt++; | ||||
|   if (s1.available())  //Look for data from other Arduino
 | ||||
|   { | ||||
|     int inByte = s1.read(); | ||||
|     Serial.write(inByte);    | ||||
|   } | ||||
| 
 | ||||
|   //delay(100); // Wait a second before the next loop
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|   if(millis() - lastRefreshTime >= 1000) | ||||
| 	{ | ||||
|      | ||||
|     digitalWrite(RE_PIN, HIGH); | ||||
|     digitalWrite(DE_PIN, HIGH); | ||||
|     s1.print("Its working.....");   | ||||
|     s1.println(cnt);  | ||||
|      | ||||
|     s1.flush(); | ||||
|     //delay(1000);
 | ||||
|     digitalWrite(RE_PIN, LOW); | ||||
|     digitalWrite(DE_PIN, LOW); | ||||
| 		lastRefreshTime += 1000; | ||||
|     Serial.println("Cycle2\n\n"); | ||||
|     | ||||
| 	} | ||||
| } | ||||
							
								
								
									
										70
									
								
								firmware/testing/rs485/dev_b.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								firmware/testing/rs485/dev_b.ino
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,70 @@ | ||||
| #include <ModbusMaster.h> | ||||
| #include <SoftwareSerial.h> | ||||
| 
 | ||||
| // Define software serial pins (use any digital pins if not using hardware serial)
 | ||||
| #define RX_PIN 13 | ||||
| #define TX_PIN 12 | ||||
| #define DE_PIN 3 | ||||
| #define RE_PIN 4 | ||||
| 
 | ||||
| SoftwareSerial s1(RX_PIN, TX_PIN); | ||||
| 
 | ||||
| void setup() { | ||||
|   // Start the software serial port
 | ||||
|   s1.begin(9600 ); | ||||
|   | ||||
|   //pinMode(RX_PIN, INPUT);
 | ||||
|   //pinMode(TX_PIN, OUTPUT);
 | ||||
|   pinMode(DE_PIN, OUTPUT); | ||||
|   pinMode(RE_PIN, OUTPUT); | ||||
| 
 | ||||
|   digitalWrite(DE_PIN, LOW); | ||||
|   digitalWrite(RE_PIN, LOW); | ||||
| 
 | ||||
|   //node.setTimeout(2000);
 | ||||
|   // Optionally, start the Serial monitor for debugging
 | ||||
|   Serial.begin(9600); | ||||
| 
 | ||||
|   while (!s1) { | ||||
|   } | ||||
|   Serial.println("Setup done A"); | ||||
|    | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| void loop() { | ||||
|   static unsigned long lastRefreshTime = 0; | ||||
|   s1.listen(); | ||||
|   while (s1.available() > 0) { | ||||
|     int inByte = s1.read(); | ||||
|     Serial.write(inByte); | ||||
|   } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|   //delay(10); // Wait a second before the next loop
 | ||||
| 
 | ||||
|   // digitalWrite(RE_PIN, HIGH);
 | ||||
|   // s1.print("Hello");   
 | ||||
|   // digitalWrite(RE_PIN, LOW);
 | ||||
| 
 | ||||
|   if(millis() - lastRefreshTime >= 1000) | ||||
| 	{ | ||||
| 		lastRefreshTime += 1000; | ||||
|     Serial.println("Cycle\n\n"); | ||||
| 
 | ||||
|     digitalWrite(RE_PIN, HIGH); | ||||
|     digitalWrite(DE_PIN, HIGH); | ||||
|     s1.print("Ping..");       | ||||
|     s1.flush(); | ||||
|     //delay(1000);
 | ||||
|     digitalWrite(RE_PIN, LOW); | ||||
|     digitalWrite(DE_PIN, LOW); | ||||
| 
 | ||||
| 	} | ||||
|    | ||||
| } | ||||
							
								
								
									
										108
									
								
								firmware/testing/rs485/working_reader_datetime.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								firmware/testing/rs485/working_reader_datetime.ino
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,108 @@ | ||||
| #include <ModbusMaster.h> | ||||
| #include <SoftwareSerial.h> | ||||
| 
 | ||||
| // Define software serial pins (use any digital pins if not using hardware serial)
 | ||||
| #define RX_PIN 13 | ||||
| #define TX_PIN 12 | ||||
| #define DE_PIN 3 | ||||
| #define RE_PIN 4 | ||||
| 
 | ||||
| SoftwareSerial s1(RX_PIN, TX_PIN); | ||||
| ModbusMaster node; | ||||
| uint16_t holdingRegisters[20];  | ||||
| 
 | ||||
| void setup() { | ||||
|   // Start the software serial port
 | ||||
|   s1.begin(9600 ); | ||||
|   | ||||
|   //pinMode(RX_PIN, INPUT);
 | ||||
|   //pinMode(TX_PIN, OUTPUT);
 | ||||
|   pinMode(DE_PIN, OUTPUT); | ||||
|   pinMode(RE_PIN, OUTPUT); | ||||
| 
 | ||||
|   digitalWrite(DE_PIN, LOW); | ||||
|   digitalWrite(RE_PIN, LOW); | ||||
| 
 | ||||
|   //node.setTimeout(2000);
 | ||||
|   // Optionally, start the Serial monitor for debugging
 | ||||
|   Serial.begin(9600); | ||||
| 
 | ||||
|   while (!s1) { | ||||
|   } | ||||
|   node.begin(101, s1);  | ||||
|   node.preTransmission(preTransmission); | ||||
|   node.postTransmission(postTransmission); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| void preTransmission() | ||||
| { | ||||
|   digitalWrite(DE_PIN, HIGH); // Enable RS485 transmit
 | ||||
|   digitalWrite(RE_PIN, HIGH); | ||||
| 
 | ||||
|   | ||||
| } | ||||
| 
 | ||||
| void postTransmission() | ||||
| { | ||||
|   digitalWrite(DE_PIN, LOW); // Disable RS485 transmit
 | ||||
|   digitalWrite(RE_PIN, LOW); | ||||
| } | ||||
| 
 | ||||
| static unsigned long lastRefreshTime = 0; | ||||
| #define ReadSize 4 | ||||
| void loop() { | ||||
|   static uint16_t count = 0;  | ||||
|   uint8_t result; | ||||
|   int16_t data[ReadSize]; | ||||
| 
 | ||||
| 
 | ||||
|   //delay(100); // Wait a second before the next loop
 | ||||
| 
 | ||||
|   // digitalWrite(RE_PIN, HIGH);
 | ||||
|   // s1.print("Hello");   
 | ||||
|   // digitalWrite(RE_PIN, LOW);
 | ||||
| 
 | ||||
|   if(millis() - lastRefreshTime >= 1000) | ||||
| 	{ | ||||
| 
 | ||||
| 		lastRefreshTime += 1000; | ||||
|     Serial.print("Cycle\n\n"); | ||||
| 
 | ||||
| 
 | ||||
|     result = node.readHoldingRegisters(1836 , 8); | ||||
| 
 | ||||
|     // Check if the read was successful
 | ||||
|     if (result == node.ku8MBSuccess) | ||||
|     { | ||||
|       Serial.print("Meter Date Time Read successful: "); | ||||
|       Serial.print("Y : "); | ||||
|       Serial.println(node.getResponseBuffer(0)); | ||||
|       Serial.print("M : "); | ||||
|       Serial.println(node.getResponseBuffer(1)); | ||||
|       Serial.print("D : "); | ||||
|       Serial.println(node.getResponseBuffer(2)); | ||||
|       Serial.print("H : "); | ||||
|       Serial.println(node.getResponseBuffer(3)); | ||||
|       Serial.print("M : "); | ||||
|       Serial.println(node.getResponseBuffer(4)); | ||||
|       // for (uint8_t j = 0; j < ReadSize; j++)
 | ||||
|       // {
 | ||||
|       //   int recv = node.getResponseBuffer(j);
 | ||||
|       //   data[j] = recv;
 | ||||
|       //   Serial.print(recv );
 | ||||
|       // }
 | ||||
|       // Serial.print("\nHEX:");
 | ||||
|       // for (uint8_t j = 0; j < ReadSize; j++)
 | ||||
|       // {
 | ||||
|       //   Serial.print(data[j],"HEX");
 | ||||
|       // }
 | ||||
|       // Serial.println();
 | ||||
|     } else { | ||||
|       Serial.print("Read error: "); | ||||
|       Serial.println(result, HEX); | ||||
|     } | ||||
| 
 | ||||
| 	} | ||||
|    | ||||
| } | ||||
							
								
								
									
										109
									
								
								firmware/testing/rs485/working_reader_kwh_del_rec.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								firmware/testing/rs485/working_reader_kwh_del_rec.ino
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,109 @@ | ||||
| #include <ModbusMaster.h> | ||||
| #include <SoftwareSerial.h> | ||||
| 
 | ||||
| // Define software serial pins (use any digital pins if not using hardware serial)
 | ||||
| #define RX_PIN 13 | ||||
| #define TX_PIN 12 | ||||
| #define DE_PIN 3 | ||||
| #define RE_PIN 4 | ||||
| 
 | ||||
| SoftwareSerial s1(RX_PIN, TX_PIN); | ||||
| ModbusMaster node; | ||||
| uint16_t holdingRegisters[20];  | ||||
| 
 | ||||
| void setup() { | ||||
|   // Start the software serial port
 | ||||
|   s1.begin(9600 ); | ||||
|   | ||||
|   //pinMode(RX_PIN, INPUT);
 | ||||
|   //pinMode(TX_PIN, OUTPUT);
 | ||||
|   pinMode(DE_PIN, OUTPUT); | ||||
|   pinMode(RE_PIN, OUTPUT); | ||||
| 
 | ||||
|   digitalWrite(DE_PIN, LOW); | ||||
|   digitalWrite(RE_PIN, LOW); | ||||
| 
 | ||||
|   //node.setTimeout(2000);
 | ||||
|   // Optionally, start the Serial monitor for debugging
 | ||||
|   Serial.begin(9600); | ||||
| 
 | ||||
|   while (!s1) { | ||||
|   } | ||||
|   node.begin(101, s1);  | ||||
|   node.preTransmission(preTransmission); | ||||
|   node.postTransmission(postTransmission); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| void preTransmission() | ||||
| { | ||||
|   digitalWrite(DE_PIN, HIGH); // Enable RS485 transmit
 | ||||
|   digitalWrite(RE_PIN, HIGH); | ||||
| 
 | ||||
|   | ||||
| } | ||||
| 
 | ||||
| void postTransmission() | ||||
| { | ||||
|   digitalWrite(DE_PIN, LOW); // Disable RS485 transmit
 | ||||
|   digitalWrite(RE_PIN, LOW); | ||||
| } | ||||
| 
 | ||||
| static unsigned long lastRefreshTime = 0; | ||||
| #define ReadSize 4 | ||||
| void loop() { | ||||
|   static uint16_t count = 0;  | ||||
|   uint8_t result; | ||||
|   int16_t data[ReadSize]; | ||||
| 
 | ||||
| 
 | ||||
|   //delay(100); // Wait a second before the next loop
 | ||||
| 
 | ||||
|   // digitalWrite(RE_PIN, HIGH);
 | ||||
|   // s1.print("Hello");   
 | ||||
|   // digitalWrite(RE_PIN, LOW);
 | ||||
| 
 | ||||
|   if(millis() - lastRefreshTime >= 1000) | ||||
| 	{ | ||||
| 
 | ||||
| 		lastRefreshTime += 1000; | ||||
|     Serial.print("Cycle\n\n"); | ||||
| 
 | ||||
|      | ||||
|     result = node.readHoldingRegisters(2705 , 2); | ||||
| 
 | ||||
|     // Check if the read was successful
 | ||||
|     if (result == node.ku8MBSuccess) | ||||
|     { | ||||
|       Serial.print("Read successful: "); | ||||
|   | ||||
|       uint16_t highWord = node.getResponseBuffer(0); | ||||
|       uint16_t lowWord = node.getResponseBuffer(1); | ||||
|       uint32_t floatRaw = ((uint32_t)highWord << 16) | lowWord; // Combine registers
 | ||||
|       float floatValue; | ||||
|        | ||||
|       // Convert raw 32-bit value to float
 | ||||
|       memcpy(&floatValue, &floatRaw, sizeof(float)); | ||||
|        | ||||
|       Serial.print("Float value: "); | ||||
|       Serial.println(floatValue); | ||||
|       // for (uint8_t j = 0; j < ReadSize; j++)
 | ||||
|       // {
 | ||||
|       //   int recv = node.getResponseBuffer(j);
 | ||||
|       //   data[j] = recv;
 | ||||
|       //   Serial.print(recv );
 | ||||
|       // }
 | ||||
|       // Serial.print("\nHEX:");
 | ||||
|       // for (uint8_t j = 0; j < ReadSize; j++)
 | ||||
|       // {
 | ||||
|       //   Serial.print(data[j],"HEX");
 | ||||
|       // }
 | ||||
|       // Serial.println();
 | ||||
|     } else { | ||||
|       Serial.print("Read error: "); | ||||
|       Serial.println(result, HEX); | ||||
|     } | ||||
| 
 | ||||
| 	} | ||||
|    | ||||
| } | ||||
							
								
								
									
										99
									
								
								firmware/testing/rs485/working_reader_string.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								firmware/testing/rs485/working_reader_string.ino
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,99 @@ | ||||
| #include <ModbusMaster.h> | ||||
| #include <SoftwareSerial.h> | ||||
| 
 | ||||
| // Define software serial pins (use any digital pins if not using hardware serial)
 | ||||
| #define RX_PIN 13 | ||||
| #define TX_PIN 12 | ||||
| #define DE_PIN 3 | ||||
| #define RE_PIN 4 | ||||
| 
 | ||||
| SoftwareSerial s1(RX_PIN, TX_PIN); | ||||
| ModbusMaster node; | ||||
| uint16_t holdingRegisters[20];  | ||||
| 
 | ||||
| void setup() { | ||||
|   // Start the software serial port
 | ||||
|   s1.begin(9600 ); | ||||
|   | ||||
|   //pinMode(RX_PIN, INPUT);
 | ||||
|   //pinMode(TX_PIN, OUTPUT);
 | ||||
|   pinMode(DE_PIN, OUTPUT); | ||||
|   pinMode(RE_PIN, OUTPUT); | ||||
| 
 | ||||
|   digitalWrite(DE_PIN, LOW); | ||||
|   digitalWrite(RE_PIN, LOW); | ||||
| 
 | ||||
|   //node.setTimeout(2000);
 | ||||
|   // Optionally, start the Serial monitor for debugging
 | ||||
|   Serial.begin(9600); | ||||
| 
 | ||||
|   while (!s1) { | ||||
|   } | ||||
|   node.begin(101, s1);  | ||||
|   node.preTransmission(preTransmission); | ||||
|   node.postTransmission(postTransmission); | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| void preTransmission() | ||||
| { | ||||
|   digitalWrite(DE_PIN, HIGH); // Enable RS485 transmit
 | ||||
|   digitalWrite(RE_PIN, HIGH); | ||||
| 
 | ||||
|   | ||||
| } | ||||
| 
 | ||||
| void postTransmission() | ||||
| { | ||||
|   digitalWrite(DE_PIN, LOW); // Disable RS485 transmit
 | ||||
|   digitalWrite(RE_PIN, LOW); | ||||
| } | ||||
| 
 | ||||
| static unsigned long lastRefreshTime = 0; | ||||
| #define ReadSize 4 | ||||
| void loop() { | ||||
|   static uint16_t count = 0;  | ||||
|   uint8_t result; | ||||
|   int16_t data[ReadSize]; | ||||
| 
 | ||||
| 
 | ||||
|   //delay(100); // Wait a second before the next loop
 | ||||
| 
 | ||||
|   // digitalWrite(RE_PIN, HIGH);
 | ||||
|   // s1.print("Hello");   
 | ||||
|   // digitalWrite(RE_PIN, LOW);
 | ||||
| 
 | ||||
|   if(millis() - lastRefreshTime >= 1000) | ||||
| 	{ | ||||
| 
 | ||||
| 		lastRefreshTime += 1000; | ||||
|     Serial.print("Cycle\n\n"); | ||||
| 
 | ||||
| 
 | ||||
|     result = node.readHoldingRegisters(29 , 70); | ||||
| 
 | ||||
|     // Check if the read was successful
 | ||||
|     if (result == node.ku8MBSuccess) | ||||
|     { | ||||
|       Serial.print("Read successful: "); | ||||
|       for (uint8_t j = 0; j < 70; j++) | ||||
|       { | ||||
|         uint8_t v = node.getResponseBuffer(j); | ||||
|         char a = v; | ||||
|         if (v == 0) { | ||||
|           continue; | ||||
|         } | ||||
|         Serial.print(a); | ||||
|       } | ||||
|     | ||||
|   | ||||
|     | ||||
| 
 | ||||
|     } else { | ||||
|       Serial.print("Read error: "); | ||||
|       Serial.println(result, HEX); | ||||
|     } | ||||
| 
 | ||||
| 	} | ||||
|    | ||||
| } | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user