ClearCore Library
Loading...
Searching...
No Matches
EthernetUdpHelloWorld.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: EthernetUdpHelloWorld
3 *
4 * Objective:
5 * This example demonstrates how to enable Ethernet functionality to send and
6 * receive UDP datagrams (packets).
7 *
8 * Description:
9 * This example will set up ethernet communications between a ClearCore and
10 * another Ethernet source (a PC or ClearCore). The example then prints the
11 * contents of the packets received to the specified serial port and sends
12 * a "Hello World" response to the sender.
13 *
14 * Requirements:
15 * ** Setup 1 (ClearCore and a PC): The PC should be running software capable
16 * of sending and receiving UDP packets. PacketSender is highly recommended
17 * as a free, cross-platform software. Configure PacketSender to send a UDP
18 * packet to the ClearCore by specifying the IP address and port provided to
19 * EthernetMgr.LocalIp(). Your firewall or network settings may need to be
20 * adjusted in order to receive the response back from the ClearCore.
21 * ** Setup 2 (ClearCore to a ClearCore): A partner sketch is included at the
22 * end of this file that can be used on the other ClearCore. The MAC address
23 * and IP address values set up for each ClearCore must be unique. The remote
24 * IP address and port used in the partner sketch must match the IP address
25 * and port used to setup the ClearCore in this sketch.
26 *
27 * Links:
28 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
29 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
30 *
31 *
32 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
33 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
34 */
35#include "ClearCore.h"
36#include "EthernetUdp.h"
37
38// Change the IP address below to match your ClearCore's IP address.
39IpAddress ip = IpAddress(192, 168, 1, 177);
40
41// The local port to listen for connections on.
42uint16_t localPort = 8888;
43
44// The maximum number of characters to receive from an incoming packet
45#define MAX_PACKET_LENGTH 100
46// Buffer for holding received packets.
47unsigned char packetReceived[MAX_PACKET_LENGTH];
48
49// The Ethernet UDP object to let us send and receive packets over UDP
50EthernetUdp Udp;
51
52// Set this false if not using DHCP to configure the local IP address.
53bool usingDhcp = true;
54
55int main() {
56 // Set up serial communication at a baud rate of 9600 bps then wait up to
57 // 5 seconds for a port to open.
58 // Serial communication is not required for this example to run, however the
59 // example will appear to do nothing without serial output.
60 ConnectorUsb.Mode(Connector::USB_CDC);
61 ConnectorUsb.Speed(9600);
62 ConnectorUsb.PortOpen();
63
64 uint32_t timeout = 5000;
65 uint32_t startTime = Milliseconds();
66 while (!ConnectorUsb && Milliseconds() - startTime < timeout) {
67 continue;
68 }
69
70 // Make sure the physical link is up before continuing.
71 while (!EthernetMgr.PhyLinkActive()) {
72 ConnectorUsb.SendLine("The Ethernet cable is unplugged...");
73 Delay_ms(1000);
74 }
75
76 // Run the setup for the ClearCore Ethernet manager.
78
79 if (usingDhcp) {
80 // Use DHCP to configure the local IP address.
81 bool dhcpSuccess = EthernetMgr.DhcpBegin();
82 if (dhcpSuccess) {
83 ConnectorUsb.Send("DHCP successfully assigned an IP address: ");
85 }
86 else {
87 ConnectorUsb.SendLine("DHCP configuration was unsuccessful!");
88 while (true) {
89 // UDP will not work without a configured IP address.
90 continue;
91 }
92 }
93 }
94 else {
96 }
97
98 // Begin listening on the local port for UDP datagrams
99 Udp.Begin(localPort);
100
101 // This loop will wait to receive a packet from a remote source, then reply
102 // back with a packet containing a "Hello, world!" message.
103 while (true) {
104 // Look for a received packet.
105 uint16_t packetSize = Udp.PacketParse();
106
107 if (packetSize > 0) {
108 ConnectorUsb.Send("Received packet of size ");
109 ConnectorUsb.Send(packetSize);
110 ConnectorUsb.SendLine(" bytes.");
111
112 ConnectorUsb.Send("Remote IP: ");
113 ConnectorUsb.SendLine(Udp.RemoteIp().StringValue());
114
115 ConnectorUsb.Send("Remote port: ");
116 ConnectorUsb.SendLine(Udp.RemotePort());
117
118 // Read the packet.
119 int32_t bytesRead = Udp.PacketRead(packetReceived, MAX_PACKET_LENGTH);
120 ConnectorUsb.Send("Number of bytes read from packet: ");
121 ConnectorUsb.SendLine(bytesRead);
122
123 ConnectorUsb.Send("Packet contents: ");
124 ConnectorUsb.SendLine((char *)packetReceived);
126
127 // Send a "Hello, world!" reply packet back to the sender.
128 Udp.Connect(Udp.RemoteIp(), Udp.RemotePort());
129 Udp.PacketWrite("Hello, world!");
130 Udp.PacketSend();
131 }
132
133 Delay_ms(10);
134 }
135}
136
137
138/*
139 // ---------------------------------
140 // Partner ClearCore Example Sketch
141 // ---------------------------------
142
143#include "ClearCore.h"
144#include "EthernetUdp.h"
145
146// Change the IP address below to match this ClearCore's IP address.
147IpAddress ip = IpAddress(192, 168, 1, 178);
148
149// The local port to listen for connections on.
150uint16_t localPort = 8888;
151
152// The remote ClearCore's IP address and port
153IpAddress remoteIp = IpAddress(192, 168, 1, 177);
154uint16_t remotePort = 8888;
155
156// The last time you sent a packet to the remote device, in milliseconds.
157uint32_t lastSendTime = 0;
158// Delay between sending packets, in milliseconds
159const uint32_t sendingInterval = 10 * 1000;
160
161// An EthernetUDP instance to let us send and receive packets over UDP
162EthernetUdp Udp;
163
164// Set this false if not using DHCP to configure the local IP address.
165bool usingDhcp = true;
166
167int main() {
168 // Set up serial communication at a baud rate of 9600 bps then wait up to
169 // 5 seconds for a port to open.
170 // Serial communication is not required for this example to run, however the
171 // example will appear to do nothing without serial output.
172 ConnectorUsb.Mode(Connector::USB_CDC);
173 ConnectorUsb.Speed(9600);
174 ConnectorUsb.PortOpen();
175
176 // Run the setup for the ClearCore Ethernet manager.
177 EthernetMgr.Setup();
178
179 if (usingDhcp) {
180 // Use DHCP to configure the local IP address
181 bool dhcpSuccess = EthernetMgr.DhcpBegin();
182 if (dhcpSuccess) {
183 ConnectorUsb.Send("DHCP successfully assigned an IP address: ");
184 ConnectorUsb.SendLine(EthernetMgr.LocalIp().StringValue());
185 }
186 else {
187 ConnectorUsb.SendLine("DHCP configuration was unsuccessful!");
188 ConnectorUsb.SendLine("Try again using a manual configuration...");
189 while (true) {
190 // UDP will not work without a configured IP address.
191 continue;
192 }
193 }
194 }
195 else {
196 EthernetMgr.LocalIp(ip);
197 }
198
199 // Make sure the physical link is up before continuing.
200 while (!EthernetMgr.PhyLinkActive()) {
201 ConnectorUsb.SendLine("The Ethernet cable is unplugged...");
202 Delay_ms(1000);
203 }
204
205 // Begin listening on the local port for UDP datagrams
206 Udp.Begin(localPort);
207
208 // This loop will send a packet to the remote IP and port specified every
209 // 10 seconds.
210 while (true) {
211 // Wait for 10 seconds.
212 if (Milliseconds() - lastSendTime > sendingInterval) {
213 Udp.Connect(remoteIp, remotePort);
214 Udp.PacketWrite("Hello ClearCore.");
215 Udp.PacketSend();
216 lastSendTime = Milliseconds();
217 }
218
219 // Keep the connection alive.
220 EthernetMgr.Refresh();
221 }
222}
223
224*/
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287
uint32_t Milliseconds(void)
Number of milliseconds since the ClearCore was initialized.
volatile const bool & PhyLinkActive()
Check the link status from the PHY.
Definition EthernetManager.h:136
IpAddress LocalIp()
Get the local IP address.
bool DhcpBegin()
Set up DHCP connection to retrieve local IP.
void Setup()
Setup LwIP with the local network interface.
bool SendLine()
Send carriage return and newline characters.
Definition ISerial.h:162
virtual bool Speed(uint32_t bitsPerSecond)=0
Change the baud rate for the port.
bool Send(const char *buffer, size_t bufferSize)
Send the array of characters out the port.
Definition ISerial.h:173
char * StringValue()
Returns a string representation of the IP Address.
Definition IpAddress.h:96
void PortOpen() override
virtual ConnectorModes Mode() override
Get the connector's operational mode.
Definition SerialUsb.h:210
SerialUsb ConnectorUsb
USB connector instance.
EthernetManager & EthernetMgr
Ethernet manager.