| 1 | //============================================================================ |
| 2 | // |
| 3 | // SSSS tt lll lll |
| 4 | // SS SS tt ll ll |
| 5 | // SS tttttt eeee ll ll aaaa |
| 6 | // SSSS tt ee ee ll ll aa |
| 7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
| 8 | // SS SS tt ee ll ll aa aa |
| 9 | // SSSS ttt eeeee llll llll aaaaa |
| 10 | // |
| 11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
| 12 | // and the Stella Team |
| 13 | // |
| 14 | // See the file "License.txt" for information on usage and redistribution of |
| 15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 16 | //============================================================================ |
| 17 | |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <fcntl.h> |
| 21 | #include <unistd.h> |
| 22 | #include <sys/termios.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <cstring> |
| 26 | |
| 27 | #include "SerialPortUNIX.hxx" |
| 28 | |
| 29 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 30 | SerialPortUNIX::SerialPortUNIX() |
| 31 | : SerialPort(), |
| 32 | myHandle(0) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 37 | SerialPortUNIX::~SerialPortUNIX() |
| 38 | { |
| 39 | closePort(); |
| 40 | } |
| 41 | |
| 42 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 43 | bool SerialPortUNIX::openPort(const string& device) |
| 44 | { |
| 45 | myHandle = open(device.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); |
| 46 | if(myHandle <= 0) |
| 47 | return false; |
| 48 | |
| 49 | struct termios termios; |
| 50 | memset(&termios, 0, sizeof(struct termios)); |
| 51 | |
| 52 | termios.c_cflag = CREAD | CLOCAL; |
| 53 | termios.c_cflag |= B19200; |
| 54 | termios.c_cflag |= CS8; |
| 55 | tcflush(myHandle, TCIFLUSH); |
| 56 | tcsetattr(myHandle, TCSANOW, &termios); |
| 57 | |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 62 | void SerialPortUNIX::closePort() |
| 63 | { |
| 64 | if(myHandle) |
| 65 | { |
| 66 | close(myHandle); |
| 67 | myHandle = 0; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 72 | bool SerialPortUNIX::writeByte(uInt8 data) |
| 73 | { |
| 74 | if(myHandle) |
| 75 | { |
| 76 | // cerr << "SerialPortUNIX::writeByte " << int(data) << endl; |
| 77 | return write(myHandle, &data, 1) == 1; |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |