add pty-gpib-emulator from github

This commit is contained in:
2020-09-09 05:17:31 +00:00
parent 63554da593
commit 3fe71a6f3b
22 changed files with 997 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
#include "Device.h"
#include <string>
#include <iostream>
string Device::handleCMD(Interface *fpl,
string cmd,
vector<string> *args) {
std::cout << "$$ Device: " << cmd << std::endl;
return "";
}

View File

@@ -0,0 +1,23 @@
#ifndef DEVICE
#define DEVICE
#include <string>
#include <iostream>
class Device;
#include "PTY_Interface.h"
#include <vector>
using namespace std;
class Device {
public:
virtual string handleCMD(Interface *fpl,
string cmd,
vector<string> *args) {
return "";
} /**/
};
#endif

View File

@@ -0,0 +1,28 @@
CC=g++
CFLAGS=-c -std=c++11
LDFLAGS=
LIBRARIES=-lutil -lboost_system
SOURCES=PTY_Interface.cpp Prologix.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=
TEST_SRC=$(SOURCES) test.cpp
TEST_OBJ=$(TEST_SRC:.cpp=.o)
TEST_EXE=test
all: $(SOURCES) $(OBJECTS)
test: $(TEST_SRC) $(TEST_OBJ)
$(TEST_EXE):
$(CC) $(LDFLAGS) $(TEST_OBJ) $(LIBRARIES) -o $(TEST_EXE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
.cpp.o:
$(CC) $(CFLAGS) $< -o $@
clean:
rm *.o *~ *.gch

View File

@@ -0,0 +1,110 @@
#include "PTY_Interface.h"
#include <pty.h>
#include <iostream>
#include <unistd.h>
#include <boost/algorithm/string.hpp>
#include <termios.h>
using namespace boost::algorithm;
static void set_noecho(int fd) { // Turn off Slave echo
struct termios stermios;
if (tcgetattr(fd, &stermios) < 0)
std::cout << "Error tcgetattr()" << std::endl;
stermios.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
stermios.c_oflag &= ~(ONLCR);
/* also turn off NL to CR/NL mapping on output */
if (tcsetattr(fd, TCSANOW, &stermios) < 0)
std::cout << "Error tcsetattr()" << std::endl;
}
Interface::Interface(Device *default_device,
int default_address) {
char name[40];
this->addr = default_address;
this->addDevice(default_device, default_address);
if (openpty(&this->m, &this->s, name, NULL, NULL) < 0)
std::cerr << "Could not open pty device" << std::endl;
else {
set_noecho(this->s);
this->pname = name;
this->running = false;
}
}
Interface::~Interface() {
close(this->m);
}
string Interface::printFilename() {
std::cout << this->pname << std::endl;
return this->pname;
}
void Interface::run(int readLen) {
char *tty_in = new char[readLen+1];
vector<string> tty_out;
string tty_full;
int i;
this->running = true;
while (this->running) {
tty_full = "";
while (read(this->m, tty_in, readLen) >= readLen) {
std::cout << tty_in << std::endl;
tty_full += tty_in;
}
tty_full += tty_in;
i = tty_full.find("\n");
if ( i != string::npos )
tty_full = tty_full.substr(0, i);
tty_out = this->handleInput(tty_full);
for (std::vector<string>::iterator it = tty_out.begin();
it != tty_out.end();
it++) {
write(this->m, it->c_str(), it->length());
}
}
delete[] tty_in;
}
vector<string> Interface::handleInput(string tty_in) {
vector<string> out;
Device *dev = NULL;
vector<string> cmd;
vector<string> p;
string first;
int i;
trim(tty_in);
if (tty_in != "" && this->devices.count(this->addr) > 0) {
dev = this->devices.at(this->addr);
if (dev == NULL) {
out.push_back("ERROR! No Device at address: " +
to_string(this->addr));
}
boost::split(cmd, tty_in, boost::is_any_of(";\n"));
for (i=0;i<cmd.size();i++) {
boost::split(p, cmd.at(i), boost::is_any_of(" "));
first = p.at(0);
p.erase(p.begin());
out.push_back(dev->handleCMD(this, first, &p) + "\n");
}
}
out.push_back("");
return out;
}
void Interface::addDevice(Device *dev, int addr) {
this->devices.insert( std::pair<int, Device*>(addr, dev));
}

View File

@@ -0,0 +1,35 @@
#ifndef PTY_INTERFACE
#define PTY_INTERFACE
#include <string>
#include <map>
class Interface;
#include "Device.h"
using namespace std;
class Interface {
public:
map<int, Device*> devices;
int addr, m, s;
bool running;
string pname;
Interface(Device *default_device=NULL,
int default_address=0);
~Interface();
string printFilename();
void run(int readLen=100);
vector<string> handleInput(string tty_in);
void addDevice(Device *dev, int addr);
};
#endif

View File

View File

@@ -0,0 +1,66 @@
#include "Prologix.h"
#include <string>
#include <cstdlib>
#include <iostream>
Prologix_GPIB_USB::Prologix_GPIB_USB() {
this->addCMD("++ver", &Prologix_GPIB_USB::version);
this->addCMD("++addr", &Prologix_GPIB_USB::addr);
this->addCMD("++kill", &Prologix_GPIB_USB::kill);
}
Prologix_GPIB_USB::~Prologix_GPIB_USB() {
}
string Prologix_GPIB_USB::handleCMD(Interface *fpl,
string cmd,
vector<string> *args) {
string out = "";
handler h;
if (out != "") {
return out;
} else if ( this->cmds.count(cmd) > 0 ) {
h = this->cmds.at(cmd);
return (this->*h)(fpl, args);
}
return "";
}
string Prologix_GPIB_USB::version(Interface *fpl,
vector<string> *args) {
return "Prologix Version 1.1 Simulator (C++)";
}
string Prologix_GPIB_USB::addr(Interface *fpl,
vector<string> *args) {
if (args == NULL || args->size() == 0) {
return std::to_string(fpl->addr);
} else {
fpl->addr = atoi(args->at(0).c_str());
if ( fpl->devices.count(fpl->addr) == 0 )
fpl->addDevice(this, fpl->addr);
return "";
}
}
string Prologix_GPIB_USB::kill(Interface *fpl,
vector<string> *args) {
fpl->running = false;
return "";
}
void Prologix_GPIB_USB::addCMD(const char *cmd, handler h) {
this->cmds.insert( std::pair<string, handler>(cmd, h) );
}
void Prologix_GPIB_USB::addCMD(string cmd, handler h) {
this->cmds.insert( std::pair<string, handler>(cmd, h) );
}

View File

@@ -0,0 +1,34 @@
#ifndef PROLOGIX
#define PROLOGIX
#include "Device.h"
#include <map>
#include "PTY_Interface.h"
#include <vector>
#include <string>
using namespace std;
class Prologix_GPIB_USB : public Device {
public:
typedef string (Prologix_GPIB_USB::*handler)(Interface *fpl,
vector<string> *args);
map<string, handler> cmds;
Prologix_GPIB_USB();
~Prologix_GPIB_USB();
string handleCMD(Interface *fpl, string cmd, vector<string> *args);
string version(Interface *fpl, vector<string> *args);
string addr(Interface *fpl, vector<string> *args);
string kill(Interface *fpl, vector<string> *args);
void addCMD(const char *cmd, handler h);
void addCMD(string cmd, handler h);
};
#endif

View File

@@ -0,0 +1,28 @@
#include "PTY_Interface.h"
#include "Prologix.h"
#include <iostream>
#define TEST(A) std::cout << A << ": " << inter.handleInput(A).at(0) << std::endl;
int main() {
Prologix_GPIB_USB *plg = new Prologix_GPIB_USB();
Interface inter(plg);
vector<string> v;
inter.printFilename();
inter.addDevice(plg, 0);
TEST("++ver");
TEST("++addr");
TEST("++addr 2");
TEST("++addr");
TEST("++ver");
inter.printFilename();
inter.run();
delete plg;
return 0;
}