Advertisement
RuiViana

Teste_ModBus_Master

Feb 24th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. /* YourDuino SoftwareSerialExample1
  2. - Connect to another Arduino running "YD_SoftwareSerialExampleRS485_1Remote"
  3. - Connect this unit Pins 10, 11, Gnd
  4. - Pin 3 used for RS485 direction control
  5. - To other unit Pins 11,10, Gnd (Cross over)
  6. - Open Serial Monitor, type in top window.
  7. - Should see same characters echoed back from remote Arduino
  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.  
  18. #define SSerialTxControl 3 //RS485 Direction control
  19.  
  20. #define RS485Transmit HIGH
  21. #define RS485Receive LOW
  22.  
  23. #define Pin13LED 13
  24.  
  25. /*-----( Declare objects )-----*/
  26. SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
  27.  
  28. /*-----( Declare Variables )-----*/
  29. int byteReceived;
  30. int byteSend = 1234;
  31.  
  32. void setup() /****** SETUP: RUNS ONCE ******/
  33. {
  34. // Start the built-in serial port, probably to Serial Monitor
  35. Serial.begin(9600);
  36. Serial.println("YourDuino.com SoftwareSerial remote loop example");
  37. Serial.println("Use Serial Monitor, type in upper window, ENTER");
  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.  
  47. }//--(end setup )---
  48.  
  49.  
  50. void loop() /****** LOOP: RUNS CONSTANTLY ******/
  51. {
  52. digitalWrite(Pin13LED, HIGH); // Show activity
  53. // if (Serial.available())
  54. // while (Serial.available())
  55. // {
  56. // byteReceived = Serial.read();
  57. // byteSend = Serial.read();
  58. digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
  59. RS485Serial.write(byteSend); // Send byte to Remote Arduino
  60.  
  61. // digitalWrite(Pin13LED, LOW); // Show activity
  62. // delay(10);
  63. digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
  64. delay(10);
  65. digitalWrite(Pin13LED, LOW); // Show activity
  66. Serial.print(byteSend);
  67. // }
  68. /* if (RS485Serial.available()) //Look for data from other Arduino
  69. {
  70. digitalWrite(Pin13LED, HIGH); // Show activity
  71. byteReceived = RS485Serial.read(); // Read received byte
  72. Serial.write(byteReceived); // Show on Serial Monitor
  73. delay(10);
  74. digitalWrite(Pin13LED, LOW); // Show activity
  75. }
  76. */
  77. }//--(end main loop )---
  78.  
  79. /*-----( Declare User-written Functions )-----*/
  80.  
  81. //NONE
  82. //*********( THE END )***********
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement