Hardware Serial will not work unless I type in Serial Monitor

Air_Born

New member
Hello everyone,

I am pretty new at this, and I could really use your help. I have hooked up a 4 wire(Tx, Rx, Gnd, Vcc), IMU unit to my teensy 3.5, and using the IMU's library and Serial1 I try to stream the data onto the serial monitor. The problem is that when I start serial monitor, all i see is zeros, but as soon as I send anything in the serial monitor it starts showing me the values and its very responsive from there on. I don't know why this happens. I did also try this on an arduino Mega with it's hardware serial port and it did the same thing. But when I wired it to the shared serial port, this was not an issue. I have attached the code and the library below as well as the link to the IMU unit on amazon. The code below is a slightly simplified version of the example in the library. Thanks in advance!

This is a Chinese made IMU, model: WTGAHRS2
Link: https://www.amazon.ca/gp/product/B072ZZ83JZ/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1


Code:
#include <JY901.h>

void serialEvent() {
  while (Serial1.available()) 
  {
    JY901.CopeSerialData(Serial1.read()); //Call JY901 data cope function
  }
};
  
void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() 
{
 
  //print received data. Data was received in serialEvent;
  Serial.print((float)JY901.stcGyro.w[0]/32768*2000);
  Serial.print(",");
  Serial.print((float)JY901.stcGyro.w[1]/32768*2000);
  Serial.print(",");
  Serial.print((float)JY901.stcGyro.w[2]/32768*2000);
  Serial.print(",");
  Serial.print((float)JY901.stcAngle.Angle[0]/32768*180);
  Serial.print(",");
  Serial.print((float)JY901.stcAngle.Angle[1]/32768*180);
  Serial.print(",");
  Serial.println((float)JY901.stcAngle.Angle[2]/32768*180);
//  delay(100);

}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
 

Attachments

  • JY901.zip
    4.7 KB · Views: 18
The code you showed is missing the serial event function. Looks like you didn't copy the whole program before pasting it here into the forum.

I see the JY901 library has this in its example

Code:
void serialEvent()
{
  while (Serial.available())
  {
    JY901.CopeSerialData(Serial.read()); //Call JY901 data cope function
  }
}

If you use this code on Teensy with the device connected to Serial1, you must change 3 places in this code.

Code:
void [B][COLOR="#FF0000"]serialEvent1[/COLOR][/B]()
{
  while ([B][COLOR="#FF0000"]Serial1[/COLOR][/B].available())
  {
    JY901.CopeSerialData([B][COLOR="#FF0000"]Serial1[/COLOR][/B].read()); //Call JY901 data cope function
  }
}

From the problem you described, my blind guess is maybe you still have "serialEvent" rather than "serialEvent1" in the part of your program I can't seen, but you did change the other 2 places to Serial1. That would cause the code to process the Serial1 incoming data when something is received from your PC which runs serialEvent.
 
Back
Top