Advertisement
RuiViana

Teste_Modbus_Slave

Feb 24th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. /* YourDuino SoftwareSerialExample1Remote
  2. - Used with YD_SoftwareSerialExampleRS485_1 on another Arduino
  3. - Remote: Receive data, loop it back...
  4. - Connect this unit Pins 10, 11, Gnd
  5. - To other unit Pins 11,10, Gnd (Cross over)
  6. - Pin 3 used for RS485 direction control
  7. - Pin 13 LED blinks when data is received
  8.  
  9. Questions: terry@yourduino.com
  10. */
  11.  
  12. /*-----( Import needed libraries )-----*/
  13. #include <SoftwareSerial.h>
  14. /*-----( Declare Constants and Pin Numbers )-----*/
  15. //#define SSerialRX 10 //Serial Receive pin
  16. //#define SSerialTX 11 //Serial Transmit pin
  17. #define SSerialRX 4 //Serial Receive pin
  18. #define SSerialTX 5 //Serial Transmit pin
  19.  
  20. #define SSerialTxControl 3 //RS485 Direction control
  21. #define RS485Transmit HIGH
  22. #define RS485Receive LOW
  23.  
  24. #define Pin13LED 13
  25.  
  26. /*-----( Declare objects )-----*/
  27. SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
  28.  
  29. /*-----( Declare Variables )-----*/
  30. int byteReceived;
  31. int byteSend;
  32.  
  33. void setup() /****** SETUP: RUNS ONCE ******/
  34. {
  35. // Start the built-in serial port, probably to Serial Monitor
  36. Serial.begin(9600);
  37. Serial.println("SerialRemote"); // Can be ignored
  38.  
  39. pinMode(Pin13LED, OUTPUT);
  40. pinMode(SSerialTxControl, OUTPUT);
  41.  
  42. digitalWrite(SSerialTxControl, RS485Receive); // Init Transceiver
  43.  
  44. // Start the software serial port, to another device
  45. RS485Serial.begin(4800); // set the data rate
  46. }//--(end setup )---
  47.  
  48.  
  49. void loop() /****** LOOP: RUNS CONSTANTLY ******/
  50. {
  51. //Copy input data to output
  52. if (RS485Serial.available())
  53. {
  54. byteSend = RS485Serial.read(); // Read the byte
  55. Serial.print(byteSend);
  56. digitalWrite(Pin13LED, HIGH); // Show activity
  57. delay(10);
  58. digitalWrite(Pin13LED, LOW);
  59.  
  60. // digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
  61. // RS485Serial.write(byteSend); // Send the byte back
  62. delay(10);
  63. digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
  64. // delay(100);
  65. }// End If RS485SerialAvailable
  66. Serial.println();
  67. }//--(end main loop )---
  68.  
  69. /*-----( Declare User-written Functions )-----*/
  70. //NONE
  71.  
  72. //*********( THE END )***********
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement