2424 *
2525 */
2626
27- PelcoCam::PelcoCam (uint8_t Address, int baud, int txPin, int rxPin, bool log_messages=false ){
27+ PelcoCam::PelcoCam (uint8_t Address, int baud, int txPin, int rxPin, int readEnPin=NOT_A_PIN, bool log_messages=false ){
2828 Address_ = Address;
2929 baud_ = baud;
3030 txPin_ = txPin;
3131 rxPin_ = rxPin;
32+ rePin_ = readEnPin;
3233 log_messages_ = log_messages;
33-
3434}
3535
3636/* !
@@ -44,6 +44,22 @@ void PelcoCam::begin(){
4444 Serial.println (" Message log has been activated!" );
4545 }
4646
47+
48+ if (rePin_ == NOT_A_PIN){
49+ autoModule_ = false ; // Is the module an auto switching between tx and RX ?
50+ }else {
51+ pinMode (rePin_, OUTPUT);
52+ autoModule_ = true ;
53+ }
54+
55+ pinMode (txPin_, OUTPUT);
56+ pinMode (rxPin_, INPUT);
57+
58+
59+ if (autoModule_) digitalWrite (rePin_, HIGH); // Set the module in tx mode
60+
61+
62+
4763 SerialCam.begin (baud_, SWSERIAL_8N1, rxPin_, txPin_);
4864}
4965
@@ -53,10 +69,11 @@ void PelcoCam::begin(){
5369 * @param command the wanted command (see header)
5470 * @param params Main parameter
5571 * @param params2 Second parameter for command that requires 2 parameters
72+ * @param params2 Second parameter for command that requires 2 parameters
5673 *
5774 */
5875
59- void PelcoCam::send_command (uint8_t command, uint8_t params, uint8_t params2){
76+ void PelcoCam::send_command (uint8_t command, uint8_t params, uint8_t params2, bool request ){
6077 messToCamera[0 ] = 0xFF ;
6178 messToCamera[1 ] = Address_;
6279
@@ -91,13 +108,14 @@ void PelcoCam::send_command(uint8_t command, uint8_t params, uint8_t params2){
91108 }
92109
93110 SerialCam.write (messToCamera, sizeof (messToCamera));
111+ if (!request) delay (10 ); // delay because the camera respond back TODO: check reponse and delete the relay
94112}
95113
96114/* !
97115 * @brief Send a query to the camera and reads the response
98116 *
99117 * @param request the wanted reponse (see header)
100- * @param timeout default 1000; timout for waiting a byte
118+ * @param timeout default 1000; timeout for waiting a byte
101119 * @param maxbuffer Maximum size of the buffer; defaut 20
102120 *
103121 * @return true if succeded, false if an error occured
@@ -115,24 +133,36 @@ int PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer){
115133 return -1 ;
116134 }
117135
118- send_command (request); // Send the query
136+ send_command (request, 0x00 , 0x00 , true ); // Send the query
137+
138+ if (autoModule_) digitalWrite (rePin_, LOW); // Set the module at RX mode
139+
140+ byte buffer[maxbuffer]; // Buffer for the reception from the camera
141+ uint index = 0 ;
142+
119143
120- while (!SerialCam.available ()){// Wait for the first bit
144+ while (!SerialCam.available ()){// Wait for the first bit
121145 if (timeout==0 ){ // If timeout is reached
122- if (log_messages_) Serial.print (" ERROR timout reached" );
146+ if (log_messages_) Serial.println (" ERROR timout reached" );
147+ if (autoModule_) digitalWrite (rePin_, HIGH); // set back at TX mode
123148 return -1 ;
124149 }
125150 timeout--;
126151 delayMicroseconds (10 );
127152 }
128153
129- byte buffer[maxbuffer]; // Buffer for the reception from the camera
130- uint index = 0 ;
131-
132- while (SerialCam.available ()){// Do it until there is no more things to read
154+ // ////Old solution
155+ /* while(SerialCam.available()){//Do it until there is no more things to read
133156 buffer[index]=SerialCam.read();
157+ Serial.printf("%02X ",buffer[index]);
134158 index++;
135- }
159+ } */
160+
161+ SerialCam.readBytes (buffer, 7 );// Apparently this works
162+
163+ if (autoModule_) digitalWrite (rePin_, HIGH); // set back at TX mode
164+
165+ int command_index = searchIndex (buffer, 0x59 );// Looks up where is the index of the response command
136166
137167 /* A theorical response:
138168 FF 00 59 4A 13 B7
@@ -142,8 +172,6 @@ int PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer){
142172 FF 00 FF 00 59 4A 13 B7 FF 01 48
143173 */
144174
145- int command_index = searchIndex (buffer, 0x59 );// Looks up where is the index of the response command
146-
147175 if (command_index == -1 // Checks if found
148176 || command_index < 3 // Checks if the reponse byte is in the right place
149177 ){
@@ -173,9 +201,14 @@ int PelcoCam::send_request(uint8_t request, uint timeout, uint maxbuffer){
173201 Serial.println ();
174202 }
175203
176- return messFromcamera[4 ]; // Returns MSB
204+
205+
206+ return messFromcamera[4 ]; // Return MSB data
177207}
178208
209+
210+
211+
179212void PelcoCam::send_raw (String hex_string){
180213 hex_string.replace (" " , " " ); // Replace spaces
181214
0 commit comments