Teensy 4.1 Printing Carbage values

Status
Not open for further replies.

Mr.Kara

Member
Hi

I am trying to transmit the data via Moxa AP and Client with the help of Teensy 4.1 Nativeethernet and Arduino UNO shield. I am getting some carbage value on server_receiver serial monitor. Please help me.


Carbage value.jpgView attachment Teensy Forum.zip
 
Looks like binary data is received and not converted to printable form ... but

How can anyone help without knowing anything about the code you are actually running, or what dta you are supposedly transmitting. There is no way to repeat your results form the description to test what is going on.

Forum rules are: post complete code and precise description of hardware and peripheral set up, and how it is used.
 
Looks like there is code here, in a ZIP file.

This appears to be the code on the Teensy 4.1.

Code:
#include <NativeEthernet.h>
  

//#include <SPI.h>
//#include <NativeEthernet.h>
unsigned long Position = 0, Facedelay = 0;
int currentposition = 0, pos = 0, linear = 0;
int i = 0, servo1 = 9, servo2 = 10;
bool mode_ = 0;
byte mac[] = {
  0x04, 0xE9, 0xE5, 0x0C, 0xA4, 0x27
};
IPAddress ip(192, 168, 127, 177);

EthernetServer server(80);

void setup() {
  pinMode(servo1, OUTPUT);// put your setup code here, to run once:
  pinMode(servo2, OUTPUT);
  digitalWrite(servo2, LOW);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  //while (!Serial) {
  //; // wait for serial port to connect. Needed for native USB port only
  //}
  //Serial.println("Ethernet WebServer Example");

  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    //Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }
  if (Ethernet.linkStatus() == LinkOFF) {
    //Serial.println("Ethernet cable is not connected.");
  }

  // start the server
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}
void forward()
{
  digitalWrite(servo2, LOW);
  for (i = 0; i <= pos; i++)
  {
    digitalWrite(servo1, HIGH);
    delayMicroseconds(500);
    digitalWrite(servo1, LOW);
    delayMicroseconds(500);
  }
}


void reverse()
{
  digitalWrite(servo2, HIGH);
  for (i = 0; i <= pos; i++)
  {
    digitalWrite(servo1, HIGH);
    delayMicroseconds(500);
    digitalWrite(servo1, LOW);
    delayMicroseconds(500);
  }
}
void loop() {
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    client.println("Ready");
    while (client.connected()) {
      if (client.available()) {
        String msg = client.readString();
        Serial.print(msg);
      }
      if (Serial.available()) {
        String cmd = Serial.readString();
        client.print(cmd);
      }
    }
  }
}

And I'm guessing this is the code running on Arduino Uno

Code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x04, 0xE9, 0xE5, 0x0C, 0xA4, 0x26 };
IPAddress server(192, 168, 127, 177);
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 127, 150);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac,ip);
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.print("connecting to ");
  Serial.print(server);
  Serial.println("...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.print("connected to ");
    Serial.println(client.remoteIP());
    client.println("Ready");
    client.println();
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  beginMicros = micros();
}

void loop() {
  // if there are incoming bytes available
  // from the server, read them and print them:
  while (client.connected()) {
    if (client.available()) {
      String msg;
      msg = client.readString();
      Serial.print(msg);
    }
    if (Serial.available()) {
      String cmd;
      cmd = Serial.readString();
      delayMicroseconds(1);
      client.print(cmd);
      Serial.print(cmd);
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    if (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      // Check for Ethernet hardware present
      if (Ethernet.hardwareStatus() == EthernetNoHardware) {
        Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
        while (true) {
          delay(1); // do nothing, no point running without Ethernet hardware
        }
      }
      if (Ethernet.linkStatus() == LinkOFF) {
        Serial.println("Ethernet cable is not connected.");
      }
      // try to congifure using IP address instead of DHCP:
      Ethernet.begin(mac, ip);
    }
    // give the Ethernet shield a second to initialize:
    delay(1000);
    Serial.print("connecting to ");
    Serial.print(server);
    Serial.println("...");
    // if you get a connection, report back via serial:
    if (client.connect(server, 80)) {
      Serial.print("connected to ");
      Serial.println(client.remoteIP());
      client.println("Ready");
      client.println();
    }
    endMicros = micros();
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    Serial.print("Received ");
    Serial.print(byteCount);
    Serial.print(" bytes in ");
    float seconds = (float)(endMicros - beginMicros) / 1000000.0;
    Serial.print(seconds, 4);
    float rate = (float)byteCount / seconds / 1000.0;
    Serial.print(", rate = ");
    Serial.print(rate);
    Serial.print(" kbytes/second");
    Serial.println();

    // do nothing forevermore:
    while (true) {
      delay(1);
    }
  }
}
 
I don't know what a "Moxa AP" is. This page on the Moxa website shows 4 products. I'm not familiar with any of these, or how they would fit into this... or if my quick searching even found the relevant info.

But from the screenshot, this looks like a classic case of baud rate mismatch. My guess is it's happening with this code on the Arduino Uno.

Code:
    if (Serial.available()) {
      String cmd;
      cmd = [B]Serial.readString();[/B]  // <--- incoming data probably is not 9600 baud
      delayMicroseconds(1);
      client.print(cmd);
      Serial.print(cmd);
    }

I have no idea what hardware is connected to that Arduino Uno which sends it data. Maybe it's just another PC or even a different terminal emulator running on the same PC? Nothing other than "Moxa AP" was mentioned in msg #1, but from this code, obviously *something* is transmitting data to Uno's serial RX pin.

Anyway, my guess is whatever is sending data to the serial RX pin on Uno isn't using 9600 baud. Uno is probably reading at the wrong speed and getting messed up data with Serial.readString(), which Uno transmits to Teensy 4.1, where it shows up as the wrong info.
 
I don't know what a "Moxa AP" is. This page on the Moxa website shows 4 products. I'm not familiar with any of these, or how they would fit into this... or if my quick searching even found the relevant info.

But from the screenshot, this looks like a classic case of baud rate mismatch. My guess is it's happening with this code on the Arduino Uno.

Code:
    if (Serial.available()) {
      String cmd;
      cmd = [B]Serial.readString();[/B]  // <--- incoming data probably is not 9600 baud
      delayMicroseconds(1);
      client.print(cmd);
      Serial.print(cmd);
    }

I have no idea what hardware is connected to that Arduino Uno which sends it data. Maybe it's just another PC or even a different terminal emulator running on the same PC? Nothing other than "Moxa AP" was mentioned in msg #1, but from this code, obviously *something* is transmitting data to Uno's serial RX pin.

Anyway, my guess is whatever is sending data to the serial RX pin on Uno isn't using 9600 baud. Uno is probably reading at the wrong speed and getting messed up data with Serial.readString(), which Uno transmits to Teensy 4.1, where it shows up as the wrong info.

MOXA AP is a kind of wireless router. One side Arduino Uno reads the data from serial monitor and transmits to router via Ethernet shield, other side Teensy reads the data from router via Ethernet and displays it in serial monitor. It's happening vice versa.
 
I believe you should test without the Moxa products. If the same error happens with both plugged into the same LAN, that is much easier to reproduce and investigate.

If the error only happens with Moxa, perhaps it is not the fault of either Uno or Teensy, but some sort of problem with Moxa?
 
Status
Not open for further replies.
Back
Top