Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 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.  
  18. #define SSerialTxControl 3 //RS485 Direction control
  19. #define RS485Transmit HIGH
  20. #define RS485Receive LOW
  21.  
  22. #define Pin13LED 13
  23.  
  24. /*-----( Declare objects )-----*/
  25. SoftwareSerial RS485Serial(SSerialRX, SSerialTX); // RX, TX
  26.  
  27. /*-----( Declare Variables )-----*/
  28. int byteReceived;
  29. int byteSend;
  30. String byteConvert;
  31. char byteChar;
  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. bool maciej=0;
  53. if (RS485Serial.available())
  54. {
  55. byteSend = RS485Serial.read(); // Read the byte
  56.  
  57. if (byteSend > 0){
  58. Serial.print(char(byteSend));
  59. maciej=1;
  60. }
  61. else if(maciej==1)
  62. {
  63. Serial.println("");
  64. maciej=0;
  65. }
  66.  
  67. digitalWrite(Pin13LED, HIGH); // Show activity
  68. delay(10);
  69. digitalWrite(Pin13LED, LOW);
  70.  
  71.  
  72. digitalWrite(SSerialTxControl, RS485Transmit); // Enable RS485 Transmit
  73. RS485Serial.write(byteSend); // Send the byte back
  74. delay(10);
  75. digitalWrite(SSerialTxControl, RS485Receive); // Disable RS485 Transmit
  76. // delay(100);
  77. }// End If RS485SerialAvailable
  78.  
  79. }//--(end main loop )---
  80.  
  81. /*-----( Declare User-written Functions )-----*/
  82. //NONE
  83.  
  84. //*********( THE END )***********
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement