Last post I got my RC2014 connected to my BBS using an Arduino Mega connected to a Laptop running tcpser. I could use QTerm to “dial” telnet BBSes and connect to them.

However, I wanted to remove the laptop running tcpser from the equation, and I have a WizNet ethernet shield, I thought I could use to have the arduino connect to the BBS itself.

It took a little while, but I eventually got it to work, with this attached sketch. It could probably be adapted for an UNO, just change the Serial1 to Serial.

I think it dropped characters because I’m not using CTS/RTS pins, I got around it by adding a 1ms delay between characters. It’s probably a bit slower, but then that’s part of the nostalgic feel…

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };


void setup() {
  // put your setup code here, to run once:
  Serial1.begin(115200);
  Ethernet.begin(mac);
}

EthernetClient client;
int gotIAC = 0;
int count = 0;
bool commandMode = true;
char tmpbuffer[64];
int bufferpos = 0;
char happyland[] = "magickabbs.com";
uint16_t happyland_port = 2023;

void writeser(char *str) {
  while (*str) {
    Serial1.write(*str);
    delay(1);
    Serial1.flush();
    str++;
  }
}
  
void loop() {

  if (commandMode) {
    count = Serial1.available();
    while (count--) {
       char inbyte = Serial1.read();
       if (inbyte == '\r' || inbyte == '\n') {
        if (inbyte == '\r' && Serial1.peek() == '\n') inbyte = Serial1.read();
        writeser("\r\n");
        if (bufferpos > 2) {
          if (toupper(tmpbuffer[0]) == 'A' && toupper(tmpbuffer[1]) == 'T') {
            if (toupper(tmpbuffer[2]) == 'Z') {
              bufferpos = 0;
              writeser("OK\r\n");
            } else
            if (toupper(tmpbuffer[2]) == 'D') {
              if (bufferpos > 3) {
                if (tmpbuffer[3] == '1') {
                  if (client.connect(happyland, happyland_port)) {
                    bufferpos = 0;
                    writeser("CONNECTED\r\n");
                    commandMode = false;
                  } else {
                    bufferpos = 0;
                    writeser("BUSY\r\n");
                  }
                } else if (toupper(tmpbuffer[3]) == 'T') {
                  uint16_t port = 23;
                  if (bufferpos > 4) {
                    for (int z = 4; z < bufferpos;z++) {
                      if (tmpbuffer[z] == ':') {
                        port = atoi(&tmpbuffer[z+1]);
                        tmpbuffer[z] = '\0';
                        break;
                      }
                    }
                    if (client.connect(&tmpbuffer[4], port)) {
                      bufferpos = 0;
                      writeser("CONNECTED\r\n");
                      commandMode = false;
                    } else {
                      bufferpos = 0;
                      writeser("BUSY\r\n");
                    }
                  } else {
                    bufferpos = 0;
                    writeser("ERROR\r\n");
                  }
                }
              }
            } else {
              bufferpos = 0;
              writeser("ERROR\r\n");
            }
          } else {
            bufferpos = 0;
            writeser("ERROR\r\n");
          }
        }
       } else {
        if (inbyte == '\b') {
          if (bufferpos > 0) {
            bufferpos--;
            tmpbuffer[bufferpos] = '\0';
            Serial1.write(inbyte);
          }
        } else {
          tmpbuffer[bufferpos++] = inbyte;
          tmpbuffer[bufferpos] = '\0';
          Serial1.write(inbyte);
        }
       }
    }
  } else {
    if (!client.connected()) {
      writeser("\r\n\r\nDISCONNECTED\r\n");
      commandMode = true;
      return;
    }
    
    count = client.available();
  
    if (count > 0) {
      while (count--) {    
        int inByte = client.read();
        if (gotIAC == 0) {
          if (inByte == 255) {
            gotIAC = 1;
          } else {
            Serial1.write(inByte);  
          }
        } else if (gotIAC == 1) {
          if (inByte == 255) {
            Serial1.write(inByte);
            gotIAC = 0;
          } else if (inByte == 240) {
            gotIAC = 3;
          } else {
            gotIAC = 2;
          }
        } else if (gotIAC == 2) {
          gotIAC = 0;
        } else if (gotIAC == 3) {
          if (inByte == 250) {
            gotIAC = 0;
          }
        }
        delay(1);
      Serial1.flush();
      }
    }
    count = Serial1.available();
    if (count > 0) {
       while (count--) {
          int inByte = Serial1.read();
          if (inByte == 255) {
            if (client.connected()) {
              client.write(inByte);
            }
          }
          if (client.connected()) {
            client.write(inByte);
          }
        }
    }
  }
}