Adding option to change CR to NL conversion Signed-off-by: dkubischlu1 <daniel.kubisch@sigmatechnology.se>
diff --git a/src/SerialPort.cc b/src/SerialPort.cc index f1c61f2..59f4833 100644 --- a/src/SerialPort.cc +++ b/src/SerialPort.cc
@@ -23,6 +23,7 @@ dataBits = CS8; //8 data bits stopBit = ~CSTOPB; //1 stop bit parity = 0; //parity: none + CRtoNL = false; // convert NL to CR: false } SerialPort::~SerialPort() @@ -33,8 +34,11 @@ // Implementation of the external function <set_parameter>. void set__port__parameter(const CHARSTRING& parameter_name, const CHARSTRING& parameter_value, SerialPort& serial_port) { serial_port.set_parameter(parameter_name, parameter_value); - const char* device_file_name = serial_port.get_device_file_name(); - printf("Device File Name: %s\n", device_file_name); + + if (strcmp(parameter_name, "deviceFileName") == 0) { + const char* device_file_name = serial_port.get_device_file_name(); + printf("Device File Name: %s\n", device_file_name); + } } char* SerialPort::get_device_file_name(){ @@ -123,7 +127,7 @@ //printf("Parameter %s set to %s (int: %d)\n", parameter_name, parameter_value, blocking); } else if (strncmp(parameter_name, "EOLChar", 7) == 0) { eolChars.push_back(parameter_value[0]); - printf("Parameter %s set to %s (int: %d)\n", parameter_name, parameter_value, eolChars.back()); + //printf("Parameter %s set to %s (int: %d)\n", parameter_name, parameter_value, eolChars.back()); } else if (strcmp(parameter_name, "dataBits") == 0) { if (strcmp(parameter_value, "5") == 0) { dataBits = CS5; @@ -146,6 +150,12 @@ TTCN_error("Invalid %s value: %s\n", parameter_name, parameter_value); } //printf("Parameter %s set to %s (int: %d, hex: 0x%0)\n", parameter_name, parameter_value, stopBit); + }else if (strcmp(parameter_name, "CRtoNL") == 0) { + if (strcmp(parameter_value, "no") == 0){ + CRtoNL = false; + } else if (strcmp(parameter_value, "yes") == 0){ + CRtoNL = true; + } } } @@ -190,7 +200,7 @@ incoming_message(ret_val); } } - } + } } /*void SerialPort::Handle_Timeout(double time_since_last_call) {}*/ @@ -203,7 +213,7 @@ { TTCN_error("Unable to open port %s", param_device_file); } else { - set_interface_attribs (device_fd, speed, parity, dataBits, stopBit); + set_interface_attribs (device_fd, speed, parity, dataBits, stopBit, CRtoNL); set_blocking (device_fd, blocking); Handler_Add_Fd_Read(device_fd); ret_buffer = CHARSTRING(""); @@ -246,7 +256,7 @@ } } - int SerialPort::set_interface_attribs (int fd, speed_t speed, int parity, int dataBits, int stopBit) + int SerialPort::set_interface_attribs (int fd, speed_t speed, int parity, int dataBits, int stopBit, bool CRtoNL) { struct termios tty; memset (&tty, 0, sizeof tty); @@ -259,6 +269,8 @@ cfsetospeed (&tty, speed); cfsetispeed (&tty, speed); + + tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars // disable IGNBRK for mismatched speed tests; otherwise receive break // as \000 chars @@ -266,7 +278,15 @@ tty.c_lflag = 0; // no signaling chars, no echo, // no canonical processing tty.c_oflag = 0; // no remapping, no delays - + + if(CRtoNL){ + tty.c_iflag |= ICRNL; + //printf("now iflag: %u ",tty.c_iflag); + } else { + tty.c_iflag &= ~ICRNL; + //printf("iflag: %u ",tty.c_iflag); + } + tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls, @@ -275,7 +295,7 @@ tty.c_cflag |= parity; tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CRTSCTS; - + if (tcsetattr (fd, TCSANOW, &tty) != 0) { TTCN_error ("error %d from tcsetattr", errno); @@ -304,3 +324,4 @@ } /* end of namespace */ +
diff --git a/src/SerialPort.hh b/src/SerialPort.hh index 1520779..d4c0c44 100644 --- a/src/SerialPort.hh +++ b/src/SerialPort.hh
@@ -50,7 +50,7 @@ void user_stop(); void outgoing_send(const CHARSTRING& send_par); - int set_interface_attribs (int fd, speed_t speed, int parity, int dataBits, int stopBit); + int set_interface_attribs (int fd, speed_t speed, int parity, int dataBits, int stopBit, bool CRtoNL); void set_blocking (int fd, int should_block); char *param_device_file; @@ -61,9 +61,11 @@ std::vector<char> eolChars; int dataBits; int stopBit; + bool CRtoNL; CHARSTRING ret_buffer; }; } /* end of namespace */ #endif +