Skip to content

Instantly share code, notes, and snippets.

@julianblanco
Created August 31, 2023 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianblanco/98252df1f1f44fd49bc70949d5d0ad54 to your computer and use it in GitHub Desktop.
Save julianblanco/98252df1f1f44fd49bc70949d5d0ad54 to your computer and use it in GitHub Desktop.
buffer data coming over serial and write out over single pair ethernet to another board
#include </Users/jules/Documents/Arduino/libraries/CircularBuffer/CircularBuffer.h>
// #include <CircularBuffer.h>// issue with name conflict with core libary, use full path to library instead
#include "SparkFun_SinglePairEthernet.h"
CircularBuffer<char, 1024> buffer;
char outputString[1024];
SinglePairEthernet adin1110;
int msg = 0;
const int NUM_MSGS = 8;
const int MAX_MSG_SIZE = 200;
byte deviceMAC[6] = {0x00, 0xE0, 0x22, 0xFE, 0xDA, 0xC9};
byte destinationMAC[6] = {0x00, 0xE0, 0x22, 0xFE, 0xDA, 0xCA};
static void rxCallback(byte * data, int dataLen, byte * senderMac)
{
Serial.print("Recieved:\t");
Serial.println((char *)data); //This is ok since we know they are all null terminated strings
if(!adin1110.indenticalMacs(senderMac, destinationMAC))
{
Serial.print("From an unknown source: ");
for(int i = 0; i < 6; i++)
{
Serial.print(senderMac[i]);
Serial.print(" ");
}
}
Serial.println();
}
void setup() {
Serial.begin(115200);
Serial.println("Single Pair Ethernet - Example 1a Basic Send and Recieve");
/* Start up adin1110 */
if (!adin1110.begin(deviceMAC))
{
Serial.print("Failed to connect to ADIN1110 MACPHY. Make sure board is connected and pins are defined for target.");
while(1); //If we can't connect just stop here
}
Serial.println("Connected to ADIN1110 MACPHY");
/* Set up callback, to control what we do when data is recieved */
adin1110.setRxCallback(rxCallback);
/* Wait for link to be established */
Serial.println("Device Configured, waiting for connection...");
while (adin1110.getLinkStatus() != true);
// put your setup code here, to run once:
}
String temp;
void loop() {
while (Serial.available()) {
buffer.push(Serial.read());
}
if(!(buffer.isEmpty()))
{
if(adin1110.getLinkStatus())
{
while(!buffer.isEmpty())
{
temp = temp += buffer.shift();
}
Serial.print("Sending:\t");
Serial.println(temp); //This is ok since we know they are all null terminated strings
temp.toCharArray(outputString, sizeof(outputString));
adin1110.sendData((byte *)outputString, sizeof(outputString), destinationMAC);
temp = "";
}
else
{
Serial.println("Waiting for link to resume sending");
}
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(5000);
}
// put your main code here, to run repeatedly:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment