00001 00002 #ifndef __BN_SERIAL_PORT 00003 #define __BN_SERIAL_PORT 00004 00005 #include "../../Interfaces/BufferHolder.h" 00006 #include "../../Interfaces/DcAdapter.h" 00007 00008 #include <string> 00009 #include <iostream> 00010 using namespace std; 00011 00012 #include <windows.h> 00013 #include <winbase.h> 00014 00015 #define DEFAULT_SETTINGS "baud=96 parity=E data=8 stop=1" 00016 #define DEFAULT_PORT "COM1" 00017 00018 /** 00019 * This class is a Serial Port adapter for Beensest. It will work only on Windows<br> 00020 * <b>Write</b>: always write the buffer <b>address</b> to the port.<br> 00021 * <b>Read</b>: if <b>address</b> is empty, it reads the data from the port. 00022 * if address is <b>_LastError</b> it returns an error message of the last 00023 * operation (or "OK"). 00024 * <p> 00025 * Configuration: <br> 00026 * <b>COM</b> = "COMx", x is the port number you want to work with.<br> 00027 * <b>Settings</b> = "[baud=b] [parity=p] [data=d] [stop=s] 00028 * [to={on|off}] [xon={on|off}] [odsr={on|off}] [octs={on|off}] 00029 * [dtr={on|off|hs}] [rts={on|off|hs|tg}] [idsr={on|off}]"<br> 00030 * baud = b : Specifies the transmission rate in bits per second.<br> 00031 * 11 = 110 baud, 15 = 150 baud, 30 = 300 baud, 60 = 600 baud, 12 = 1200 baud, 00032 * 24 = 2400 baud, 48 = 4800 baud, 96 = 9600 baud, 19 = 19,200 baud <br> 00033 * parity = p : Specifies how to check for transmission errors. <br> 00034 * n = none, e = even, o = odd, m = mark, s = space <br> 00035 * data = d : Specifies the number of data bits in a byte (5 - 8).<br> 00036 * stop = s : Specifies the number of stop bits that define the end of a byte 00037 * (1, 1.5, or 2).<br><br> 00038 * <b>Read-Interval-Timeout</b> - Maximum time allowed to elapse between the 00039 * arrival of two bytes on the communications line, in milliseconds.A value of 00040 * zero indicates that interval time-outs are not used.<br> 00041 * <b>Read-Total-Timeout-Multiplier</b> - Multiplier used to calculate the total 00042 * time-out period for read operations, in milliseconds. For each read 00043 * operation, this value is multiplied by the requested number of bytes to be 00044 * read.<br> 00045 * <b>Read-Total-Timeout-Constant</b> - Constant used to calculate the total 00046 * time-out period, in milliseconds. This value is added to the product on 00047 * Read-Total-Timeout-Multiplier. A value of zero for both the 00048 * Read-Total-Timeout-Multiplier and Read-Total-Timeout-Constant indicates that 00049 * total time-outs are not used for read operations.<br> 00050 * <b>Write-Total-Timeout-Multiplier</b> - Like Read-Total-Timeout-Multiplier.<br> 00051 * <b>Write-Total-Timeout-Constant</b> - Like Read-Total-Timeout-Constant. 00052 */ 00053 class BnSerialPort : public IdcAdapter 00054 { 00055 private: 00056 /** Pointer to a function of Beesnest to get parameters from the 00057 configuration files. */ 00058 bool (*pfGetParam)(const char*, char*, int); 00059 00060 /** Stores the name of this adapter object. */ 00061 string sName; 00062 00063 /** Flag to indicate an initialization error. */ 00064 bool isInitErr; 00065 /** Stores the last error code (0 - OK). */ 00066 int nLastErrCode; 00067 /** Stores the string this class returns to the user. */ 00068 string sRetMsg; 00069 00070 /** The name of the port "COMx". */ 00071 string sPortName; 00072 /** The settings string for the port.*/ 00073 string sPortSettings; 00074 /** Handle to the port. */ 00075 HANDLE hPort; 00076 /** Timeouts Struct. */ 00077 COMMTIMEOUTS oTimes; 00078 00079 /** Returns True if the port is open. */ 00080 bool IsPortOpen () { return hPort != NULL; } 00081 00082 /** Closes the port. */ 00083 void ClosePort (); 00084 00085 /** Add the last error code to the error message string. */ 00086 void AddLastErrorCode (); 00087 00088 public: 00089 /** Constructor. */ 00090 BnSerialPort (const char* name, bool (*pGetParam)(const char*, char*, int)); 00091 00092 /** Destructor. */ 00093 ~BnSerialPort (); 00094 00095 /** Read data from the port. */ 00096 virtual BufferHolder Read (BufferHolder address) throw (BufferHolder); 00097 00098 /** Write data to the port. */ 00099 virtual bool Write (BufferHolder address, BufferHolder value) throw (BufferHolder); 00100 00101 /** This adapter is NOT for multi-use. */ 00102 virtual bool IsMultiUse () 00103 { return false; } 00104 }; 00105 00106 #endif /* __BN_SERIAL_PORT */ 00107