Why does SDIserial interrupt not work well the interrupt of rotary encoder

Hi, folks,
I’m working on SDIserial library which should use interrupt. In the code, I also write for rotary encoder which also use interrupt to count pulses, please check the code below and library attached.
I hope someone can help me check out why rotary encoder can not work well woth SDIserial.
Merry Christmas.

#include <SDISerial.h>
 
  volatile int pulses=0;      //pulses of rotary encoder
  int A_SIG=0;
  int B_SIG=1;

//in order to recieve data you must choose a pin that supports interupts
#define DATALINE_PIN 10      //MEGA
#define INVERTED 1

SDISerial sdi_serial_connection(DATALINE_PIN, INVERTED);

char* get_measurement(){
 char* service_request = sdi_serial_connection.sdi_query("?M!",1000);
 //you can use the time returned above to wait for the service_request_complete
 char* service_request_complete = sdi_serial_connection.wait_for_response(1000);
 //dont worry about waiting too long it will return once it gets a response
 return sdi_serial_connection.sdi_query("?D0!",1000);
}

void setup(){
  sdi_serial_connection.begin(); // start our SDI connection 
  Serial.begin(9600); // start our uart
  attachInterrupt(0,A_RISE,RISING);
  attachInterrupt(1,B_FALL,FALLING);
}

void loop(){

  char* response = get_measurement(); // get measurement data
  Serial.print("RECV:");Serial.println(response!=NULL&&response[0] != '\0'?response:"No Response!"); //just a debug print statement to the serial port
  Serial.println(pulses);
}

  void A_RISE()
  {
  detachInterrupt(0);
  A_SIG=1;
  if(B_SIG==0)
  {pulses--;}
  if(B_SIG==1)
  {pulses++;}
  attachInterrupt(0,A_FALL,FALLING);
  }
  
  void A_FALL()
  {
  detachInterrupt(0);
  A_SIG=0;
  if(B_SIG==1)
  {pulses--;}
  if(B_SIG==0)
  {pulses++;}
  attachInterrupt(0,A_RISE,RISING);
  }
  
  void B_RISE()
  {
  detachInterrupt(1);
  B_SIG=1;
  if(A_SIG==1)
  {pulses--;}
  if(A_SIG==0)
  {pulses++;}
  attachInterrupt(1,B_FALL,FALLING);
  }
  
  void B_FALL()
  {
  detachInterrupt(1);
  B_SIG=0;
  if(A_SIG==0)
  {pulses--;}
  if(A_SIG==1)
  {pulses++;}
  attachInterrupt(1,B_RISE,RISING);
  }

SDISerial.zip (687 KB)

By the way, when I only put these code in main code, it doesn’t effect the working of encoder, However, when I put" sdi_serial_connection.begin(); " into void setup(), the rotary encoder can’t work.

#include <SDISerial.h>

//in order to recieve data you must choose a pin that supports interupts
#define DATALINE_PIN 10      //MEGA
#define INVERTED 1

SDISerial sdi_serial_connection(DATALINE_PIN, INVERTED);

char* get_measurement(){
 char* service_request = sdi_serial_connection.sdi_query("?M!",1000);
 //you can use the time returned above to wait for the service_request_complete
 char* service_request_complete = sdi_serial_connection.wait_for_response(1000);
 //dont worry about waiting too long it will return once it gets a response
 return sdi_serial_connection.sdi_query("?D0!",1000);
}

would someone like to help me with this problem? thank you very much.

Your "SDISerial" is more or less a copy of the SoftwareSerial library included in the standard IDE. Unfortunately SoftwareSerial is not the smartest peace of code. During reception or transfer of data over the emulated serial interface all interrupts are blocked. What do you need SDISerial for?

Hi,pylon,
Thanks for your kind reply. I use SDIserial for obtaining data from 5TE (a soil water content sensor produced by Decagon). Now I need 5TE to work with rotary encoder which also use interruption. Could you give me some advices on how to make these two sensors work well together?
Hope to hear from you soon.
Merry Christmas
Jingbo

What type of rotary encoder are you using, and what speed is it turning? You may be able to read the encoder using polling rather than interrupts.

Hi, cattledog,
Thanks for your kind reply. I'm using rotary encoder which type is 667-EVE-GC2F2524M. It has 24 steps for each round. For the speed you mentioned, it can only be turned by hand or other object. it can't turn automatically. For this type of encoder, can I use polling to count its step?
Best
Jingbo

For hand turned, mechanical encoders like the 667-EVE-GC2F2524M you do not need interrupts to read them. Polling should work fine.

Depending on the encoder and what you are doing with the counts, you may need to add some switch debounce code into your reading.

Thank you very much, I will try your method and then I will let you know if it works well.

Merry Christmas
Jingbo