1 /* 2 * Copyright 2009, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _SERIAL_PORT_H 6 #define _SERIAL_PORT_H 7 8 #include <BeBuild.h> 9 #include <OS.h> 10 #include <SupportDefs.h> 11 12 #include <stddef.h> 13 14 class BList; 15 16 enum data_rate { 17 B_0_BPS = 0, 18 B_50_BPS, 19 B_75_BPS, 20 B_110_BPS, 21 B_134_BPS, 22 B_150_BPS, 23 B_200_BPS, 24 B_300_BPS, 25 B_600_BPS, 26 B_1200_BPS, 27 B_1800_BPS, 28 B_2400_BPS, 29 B_4800_BPS, 30 B_9600_BPS, 31 B_19200_BPS, 32 B_38400_BPS, 33 B_57600_BPS, 34 B_115200_BPS, 35 B_230400_BPS, 36 B_31250_BPS 37 }; 38 39 enum data_bits { 40 B_DATA_BITS_7, 41 B_DATA_BITS_8 42 }; 43 44 enum stop_bits { 45 B_STOP_BITS_1, 46 B_STOP_BITS_2 47 }; 48 49 #define B_STOP_BIT_1 B_STOP_BITS_1 50 51 enum parity_mode { 52 B_NO_PARITY, 53 B_ODD_PARITY, 54 B_EVEN_PARITY 55 }; 56 57 enum { 58 B_NOFLOW_CONTROL = 0, 59 B_HARDWARE_CONTROL = 0x00000001, 60 B_SOFTWARE_CONTROL = 0x00000002 61 }; 62 63 64 class BSerialPort { 65 public: 66 BSerialPort(); 67 virtual ~BSerialPort(); 68 69 status_t Open(const char* portName); 70 void Close(); 71 72 ssize_t Read(void* buf, size_t count); 73 ssize_t Write(const void* buf, size_t count); 74 void SetBlocking(bool blocking); 75 status_t SetTimeout(bigtime_t microSeconds); 76 77 status_t SetDataRate(data_rate bitsPerSecond); 78 data_rate DataRate(); 79 80 void SetDataBits(data_bits numBits); 81 data_bits DataBits(); 82 void SetStopBits(stop_bits numBits); 83 stop_bits StopBits(); 84 85 void SetParityMode(parity_mode which); 86 parity_mode ParityMode(); 87 88 void ClearInput(); 89 void ClearOutput(); 90 91 void SetFlowControl(uint32 method); 92 uint32 FlowControl(); 93 94 status_t SetDTR(bool asserted); 95 status_t SetRTS(bool asserted); 96 status_t NumCharsAvailable(int32* waitThisMany); 97 98 bool IsCTS(); 99 bool IsDSR(); 100 bool IsRI(); 101 bool IsDCD(); 102 103 ssize_t WaitForInput(); 104 105 int32 CountDevices(); 106 status_t GetDeviceName(int32 index, char* name, 107 size_t bufSize = B_OS_NAME_LENGTH); 108 109 private: 110 void _ScanDevices(); 111 int _DriverControl(); 112 virtual void _ReservedSerialPort1(); 113 virtual void _ReservedSerialPort2(); 114 virtual void _ReservedSerialPort3(); 115 virtual void _ReservedSerialPort4(); 116 117 int ffd; 118 data_rate fBaudRate; 119 data_bits fDataBits; 120 stop_bits fStopBits; 121 parity_mode fParityMode; 122 uint32 fFlow; 123 bigtime_t fTimeout; 124 bool fBlocking; 125 BList* fDevices; 126 uint32 fReserved[3]; 127 }; 128 129 #endif //_SERIAL_PORT_H 130