Hello, Arduino Community,
I have my Arduino Uno with an Ethernet Shield and I am trying to send a TCP packet containing 1 byte of TCP data over an established TCP connection using a socket API function send(). When this code is executed I get printed:
Initializing the w5100 chip and open a TCP socket...
Done opening socket
Connection established
Sending Data...
I would very appreciate if someone could show me where am I wrong. Please check out my code:
#include <Arduino.h>
#include <SPI.h> // needed for Ethernet library communication with the W5100 (Arduino ver>0018)
#include <Ethernet.h>
#include <utility/w5100.h>
#include <utility/socket.h>
#include <util.h>
#include <avr/interrupt.h>
SOCKET s; // our socket 0 that will be opened in RAW mode
// set the src mac address
uint8_t smac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x46, 0x94 };
// set the dst mac address
uint8_t dmac[] = { 0x00, 0x21, 0xcc, 0x70, 0xf3, 0xae};
uint8_t src_ip[4] = { 192, 168, 0, 2};
uint8_t dst_ip[4] = { 192, 168, 0, 1};
uint8_t subnet_mask[4] = { 255, 255, 255, 0};
uint8_t gateway_ip[4] = { 192, 168, 0, 1};
uint8_t* src_mac = smac;
uint8_t* dst_mac = dmac;
uint8_t* dst_ip_addr = dst_ip;
uint8_t* src_ip_addr = src_ip;
uint16_t src_port = 3333;
uint16_t dst_port = 2222;
void setup() {
Serial.begin(115200);
Serial.println("\r\nSetup...");
Serial.print("\r\nInitializing the w5100 chip and open a TCP socket...");
W5100.init(); //
W5100.writeSnMR(s, SnMR::TCP); // set type of socket in Mode Register
W5100.setMACAddress(smac); // set own mac address
W5100.setIPAddress(src_ip_addr); // set own ip address
W5100.writeSnPORT(s, src_port); // set own port (for each socket different)
W5100.setSubnetMask(subnet_mask);
W5100.setGatewayIp(gateway_ip);
W5100.writeSnDHAR(s, dst_mac); // set destination mac address
W5100.writeSnDIPR(s, dst_ip_addr); // set destination ip
W5100.writeSnDPORT(s, dst_port); // set destination port
W5100.execCmdSn(s, Sock_OPEN);
Serial.print("\r\nDone opening socket");
W5100.execCmdSn(s, Sock_CONNECT);
if ( (W5100.readSnSR(s) & SnSR:: ESTABLISHED) != SnSR::ESTABLISHED) // check Status Register to find out if the connection was established
Serial.print("\r\nConnection established");
else
Serial.print("\r\nCould not establish connection!!!");
Serial.print("\r\nSending Data...");
uint8_t data = 0x49;
uint8_t* pdata = &data;
if(!send(s,pdata,1))
Serial.print("Could not send data");
else
Serial.print("Data sent");
}
void loop()
{
}
int main(void) {
init();
setup();
while(true) {
loop();
}
}