1

My arduino mega should be communicating with another card (ANET8) and also with a DC resistance meter (with python code) using 2 serial ports, one using the USB(resistance meter) and the other one with RX and TX.

The Anet8 is sending a signal to trigger the MeasGo part in the code, and therefore with the Serial.print(A8flag) it triggers a measurment part in python, that measurement part in python sends in the Serial port a command that the arduino should read through Serial.readString and thus execute a function called "MuxRelayChoicee".

My problem is that, the part with " if Serial.available()>0 ... " is not working. I know it because my debug_pin1 is not made to HIGH. Did i miss something ?

#include "pins.h"

#include <SoftwareSerial.h>

String flag, A8flag, sys;
int i;
int POW_RELAY_PIN=12;
int debug_pin = 38;
int debug_pin1 = 39;
SoftwareSerial mySerial(10, 11); // RX, TX


void setup() {
  // set the digital pin as output:
  pinMode(10, INPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);

  pinMode(debug_pin, OUTPUT);
  pinMode(debug_pin1, OUTPUT);

  Set_Pins(); //to activate mux pins
  
    // Open serial communications and wait for port to open:
  Serial.begin(9600);
//  Serial.setTimeout(500);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  
  mySerial.begin(9600);
//  mySerial.setTimeout(500);
  while (!mySerial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void loop() {
  
  if (mySerial.available()>0)
  {
     delay(50);
     String A8flag ="";
     A8flag=mySerial.readString();
     delay(50);

 if(A8flag.equals("MeasGO\n")){
     Serial.print(A8flag);
     delay(100);
     Serial.flush();
     digitalWrite(debug_pin, HIGH);
     
     if (Serial.available()>0)
     {
      
        String sys =""; 
        sys=Serial.readString();
        delay(50);
        digitalWrite(debug_pin1, HIGH);
        MuxRelayChoice(sys);
 }
 }

Here is a part of the code i send with python :

if flag=="MeasGO":
      for n in range(4): 
        ser.write(("sys"+str(n)+"\n").encode()) #Writing to the Serial port of Arduino 
        time.sleep(1)    
        data=mybk.query('FETCh?')
        time.sleep(1)
        t.append(time.time()-t0)
        print("sys"+str(n)+ '\n')
        res[n]=float(data[:13])*1000
        print(str(res[n])+'\t mOhm') 
      with open(path+'csvfile.csv','a') as f:
          f.write(str(time.time()-t0)[:7]+'\t'+str(res[0])+'\t'+str(res[1])+'\t'+str(res[2])+'\t'+str(res[3])+'\n') #Give your csv text here.
      time.sleep(0.5) 
      ser.write("MeasOK\n".encode())
4
  • Why are you using software serial, the Mega has 4 serial ports that work very nicely. Also to my knowledge the Softwareserial can only service 1 operating software serial channel at a time. – Gil Dec 1 '20 at 16:23
  • It seems likely that this code can be edited down to show the same problem using the Serial Monitor rather than python for send data and making no reference to AVET8 or MeaGo, whatever those are. If the problem is principally about everything leading up to enter a conditional statement the first time, the body of the conditional statement is unimportant to the problem. If you don't find the problem yourself in this process, you'll have code that most anyone here can test themselves. – timemage Dec 1 '20 at 19:58
  • If you're unable to completely remove the python code from the troubleshooting process (you likely can), you may be able to at least shorten it and remove all of the unnecessary dependencies yielding complete and short python source that can be run by anyone wanting to help you with your arduino code. – timemage Dec 1 '20 at 20:00
  • This has nothing to do with anything, but SoftwareSerial::begin() sets the correct pin modes for the RX and TX pin numbers given in the constructor. You needn't call pinMode for them yourself. Ignoring the constructor bit, the same is true for your available HardwareSerial objects. That can be removed from the code. – timemage Dec 1 '20 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.