Ethernet icon indicating copy to clipboard operation
Ethernet copied to clipboard

Ethernet.flush() goes into an infinite loop.

Open showengineer opened this issue 3 years ago • 0 comments

I might be missing something here, but I am trying to send raw bytes over TCP.

This works totally fine whenever I first try to send and receive a packet. The second time, however, flush() goes into an inifinite loop and the arduino will require a reset.

My source code:

main.ino


// Library imports
#include <SPI.h>

#include "ethernet.h"


#define ENC_SS_PIN      6
#define ENC_RST_PIN     7

// UID buffer
byte uid[UID_BUF_SIZE];
byte token[TOKEN_BUF_LEN];


// === ETHERNET === //
byte ENC_MAC[] = {0x69, 0xB0, 0x0B, 0x1E, 0x54, 0x20};

IPAddress ip(192, 168, 1, 123);
IPAddress mdns(192,168, 1, 1);
IPAddress server(192, 168, 1, 2);

EthernetClient client;


void setup() {

  
  
  Serial.begin(115200);   // Initialize serial communications with the PC

 
  SPI.begin();      // Init SPI bus
  
  Serial.println("Initalizing Ethernet...");

  Ethernet.init(ENC_SS_PIN);

  if(Ethernet.hardwareStatus() == EthernetNoHardware){
    Serial.println("No ethernet module found...halting");
    while(1){
      ; // Halt
    }
  }

  if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("WARNING: No ethernet cable inserted");
  }

  Ethernet.begin(ENC_MAC, ip, mdns);

  if(!client.connect(server, SERVER_PORT)){
    Serial.println("Server connection failed!");
  }


  Serial.println("\nTERMINAL READY");
  
}


void dump_byte_array(uint8_t *buf, uint8_t size) {
  for (uint8_t i = 0; i < size; i++) {
    Serial.print(buf[i] < 0x10 ? " 0" : " ");
    Serial.print(buf[i], HEX);
  }
}



void loop() {

  
  
  // unrelated code above
  // Read UID
  read_uid_and_token(&mfrc522, &key, uid, token);
  // Debug: Did it read the token?
  Serial.println("UID: "); dump_byte_array(uid, UID_BUF_SIZE);
  Serial.println("\nToken: ");dump_byte_array(token, TOKEN_LENGTH);Serial.println("");
  
  

  
   
  if(send_auth_request(&client, server, uid, token)){
    Serial.println("Sending request failure");
    return;
  }

  byte response[24];
  
  rspcpy(&client, response, 49);

  
  for(int i = 0; i < 49; i++){
    Serial.print(response[i], HEX);
  }
  Serial.println("");

}```

and ethernet.h


```cpp
#ifndef ETH_H
#define ETH_H

#define SERVER_PORT 6969

#include <EthernetENC.h>


int send_auth_request(EthernetClient *c, IPAddress server, byte *uid, byte *token){

  if(!c->connected()){
    return 1;
  }

  Serial.println("Writing buffer!");

  for(int i = 0; i < 8; i++){
    Serial.print("Writing buffer! UID bit ");
    Serial.print(i);
    Serial.print("\r");
    c->write(uid[i]);
  }
  Serial.println("");
  for(int i = 0; i < 16;i++){
    Serial.print("Writing buffer! Token bit ");
    Serial.print(i);
    Serial.print("\r");
    c->write(token[i]);
  }

  Serial.println("\nflushing buffer...");
  c->flush();

  return 0;

}


// Copies a response over to a buffer, with fixed length.
int rspcpy(EthernetClient *c, byte *dst, size_t len){
  int cp = 0;
  while (cp < len){
    if(c->available()){
      dst[cp] = c->read();
      cp++;
    }
  }
}

#endif

showengineer avatar Nov 02 '21 14:11 showengineer