Xenon UART - Sensor1.write() not working

xenon
Tags: #<Tag:0x00007fe21f201558>

#1

I am trying to write over the TX pin and when I try to verify the Serial1.write through my console, it does not work.

Specifically Serial1.available() returns false.

Any ideas?

int boardLed = D7;

void setup() {
    
  pinMode(boardLed, OUTPUT);
  Serial.begin(9600);
  Serial1.begin(115200);
  Serial1.println("Hello Serial 1");
}

void loop() {
  if (Serial1.available()) {
      delay(2000);
      digitalWrite(boardLed, HIGH);
      Serial1.write("Xenon");
      Serial1.println("Xenon fired");
      delay(2000);
      digitalWrite(boardLed, LOW);
  }
  else {
      Serial.println("Not working");
      delay(4000);
  }
}

#2

The Serial1.available() will only return true (not zero) if there are bytes in the Serial1 buffer. In this case, you are then writing if true. The following is from the reference documentation for Serial.available();

Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer.

The receive buffer size for hardware serial channels (Serial1, Serial2) is 128 bytes and cannot be changed.

Did you mean to use Serial.availableForWrite()? Alternatively, are you typing a character on the serial monitor to have the LED on for 2 seconds?


#3

Thanks for the reply. I’ve updated the script, however I’m still not getting any printed lines from Serial1. So from what I can tell

  • Serial1.availableForWrite() passes
  • However it does not println ‘Xenon fired’ or even ‘Hello Serial 1’ in the setup()

particle serial monitor returns:

Opening serial monitor for com port: "/dev/tty.usbmodem144101"
Serial monitor opened successfully:

Here is the updated script-

void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);
  Serial1.println("Hello Serial 1");
}

void loop() {
  Serial1.println("Xenon fired 1");
  if (Serial1.availableForWrite()) {
    Serial1.println("Xenon fired 2");
    Serial1.write("Xenon");
    Serial1.println("Xenon fired 3");
    delay(4000);
  }
  else {
    Serial.println("Not working");
    delay(4000);
  }
}

#4

How have you got your Serial1 (i.e TX and RX pins) connected to the com port?

Serial.begin(); will open the serial port via USB - after you open that you are not using it.


#5

I have had them connected to a Raspberry Pi, but with nothing coming across, I have disconnected them to have the bare Xenon, and am trying to debug through the CLI particle serial monitor

To simplify the script, I’ve trimmed it down to the following.

void setup() {
  Serial1.begin(115200);
  Serial1.println("Hello Serial 1");
}

void loop() {
  Serial1.println("Xenon fired 1");
}

My understanding of println is that it should print the lines out in the CLI under particle serial monitor. Correct? Since println has never fired under Serial1, I figured that is a symptom of why Serial1.write does not seem to work.

I can get Serial.println to print in the terminal, however Serial1.println does not.

As we work through this, I must be missing something about Serial1. I’m not sure what, but I have tried to strip away as any possible places for an issue to pop up, and it still is not working as I expected.


#6

Maybe you RPi expects some flow control on RX/TX (Serial1) - which pins have you used on your RPi?

However, particle serial monitor will only apply to Serial not to Serial1


#7

I hate to admit how much time I have spent trying to debug this. Is there another way to monitor Serial1.println?

I am using the pins 8 & 10.


#8

Do you have Serial1.RX connected to RPi.TX, and Serial1.TX connected to RPi.RX? it’s a simple mistake I make time and again…

Second thing, I’ve had trouble with Serial1.println on the Boron and I fixed it by using Serial1.write()

Last thing, I like of vectors because they have some easy array handling methods, so my example uses vectors. Hopefully it makes sense, heres and example I pulled from one of my programs:

#include <vector>
void sendSomeDataToSerial1(std::vector<int> message) {
    bool debugLogging = true;

    //prints bytes to console if global debug logging flag is true.
    if (debugLogging) {
        Serial.print("Sending Serial1Data: [");
    }
    
    int serialDataSize = message.size();
    byte serialByteMessage[serialDataSize];
    int i = 0;
    // Make buffered array of the message
    for(int data : message) {
        
        serialByteMessage[i]=data;

        if (debugLogging) {
            Serial.print(data, HEX);
            Serial.print(", ");
        }
        i++;
    }

    if (debugLogging) {
        Serial.println("]");
    }
    if(debugLogging){
        Serial.print("writing serial data");
    }
    Serial1.write(serialByteMessage, serialDataSize);
    messageSent = true;
    
    delay(100);
    return messageSent;
}

#9

Yep :+1:

To be honest, this is the real issue. Serial1.write() is not sending anything to the Pi.

Is there a way to see the Serial1.write() in the CLI like serial monitor?


#10

You could connect Tx to Rx on the Xenon and then in the loop check Serial1.available() and if bytes available then read them and write to Serial. That way you will be able to see what is being written to Serial1 (Tx pin).


#11

ultimately if you really want to get to the bottom of it and oscilloscope is the way to go.

But you could loopback like @armor suggested. Connect serial1 RX to TX on your Xenon. Then make a program that sends what you type in the console to Serial1 and echos what’s received back on the console… You can type in a terminal and see if it get’s echoed back. here’s an arduino example:

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

#12

Finally got it working. Thanks everyone for your help.

Here is the simple script I am using -

int incomingByte = 0;

void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);

}

void loop()
{
  Serial1.print("hi");
  incomingByte = Serial1.read();
  Serial.println(incomingByte);
  delay(2000);
}

On my Pi:

ser = serial.Serial("/dev/serial0", 9600, timeout=0.5)
while True:
    x = ser.read(10)
    print(x.decode())

The key issue was using Serial1.write() should have been Serial1.print()


Raspberry Pi as gateway
#13

glad you got it fixed. I know what the difference is between those, but I wonder why it mattered in this case. Something should have still come through…


#14

I have a sneaking suspicion that the default tty settings in debian are 7bit and not 8 bit which may well be the culprit