serial io ajustments

This commit is contained in:
IMback 2018-10-05 21:09:11 +02:00
parent 6b18e5ee70
commit fe9ab16093
2 changed files with 26 additions and 24 deletions

View File

@ -58,9 +58,8 @@ std::cout<<"Rates:\n\
B2000000 0010013\n";
}
class Config
struct Config
{
public:
std::string portFileName = "/dev/ttyUSB0";
unsigned short port = 6856;
int baud = 0000017;
@ -113,7 +112,7 @@ void acceptThreadFunction( TCPServerSocket* servSock, std::vector<TCPSocket*>*
if(newSock != nullptr)
{
clientSockets->push_back(newSock); // Wait for a client to connect
clientSockets->back()->send("UVOS serial port multiplexer v0.1\n", sizeof("uvos serial port multiplexer v0.1\n")-1);
clientSockets->back()->send("UVOS serial multiplexer v0.2\n", sizeof("UVOS serial multiplexer v0.2\n")-1);
std::cout<<"got client\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));
@ -176,11 +175,11 @@ int main(int argc, char* argv[])
std::cout<<"rec: ";
for( int j = 0; j < reclen; j++ )std::cout<<inBuffer[j];
if(strncmp( inBuffer, "bcst:", 4 ) == 0)
if(strncmp( inBuffer, "bcst: ", 6 ) == 0)
{
std::cout<<"bcst: ";
for( int j = 4; j < reclen; j++ )std::cout<<inBuffer[j];
for(unsigned int j = 0; j < clientSockets.size(); j++) if(i != j) clientSockets[j]->send(inBuffer+5, reclen-5);
for( int j = 6; j < reclen; j++ )std::cout<<inBuffer[j];
for(unsigned int j = 0; j < clientSockets.size(); j++) if(i != j) clientSockets[j]->send(inBuffer+6, reclen-6);
std::cout<<std::endl;
}

View File

@ -23,42 +23,45 @@ int serialport_init(const char* device, int baud)
int fd;
fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("init_serialport: Unable to open port ");
return -1;
}
{
perror("init_serialport: Unable to open port ");
return -1;
}
if (tcgetattr(fd, &toptions) < 0)
{
{
perror("init_serialport: Couldn't get term attributes");
return -1;
}
cfsetispeed(&toptions, baud);
cfsetospeed(&toptions, baud);
}
// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~(CRTSCTS | CLOCAL);
toptions.c_cflag |= IGNPAR | CREAD; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
toptions.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOE); // make raw
toptions.c_oflag &= ~OPOST; // make raw
//Make Raw
toptions.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
toptions.c_oflag &= ~OPOST;
toptions.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
toptions.c_cflag &= ~(CSIZE | PARENB);
toptions.c_cflag |= CS8;
cfsetispeed(&toptions, baud);
cfsetospeed(&toptions, baud);
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
/*toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;
fcntl(fd, F_SETFL, FNDELAY);*/
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 40;
fcntl(fd, F_SETFL, FNDELAY);
if( tcsetattr(fd, TCSANOW, &toptions) < 0)
{
{
perror("init_serialport: Couldn't set term attributes");
return -1;
}
}
return fd;
}