/*
* W5500 Ethernet Module TcpServer example.
*/
#include <SPI.h>
#include <Ethernet_STM.h> //
https://github.com/rogerclarkmelbourne/Arduino_STM32
#define SOCKET_PORT
9876
// You have to change
//#define
MODULE
"ENC28J60"
#define
MODULE
"W5500"
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a
sticker on the shield
#if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC
address of WIZ550io
;
#else
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
#endif
EthernetServer server = EthernetServer(SOCKET_PORT);
void setup()
{
byte myIP[] = { 192, 168, 10, 190 };
delay(1000);Serial.begin(9600);
Serial.print("Ethernet begin....");
// start Ethernet and UDP
#if defined(WIZ550io_WITH_MACADDRESS)
Ethernet.begin(myIP);
#else
Ethernet.begin(mac,myIP);
#endif
for (byte thisByte = 0; thisByte < 4;
thisByte++) {
// print the value of each byte of the
IP address:
//
Serial.println(Ethernet.localIP()[thisByte]);
if (Ethernet.localIP()[thisByte] !=
myIP[thisByte] ) {
Serial.println("fail....");
while (1) {}
}
}
Serial.print("My IP: ");
Serial.println(Ethernet.localIP());
Serial.print("Netmask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("GW IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS IP: ");
Serial.println(Ethernet.dnsServerIP());
Serial.print("localIP: ");
#if defined(WIZ550io_WITH_MACADDRESS)
byte mac_address[6] ={0,};
W5100.getMACAddress(mac_address);
Serial.print("MAC: ");
for(int i = 0; i < 6; i++) {
Serial.print("0x");
Serial.print(mac_address[i],HEX);
Serial.print(" ");
}
Serial.println();
#endif
server.begin();
Serial.println("Server Start");
}
void loop()
{
EthernetClient client;
uint8_t wk;
if (client = server.available()) {
while(client.available() > 0) {
wk = client.read(); //
全体の長さ
size_t tsize = wk;
Serial.println("[" +
String(MODULE) + " Socket Server]Receive tsize=" +
String(tsize));
char* rmsg = (char
*)malloc(tsize+1);
char* smsg =
(char*)malloc(tsize+1);
memset(rmsg,0,tsize+1);
memset(smsg,0,tsize+1);
size_t rsize =0;
int ofs = 0;
while(1) {
if
(client.available() <= 0) continue;
wk =
client.read();
rsize++;
//
Serial.println("rsize=" + String(rsize));
//
Serial.write((uint8_t *)tbuf,psize);
//
Serial.println("]");
rmsg[ofs] = wk;
if(isalpha(wk))
{
smsg[ofs++] = toupper(wk);
} else {
smsg[ofs++] = wk;
}
//
Serial.println("smsg=[" + String(smsg) + "]");
if (rsize ==
tsize) break; // すべてのデータを受信した
}
Serial.print(rmsg);
Serial.print("->");
Serial.println(smsg);
client.write((uint8_t
*)smsg,tsize);
free(rmsg);
free(smsg);
} // end while
client.stop();
}
}
|