Added VERY old code. Very cringy to look at, but hey, we all had to start somewhere...

This commit is contained in:
2018-01-08 23:37:31 -08:00
parent 7b18f6a807
commit df19ed7631
141 changed files with 26107 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
# Detect platform
UNAME = $(shell uname)
ifeq ($(UNAME),Linux)
ENV=unix
endif
ifeq ($(UNAME),Darwin)
ENV=unix
endif
ifndef ENV
ENV=windows
endif
ifeq ($(ENV),unix)
CC = g++
BOOSTFLAGS = -lboost_system
else
CC = g++
BOOSTFLAGS = -lboost_system -lws2_32 -D _WIN32_WINNT=0x0501
endif
all:
$(CC) lab5.cpp -Wall $(BOOSTFLAGS)
clean:
rm -f *.exe
rm -f *.o

View File

@@ -0,0 +1,66 @@
#include <boost/asio.hpp>
/** This code requires 'boost' to be successfully installed and when compiled will require the boost libaries to be included.
The original code source as of this writting was found here:
http://www.webalice.it/fede.tft/serial_port/serial_port.html
*/
class SimpleSerial
{
public:
/**
* Constructor.
* \param port device name, example "/dev/ttyUSB0" or "COM4"
* \param baud_rate communication speed, example 9600 or 115200
* \throws boost::system::system_error if cannot open the
* serial device
*/
SimpleSerial(std::string port, unsigned int baud_rate)
: io(), serial(io,port)
{
serial.set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
}
/**
* Write a string to the serial device.
* \param s string to write
* \throws boost::system::system_error on failure
*/
void writeString(std::string s)
{
boost::asio::write(serial,boost::asio::buffer(s.c_str(),s.size()));
}
/**
* Blocks until a line is received from the serial device. A line is denoted by a final '\n' character
* Eventual '\n' or '\r\n' characters at the end of the string are removed.
* \return a string containing the received line
* \throws boost::system::system_error on failure
*/
std::string readLine()
{
//Reading data char by char, code is optimized for simplicity, not speed
using namespace boost;
char c;
std::string result;
for(;;)
{
asio::read(serial,asio::buffer(&c,1));
switch(c)
{
case '\r':
break;
case '\n':
return result;
default:
result+=c;
}
}
}
private:
boost::asio::io_service io;
boost::asio::serial_port serial;
};

View File

@@ -0,0 +1,53 @@
/*
* conio.h
* This file has no copyright assigned and is placed in the Public Domain.
* This file is a part of the mingw-runtime package.
* No warranty is given; refer to the file DISCLAIMER within the package.
*
* Low level console I/O functions. Pretty please try to use the ANSI
* standard ones if you are writing new code.
*
*/
#ifndef _CONIO_H_
#define _CONIO_H_
/* All the headers include this file. */
#include <_mingw.h>
#ifndef RC_INVOKED
#ifdef __cplusplus
extern "C" {
#endif
_CRTIMP char* __cdecl __MINGW_NOTHROW _cgets (char*);
_CRTIMP int __cdecl __MINGW_NOTHROW _cprintf (const char*, ...);
_CRTIMP int __cdecl __MINGW_NOTHROW _cputs (const char*);
_CRTIMP int __cdecl __MINGW_NOTHROW _cscanf (char*, ...);
_CRTIMP int __cdecl __MINGW_NOTHROW _getch (void);
_CRTIMP int __cdecl __MINGW_NOTHROW _getche (void);
_CRTIMP int __cdecl __MINGW_NOTHROW _kbhit (void);
_CRTIMP int __cdecl __MINGW_NOTHROW _putch (int);
_CRTIMP int __cdecl __MINGW_NOTHROW _ungetch (int);
#ifndef _NO_OLDNAMES
_CRTIMP int __cdecl __MINGW_NOTHROW getch (void);
_CRTIMP int __cdecl __MINGW_NOTHROW getche (void);
_CRTIMP int __cdecl __MINGW_NOTHROW kbhit (void);
_CRTIMP int __cdecl __MINGW_NOTHROW putch (int);
_CRTIMP int __cdecl __MINGW_NOTHROW ungetch (int);
#endif /* Not _NO_OLDNAMES */
#ifdef __cplusplus
}
#endif
#endif /* Not RC_INVOKED */
#endif /* Not _CONIO_H_ */

View File

@@ -0,0 +1,35 @@
// g++ serial.c++ -lboost_system -lws2_32 -D _WIN32_WINNT=0x0501
#include <iostream>
#include "SimpleSerial.h"
#include "conio.h"
using namespace std;
using namespace boost;
int main(int argc, char* argv[])
{
string inputstr;
cout << "Lab 5 Code Started" << endl;
SimpleSerial wunderboard("COM5", 9600);
while(1){
try {
cout << wunderboard.readLine() << endl;
if(kbhit()){
cin >> inputstr;
wunderboard.writeString(inputstr);
if((inputstr.compare("Exit") == 0) | (inputstr.compare("EXIT") == 0) | (inputstr.compare("exit") == 0)){
return 0;
}
}
} catch(boost::system::system_error& e)
{
cout<<"Error: "<<e.what()<<endl;
return 1;
}
}
}