Hi,
I have a task to establish simple UDP communication between teensy 4.1 dev board and PC via twisted pair cable (using a Magjack extension board, teensy if of course connected to PC via standard micro-USB cable for programming too). I am using Arduino IDE for programing and a QNEthernet library (Broadcast chat example.) Since I want my communication to be completely independent from the network (WAN) I dont want to use DHCP (and I basically can not, because I dont have a router at my work, the place I am developing all this. I only have a RJ45 ethernet socket routed to the desk, the network architecture beyond that is unknown for me...)
So after a bit of effort and experiments I have set static IP for both teensy and PC and also made sure that they are on the different subnet than the internet. My idea on testing the communication was to download a UDP client for windows, connect it to the same port with teensy and ideally the text I enter in UDP terminal should be received and seen in the serial monitor of teensy, and of course, the opposite way around. However, when I type anything to the teensy terminal it gives me error, when I type anything to the UDP terminal, it is not received and depicted in the teensy terminal...
My code is basically bare QNEthernet Broadcast chat example, I only omited the DHCP part and assigned the addresses manually, other than that, no mods were made... Do you have any observations, why this could be not working? Did I miss something else?
I have a task to establish simple UDP communication between teensy 4.1 dev board and PC via twisted pair cable (using a Magjack extension board, teensy if of course connected to PC via standard micro-USB cable for programming too). I am using Arduino IDE for programing and a QNEthernet library (Broadcast chat example.) Since I want my communication to be completely independent from the network (WAN) I dont want to use DHCP (and I basically can not, because I dont have a router at my work, the place I am developing all this. I only have a RJ45 ethernet socket routed to the desk, the network architecture beyond that is unknown for me...)
So after a bit of effort and experiments I have set static IP for both teensy and PC and also made sure that they are on the different subnet than the internet. My idea on testing the communication was to download a UDP client for windows, connect it to the same port with teensy and ideally the text I enter in UDP terminal should be received and seen in the serial monitor of teensy, and of course, the opposite way around. However, when I type anything to the teensy terminal it gives me error, when I type anything to the UDP terminal, it is not received and depicted in the teensy terminal...
My code is basically bare QNEthernet Broadcast chat example, I only omited the DHCP part and assigned the addresses manually, other than that, no mods were made... Do you have any observations, why this could be not working? Did I miss something else?
C:
// SPDX-FileCopyrightText: (c) 2023 Shawn Silverman <shawn@pobox.com>
// SPDX-License-Identifier: MIT
// BroadcastChat is a simple chat application that broadcasts and
// receives text messages over UDP.
//
// This file is part of the QNEthernet library.
#include <QNEthernet.h>
using namespace qindesign::network;
// --------------------------------------------------------------------------
// Configuration
// --------------------------------------------------------------------------
//constexpr uint32_t kDHCPTimeout = 10'000; // 10 seconds
constexpr uint16_t kPort = 5190; // Chat port
// --------------------------------------------------------------------------
// Program State
// --------------------------------------------------------------------------
// UDP port.
EthernetUDP udp;
// --------------------------------------------------------------------------
// Main Program
// --------------------------------------------------------------------------
// Forward declarations (not really needed in the Arduino environment)
static void printPrompt();
static void receivePacket();
static void sendLine();
// Program setup.
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 4000) {
// Wait for Serial
}
printf("Starting...\r\n");
uint8_t mac[6];
Ethernet.macAddress(mac); // This is informative; it retrieves, not sets
printf("MAC = %02x:%02x:%02x:%02x:%02x:%02x\r\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Ethernet.onLinkState([](bool state) {
printf("[Ethernet] Link %s\r\n", state ? "ON" : "OFF");
});
printf("Starting Ethernet with DHCP...\r\n");
if (!Ethernet.begin()) {
printf("Failed to start Ethernet\r\n");
return;
}
/*
if (!Ethernet.waitForLocalIP(kDHCPTimeout)) {
printf("Failed to get IP address from DHCP\r\n");
return;
}
*/
//IPAddress ip =Ethernet.localIP();
IPAddress ip = {10, 10, 16, 111};
printf(" Local IP = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]);
ip = {255, 255, 255, 0}; //Ethernet.subnetMask();
printf(" Subnet mask = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]);
ip = {255, 255, 255, 255}; //Ethernet.broadcastIP();
printf(" Broadcast IP = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]);
ip = {10, 10, 10, 254}; //Ethernet.gatewayIP();
printf(" Gateway = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]);
ip = {10, 10, 10, 244}; //Ethernet.dnsServerIP();
printf(" DNS = %u.%u.%u.%u\r\n", ip[0], ip[1], ip[2], ip[3]);
// Start UDP listening on the port
udp.begin(kPort);
printPrompt();
}
// Main program loop.
void loop() {
receivePacket();
sendLine();
}
// --------------------------------------------------------------------------
// Internal Functions
// --------------------------------------------------------------------------
// Control character names.
static const String kCtrlNames[]{
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US",
};
// Receives and prints chat packets.
static void receivePacket() {
int size = udp.parsePacket();
if (size < 0) {
return;
}
// Get the packet data and remote address
const uint8_t *data = udp.data();
IPAddress ip = udp.remoteIP();
printf("[%u.%u.%u.%u][%d] ", ip[0], ip[1], ip[2], ip[3], size);
// Print each character
for (int i = 0; i < size; i++) {
uint8_t b = data[i];
if (b < 0x20) {
printf("<%s>", kCtrlNames[b].c_str());
} else if (b < 0x7f) {
putchar(data[i]);
} else {
printf("<%02xh>", data[i]);
}
}
printf("\r\n");
}
// Tries to read a line from the console and returns whether
// a complete line was read. This is CR/CRLF/LF EOL-aware.
static bool readLine(String &line) {
static bool inCR = false; // Keeps track of CR state
while (Serial.available() > 0) {
int c;
switch (c = Serial.read()) {
case '\r':
inCR = true;
return true;
case '\n':
if (inCR) {
// Ignore the LF
inCR = false;
break;
}
return true;
default:
if (c < 0) {
return false;
}
inCR = false;
line.append(static_cast<char>(c));
}
}
return false;
}
// Prints the chat prompt.
static void printPrompt() {
printf("chat> ");
fflush(stdout); // printf may be line-buffered, so ensure there's output
}
// Reads from the console and sends packets.
static void sendLine() {
static String line;
// Read from the console and send lines
if (readLine(line)) {
if (!udp.send(Ethernet.broadcastIP(), kPort,
reinterpret_cast<const uint8_t *>(line.c_str()),
line.length())) {
printf("[Error sending]\r\n");
}
line = "";
printPrompt();
}
}
Attachments
-
15.9 KB Views: 5
-
21.4 KB Views: 5
-
16.7 KB Views: 5
Last edited: