Skip to content

Instantly share code, notes, and snippets.

@espenfjo
Created June 16, 2013 18:22
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 espenfjo/5792908 to your computer and use it in GitHub Desktop.
Save espenfjo/5792908 to your computer and use it in GitHub Desktop.
/*
Arduino Serial Connector
Copyright 2011 Adam Outler
Licensed under the I Dont Give a **** License, Version 1.0 (the "License");
you may not use this file except in compliance with the License.
3. Send me an email if you find this helpful
If you're wondering where number 1 and 2 is, I don't give a ****.
-Adam Outler adamoutler, gmail.com
/*
HEXOUTPUT displays a line of hex output then
a line of normal text- activated by "`"
BAUD will be the computer's baud rate and the
inital baud rate set for all devices
*/
boolean HEXOUTPUT=true;
unsigned long BAUD=9600;
//begin sketch
String MESSAGE[4]; //Hexoutput messages dumped into this string array
unsigned long SerialPrintTimer[4]={0,0,0,0}; //Serial timers off
int inByte;//byte received from serials 1,2,3
void setup() {
Serial.begin(BAUD); //Initialize Computer baud
setSerialBaudRate(BAUD); //Initialize Serials 1,2,3 baud
Serial.print("Communications established");
}
/*
Loop will check if Serial(computer) has anything to
say to the Serial1(device), and then says it. It
will also print all values from Serial1, Serial2,
and Serial3 to Serial(computer)
If HEXOUTPUT formatting is requested by pressing the
"`" key, it will display output in hex format, then
output a line of human readable text
*/
void loop() {
if ( HEXOUTPUT ) { // if hex output is requested then use this
hexOutputHandling();
} else { //Serial1,Serial2, Serial3>Computer in byte format
standardOutputHandling();
}
serialInputHandler();//send to Serial1 and handle special keys
checkForUntruncatedSerial(); //timer to print serial lines in std output
}
/*
serialInputHandler reads keys comming from computer
makes decisions based on key numbers
sends output to Serial1
*/
void serialInputHandler() {
if (Serial.available()) { //User has commanded input
int IncommingSerial=Serial.read(); //read input to var
//turn on/off HEXOUTPUT with the "`" key
if (IncommingSerial == 96 ) displayMenu();
Serial1.write(IncommingSerial); //Send var to Serial1
}
}
void displayMenu(){
Serial.println("");
Serial.println("Main Menu");
Serial.println("--------------------");
Serial.println("--HEX OUTPUT--");
Serial.println("`-toggle hex output"); //96
Serial.println("--BAUD RATE--");
Serial.println("a-115200");//97
Serial.println("b-57600");//98
Serial.println("c-38400");//99
Serial.println("d-28800");//100
Serial.println("e-19200");//101
Serial.println("f-14400");//102
Serial.println("g-9600");//103
Serial.println("h-4800");//104
Serial.println("i-2400");//105
Serial.println("j-1200");//106
Serial.println("k-300");//107
Serial.print("Select one, press enter");
int inkey;
Serial.flush();
while (inkey != 13 ){
inkey=-1;
if (Serial.available()) inkey=Serial.read();
switch (inkey) {
case 96: //`
HEXOUTPUT=(!HEXOUTPUT);
displayBlatentChangeMessage("HEX Output toggled");
break;
case 97: //a
setSerialBaudRate(115200);
displayBlatentChangeMessage("Baud 115200");
break;
case 98: //b
setSerialBaudRate(57600);
displayBlatentChangeMessage("Baud 57600");
break;
case 99: //c
setSerialBaudRate(38400);
displayBlatentChangeMessage("Baud 38400");
break;
case 100: //d
setSerialBaudRate(28800);
displayBlatentChangeMessage("Baud 28800");
break;
case 101: //e
setSerialBaudRate(19200);
displayBlatentChangeMessage("Baud 19200");
break;
case 102: //f
setSerialBaudRate(14400);
displayBlatentChangeMessage("Baud 14400");
break;
case 103: //g
setSerialBaudRate(9600);
displayBlatentChangeMessage("Baud 9600");
break;
case 104: //h
setSerialBaudRate(4800);
displayBlatentChangeMessage("Baud 4800");
break;
case 105: //i
setSerialBaudRate(2400);
displayBlatentChangeMessage("Baud 2400");
break;
case 106: //j
setSerialBaudRate(1200);
displayBlatentChangeMessage("Baud 1200");
break;
case 107: //k
setSerialBaudRate(300);
displayBlatentChangeMessage("Baud 300");
break;
case 13: //l
return;
break;
}
}
}
void displayBlatentChangeMessage(String Message){
for (int i = 0; i < 40; i++){
Serial.println(Message);
}
}
/*
checkForUntruncatedSerial reads timers put on
each serial line and if it has been greater then
the time allowed for a 0x13, then it will print the line
*/
void checkForUntruncatedSerial() {
double time=millis();
for ( int i = 1; i <= 3; i++){
if ( SerialPrintTimer[i] != 0 && SerialPrintTimer[i] < time ){
displayNormal(i);
}
}
}
void setSerialBaudRate(unsigned long baud){
if ( baud == 0.00 ) baud=BAUD;
Serial1.begin(baud);
Serial2.begin(baud);
Serial3.begin(baud);
}
/*
standardOutputHandling reads from Serial1
Serial2, and serial3 then outputs to computer
*/
void standardOutputHandling(){
if (Serial1.available()) Serial.write(Serial1.read());
if (Serial2.available()) Serial.write(Serial2.read());
if (Serial3.available()) Serial.write(Serial3.read());
}
/*
hexOutputHandling reads from Serial1, Serial2,
and Serial3 then sends to displayHex function
for processing. The output is stored in an
array and displayed on 0x13, or after timer
has elapsed
*/
void hexOutputHandling(){
if (Serial1.available()) {
inByte = Serial1.read();
displayHex(1); //Serial1>displayHexfunction>Computer
}
if (Serial2.available()) {
inByte = Serial2.read();
displayHex(2);//Serial2>displayHexfunction>Computer
}
if (Serial3.available()) {
inByte = Serial3.read();
displayHex(3);//Serial3>displayHexfunction>Computer
}
}
/*
displayNormal is called at the end of a line of hexoutput
or when serialPrintTimer has elapsed. It displays the
byte format output and the serial line which originated
the transaction
*/
void displayNormal( int SerialID ){
Serial.println("");
Serial.print(" SERIAL");
Serial.print(SerialID);
Serial.println(": " + MESSAGE[SerialID]);//Serial1:Message
MESSAGE[SerialID]="";//clear message
SerialPrintTimer[SerialID]=0;//reset print timer
}
/*
displayHex formats the hexoutput and the final message
which is displayed when char 13 is received or the timer
has elapsed
*/
void displayHex(int SerialID){
SerialPrintTimer[SerialID]=millis()+ 1000;//set timer for display
if ( inByte >= 19 ) MESSAGE[SerialID] = MESSAGE[SerialID] + " " + (char)inByte; //dump char into string
Serial.print("|");
Serial.print(inByte, HEX); //print character
if ( inByte == 13 ){ displayNormal( SerialID ); }//line ending dump string onto screen
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment