fixed serial bugs

This commit is contained in:
IMback
2018-10-05 22:23:44 +02:00
parent fe9ab16093
commit 7fd14320db

View File

@ -12,6 +12,8 @@
#include "serial_io.h" #include "serial_io.h"
#include "Socket.h" #include "Socket.h"
#define VERSION "v0.3"
bool stop = false; bool stop = false;
void intHandler(int dummy) void intHandler(int dummy)
@ -112,7 +114,7 @@ void acceptThreadFunction( TCPServerSocket* servSock, std::vector<TCPSocket*>*
if(newSock != nullptr) if(newSock != nullptr)
{ {
clientSockets->push_back(newSock); // Wait for a client to connect clientSockets->push_back(newSock); // Wait for a client to connect
clientSockets->back()->send("UVOS serial multiplexer v0.2\n", sizeof("UVOS serial multiplexer v0.2\n")-1); clientSockets->back()->send("UVOS serial multiplexer " VERSION "\n", sizeof("UVOS serial multiplexer " VERSION "\n")-1);
std::cout<<"got client\n"; std::cout<<"got client\n";
} }
std::this_thread::sleep_for(std::chrono::milliseconds(20)); std::this_thread::sleep_for(std::chrono::milliseconds(20));
@ -125,24 +127,34 @@ int main(int argc, char* argv[])
if(parseCmdArgs(argc, argv, &config) != 0) return -1; if(parseCmdArgs(argc, argv, &config) != 0) return -1;
std::cout<<"UVOS serial mulitplexer v0.2\n"; std::cout<<"UVOS serial mulitplexer "<<VERSION<<'\n';
int serial = -1; int serial = -1;
if(!config.noSerial) if(!config.noSerial)
{ {
std::cout<<"Using serial port: "<<config.portFileName<<" at "<<config.baud<<" baud\n"; std::cout<<"Using serial port: "<<config.portFileName<<" at "<<config.baud<<" baud\n";
int serial = serialport_init(config.portFileName.c_str(), config.baud); serial = serialport_init(config.portFileName.c_str(), config.baud);
if(serial == -1) return 1; if(serial == -1) return 1;
} }
else std::cout<<"Sinkless mode\n"; else std::cout<<"Sinkless mode\n";
std::vector<TCPSocket*> clientSockets; std::vector<TCPSocket*> clientSockets;
std::cout<<"opening TCP socket on port "<<config.port<<'\n'; std::thread* acceptThread;
TCPServerSocket servSock(config.port, 5, true); // Server Socket object TCPServerSocket* servSock;
servSock.setBlocking(false);
std::thread acceptThread(acceptThreadFunction, &servSock, &clientSockets); std::cout<<"opening TCP socket on port "<<config.port<<'\n';
try
{
servSock = new TCPServerSocket(config.port, 5, true); // Server Socket object
servSock->setBlocking(false);
acceptThread = new std::thread(acceptThreadFunction, servSock, &clientSockets);
}
catch(SocketException &e)
{
std::cerr<<"Could not open port"<<std::endl;
return 1;
}
char buffer[4096]; char buffer[4096];
@ -154,7 +166,6 @@ int main(int argc, char* argv[])
while(!stop) while(!stop)
{ {
int readlen = sRead(serial, buffer, 4096); int readlen = sRead(serial, buffer, 4096);
//std::cout<<clientSockets.size()<<std::endl;
for(unsigned int i = 0; i < clientSockets.size(); i++) for(unsigned int i = 0; i < clientSockets.size(); i++)
{ {
try try
@ -182,6 +193,13 @@ int main(int argc, char* argv[])
for(unsigned int j = 0; j < clientSockets.size(); j++) if(i != j) clientSockets[j]->send(inBuffer+6, reclen-6); for(unsigned int j = 0; j < clientSockets.size(); j++) if(i != j) clientSockets[j]->send(inBuffer+6, reclen-6);
std::cout<<std::endl; std::cout<<std::endl;
} }
else
{
std::cout<<"wrote \"";
for( int j = 0; j < reclen; j++ )std::cout<<inBuffer[j];
std::cout<<"\" to serial \n";
sWrite(serial, inBuffer, reclen);
}
std::cout<<std::endl; std::cout<<std::endl;
} }
@ -194,7 +212,7 @@ int main(int argc, char* argv[])
if(i < 0) i=0; if(i < 0) i=0;
} }
} }
catch(SocketException e) catch(SocketException &e)
{ {
std::cout<<e.what()<<std::endl; std::cout<<e.what()<<std::endl;
clientSockets[i]->cleanUp(); clientSockets[i]->cleanUp();
@ -203,11 +221,13 @@ int main(int argc, char* argv[])
if(i < 0) i=0; if(i < 0) i=0;
} }
} }
std::this_thread::sleep_for(std::chrono::milliseconds(20)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
} }
acceptThread.join(); acceptThread->join();
delete acceptThread;
for(unsigned int i = 0; i < clientSockets.size(); i++) clientSockets[i]->cleanUp(); for(unsigned int i = 0; i < clientSockets.size(); i++) clientSockets[i]->cleanUp();
servSock.cleanUp(); servSock->cleanUp();
delete servSock;
return 0; return 0;
} }