@@ -115,7 +115,7 @@ uint32_t modbusMaster::getCommandTimeout() {
115115}
116116
117117void modbusMaster::setFrameTimeout (uint32_t timeout) {
118- _stream->setTimeout (timeout);
118+ if ( _stream != nullptr ) { _stream ->setTimeout (timeout); }
119119 modbusFrameTimeout = timeout;
120120}
121121uint32_t modbusMaster::getFrameTimeout () {
@@ -131,11 +131,11 @@ uint8_t modbusMaster::getCommandRetries() {
131131
132132void modbusMaster::setStream (Stream* stream) {
133133 _stream = stream;
134- _stream->setTimeout (modbusFrameTimeout);
134+ if ( _stream != nullptr ) { _stream ->setTimeout (modbusFrameTimeout); }
135135}
136136void modbusMaster::setStream (Stream& stream) {
137137 _stream = &stream;
138- _stream->setTimeout (modbusFrameTimeout);
138+ if ( _stream != nullptr ) { _stream ->setTimeout (modbusFrameTimeout); }
139139}
140140Stream* modbusMaster::getStream () {
141141 return _stream;
@@ -1015,6 +1015,12 @@ bool modbusMaster::setCoils(int16_t startCoil, int16_t numCoils, byte* value) {
10151015
10161016// This sends a command to the sensor bus and listens for a response
10171017uint16_t modbusMaster::sendCommand (byte* command, int commandLength) {
1018+ if (_stream == nullptr ) {
1019+ debugPrint (" Modbus Error: No Stream Defined!\n " );
1020+ lastError = NO_RESPONSE ;
1021+ return static_cast <uint16_t >(lastError) << 12 ;
1022+ }
1023+
10181024 // Empty the response buffer
10191025 memset (responseBuffer, 0x00 , RESPONSE_BUFFER_SIZE );
10201026
0 commit comments