Prusa Slicer 2.6.0
Loading...
Searching...
No Matches
Slic3r::Utils::Serial Class Reference

#include <src/slic3r/Utils/Serial.hpp>

+ Inheritance diagram for Slic3r::Utils::Serial:
+ Collaboration diagram for Slic3r::Utils::Serial:

Public Member Functions

 Serial (boost::asio::io_service &io_service)
 
 Serial (boost::asio::io_service &io_service, const std::string &name, unsigned baud_rate)
 
 Serial (const Serial &)=delete
 
Serialoperator= (const Serial &)=delete
 
 ~Serial ()
 
void set_baud_rate (unsigned baud_rate)
 

Detailed Description

Constructor & Destructor Documentation

◆ Serial() [1/3]

Slic3r::Utils::Serial::Serial ( boost::asio::io_service &  io_service)

◆ Serial() [2/3]

Slic3r::Utils::Serial::Serial ( boost::asio::io_service &  io_service,
const std::string &  name,
unsigned  baud_rate 
)

◆ Serial() [3/3]

Slic3r::Utils::Serial::Serial ( const Serial )
delete

◆ ~Serial()

Slic3r::Utils::Serial::~Serial ( )
291{}

Member Function Documentation

◆ operator=()

Serial & Slic3r::Utils::Serial::operator= ( const Serial )
delete

◆ set_baud_rate()

void Slic3r::Utils::Serial::set_baud_rate ( unsigned  baud_rate)
294{
295 try {
296 // This does not support speeds > 115200
297 set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
298 } catch (boost::system::system_error &) {
299 auto handle = native_handle();
300
301 auto handle_errno = [](int retval) {
302 if (retval != 0) {
303 throw Slic3r::RuntimeError(
304 (boost::format("Could not set baud rate: %1%") % strerror(errno)).str()
305 );
306 }
307 };
308
309#if __APPLE__
310 termios ios;
311 handle_errno(::tcgetattr(handle, &ios));
312 handle_errno(::cfsetspeed(&ios, baud_rate));
313 speed_t newSpeed = baud_rate;
314 handle_errno(::ioctl(handle, IOSSIOSPEED, &newSpeed));
315 handle_errno(::tcsetattr(handle, TCSANOW, &ios));
316#elif __linux__
317
318 /* The following definitions are kindly borrowed from:
319 /usr/include/asm-generic/termbits.h
320 Unfortunately we cannot just include that one because
321 it would redefine the "struct termios" already defined
322 the <termios.h> already included by Boost.ASIO. */
323#define K_NCCS 19
324 struct termios2 {
325 tcflag_t c_iflag;
326 tcflag_t c_oflag;
327 tcflag_t c_cflag;
328 tcflag_t c_lflag;
329 cc_t c_line;
330 cc_t c_cc[K_NCCS];
331 speed_t c_ispeed;
332 speed_t c_ospeed;
333 };
334#define BOTHER CBAUDEX
335
336 termios2 ios;
337 handle_errno(::ioctl(handle, TCGETS2, &ios));
338 ios.c_ispeed = ios.c_ospeed = baud_rate;
339 ios.c_cflag &= ~CBAUD;
340 ios.c_cflag |= BOTHER | CLOCAL | CREAD;
341 ios.c_cc[VMIN] = 1; // Minimum of characters to read, prevents eof errors when 0 bytes are read
342 ios.c_cc[VTIME] = 1;
343 handle_errno(::ioctl(handle, TCSETS2, &ios));
344
345#elif __OpenBSD__
346 struct termios ios;
347 handle_errno(::tcgetattr(handle, &ios));
348 handle_errno(::cfsetspeed(&ios, baud_rate));
349 handle_errno(::tcsetattr(handle, TCSAFLUSH, &ios));
350#else
351 throw Slic3r::RuntimeError("Custom baud rates are not currently supported on this OS");
352#endif
353 }
354}

The documentation for this class was generated from the following files: