0

I'm trying to use softwareserial to exchange datas with a sensor. After some tries without any answer from the sensor, I checked the arduino TX with my scope and what I've seen is not what I've sended.

My original byte array (before the setup function):

static byte simple_write[] = {0xfe, 0x41, 0x00, 0x80, 0x01, 0x10, 0x28, 0x7e};

What I got on my scope:

0X77 , 0x61 , 0x69 , 0x74 , ...

Every single sent byte is wrong. I don't understand why. The only thing on the way is a bridge divider to reduce 5V TTL from the arduino to 3,3V CMOS.

If someone have an idea, thanks in advance.

EDIT2: I use IS.MDuino.21+ (ATmega2560)

EDIT: Here are the parts of my code who involve the softwareserial

    #include "SoftwareSerial.h"

    SoftwareSerial LP8_Serial(1, 0);

    static byte simple_write[] = {0xfe, 0x41, 0x00, 0x80, 0x01, 0x10, 0x28, 0x7e}; //Write 0x010 to address 0x0080

void setup(){
LP8_Serial.begin(9600); //Opens the virtual serial port with baud of 9600
}

void loop(){

LP8_val1 = GetDatas_uart();

}

int GetDatas_uart() {
  if (first_loop == 0) {
    //reset_device();  // includes a 500 msec delay
    sendRequest(simple_write, 8, 4); // send to address 0x0080 a 0x10
    first_loop = 1;
  }
  else
    sendRequest(write_to_0x20, 8, 4); // send to address 0x0080 a 0x10
  delay(2000);
  unsigned long valCO2 = getValue(response);
  delay(3000);
  Serial.println("Now reading 32 bytes");   //sendRequest(read_16_bytes,7,21);
  sendRequest(read_32_bytes, 7, 37);
  Serial.print("CO2 from 0x9a-0x9b = ");
  Serial.print(response[29], HEX);
  Serial.print(response[30], HEX);
  Serial.print(" = ");
  int decimal = 256 * response[29] + response[30];
  Serial.print(decimal);
  Serial.println("d");

  return decimal;
}

void sendRequest(byte packet[], int m, int n)
{
  while (!LP8_Serial.available()) //keep sending request until we start to get a response
  {
    Serial.println("waiting for Software.serial port availability");
    LP8_Serial.write(packet, m);
    //LP8_Serial.write(simple_write, m);
    delay(1000); // Necessary to get consistent loading of response[i]
  }
  int timeout = 0; //set a timeout counter
  while (LP8_Serial.available() < n ) //Wait to get a n byte response
  {
    timeout++;
    if (timeout > 10) //if it takes too long there was probably an error
    {
      while (LP8_Serial.available()) //flush whatever we have
        LP8_Serial.read();
      break; //exit and try again
    }
    delay(50);
  }
  for (int i = 0; i < n; i++)
  {
    response[i] = LP8_Serial.read();
    //    Serial.print("response[i] = ");
    Serial.print("response[");
    Serial.print(i);
    Serial.print("] = ");
    Serial.println(response[i], HEX);
  }
  Serial.print("\n\n");
  Serial.flush();
}
5
  • Please show us your code. How did you analyse the scope data? Through a function of the scope? Are you sure that is configured correctly? Commented Aug 20, 2021 at 11:50
  • The code is pretty big. It's the only function who don't work. I will add the concerned parts in the original message. But yes, it's through a function of my scope. Commented Aug 20, 2021 at 11:51
  • Then please create a smaller working example code, that shows your problem, by cutting away code, that does not have something to do with the SoftwareSerial code Commented Aug 20, 2021 at 12:10
  • @chrisl, it is ATmega2560 and OP uses SoftwareSerial on hardware Serial pins (on Mega!) Commented Aug 20, 2021 at 12:36
  • So than this answer might be fitting here Commented Aug 20, 2021 at 18:22

1 Answer 1

2

Sooo, I feel a little ashamed...

After re-reading the documentation of the Arduino from Industrial Shields I use (model 21+), I discovered that using Serial0 on this board is unadvised because these pins are shared with USB.

I connected my serial wires to the Serial1 connection and now, my scope show me exactly what I wanted to see on my TX line.

Sorry for that. Like often, the problem whas between the screen and the chair.

Thanks,

5
  • so you used SoftwareSerial on hardware serial pins? Arduino Mega has 4 hardware Serials. don't use SoftwareSerial library with Arduibo Mega Commented Aug 20, 2021 at 12:32
  • Yes. I did. When I use Arduino, I rarely bother to use hardware serial except for debugging. I needed a "ready to use" electronic system for a project. Commented Aug 20, 2021 at 12:39
  • I don't meant an external USB-to-TLL-Serial, but the on chip UART peripheral Commented Aug 20, 2021 at 12:41
  • This module (Industrial Shields M21+) provide only TTL and RS232 level serials. Moreover, I wanted to go fast with a new sensor and someone wrote an arduino library for it. The example use a softwareserial, so basicaly "Copy / Paste / Check". Commented Aug 20, 2021 at 12:44
  • 1
    and then quick to Stack Exchange Commented Aug 20, 2021 at 12:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.