00001 #include "BnSerialPort.h"
00002
00003
00004 BnSerialPort::BnSerialPort (const char* name, bool (*pGetParam)(const char*, char*, int))
00005 :nLastErrCode (0), isInitErr (false), hPort (NULL)
00006 {
00007 sName = name;
00008 pfGetParam = pGetParam;
00009
00010
00011 char path[100];
00012 char val[100] = {0};
00013
00014 sprintf (path, "Adapters->%s->COM", name);
00015 if (pGetParam (path, val, 9))
00016 sPortName = val;
00017 else
00018 sPortName = DEFAULT_PORT;
00019
00020 sprintf (path, "Adapters->%s->Settings", name);
00021 if (pGetParam (path, val, 99))
00022 sPortSettings = val;
00023 else
00024 sPortSettings = DEFAULT_SETTINGS;
00025 if ((hPort = CreateFile (sPortName.c_str (), GENERIC_READ | GENERIC_WRITE,
00026 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == NULL)
00027 {
00028 nLastErrCode = GetLastError ();
00029 isInitErr = true;
00030 return;
00031 }
00032
00033 DCB param;
00034 if (GetCommState (hPort, ¶m) == 0)
00035 {
00036 nLastErrCode = GetLastError ();
00037 isInitErr = true;
00038 return;
00039 }
00040 if (BuildCommDCB (sPortSettings.c_str (), ¶m) == 0)
00041 {
00042 nLastErrCode = GetLastError ();
00043 isInitErr = true;
00044 return;
00045 }
00046 param.DCBlength = sizeof param;
00047 if (SetCommState (hPort, ¶m) == 0)
00048 {
00049 nLastErrCode = GetLastError ();
00050 isInitErr = true;
00051 return;
00052 }
00053
00054 oTimes.ReadTotalTimeoutConstant = 0;
00055 oTimes.ReadTotalTimeoutMultiplier = 0;
00056 oTimes.ReadIntervalTimeout = 0;
00057 oTimes.WriteTotalTimeoutConstant = 0;
00058 oTimes.WriteTotalTimeoutMultiplier = 0;
00059
00060 sprintf (path, "Adapters->%s->Read-Interval-Timeout", name);
00061 if (pGetParam (path, val, 9))
00062 oTimes.ReadIntervalTimeout = atoi (val);
00063 sprintf (path, "Adapters->%s->Read-Total-Timeout-Multiplier", name);
00064 if (pGetParam (path, val, 9))
00065 oTimes.ReadTotalTimeoutMultiplier = atoi (val);
00066 sprintf (path, "Adapters->%s->Read-Total-Timeout-Constant", name);
00067 if (pGetParam (path, val, 9))
00068 oTimes.ReadTotalTimeoutConstant = atoi (val);
00069 sprintf (path, "Adapters->%s->Write-Total-Timeout-Multiplier", name);
00070 if (pGetParam (path, val, 9))
00071 oTimes.WriteTotalTimeoutMultiplier = atoi (val);
00072 sprintf (path, "Adapters->%s->Write-Total-Timeout-Constant", name);
00073 if (pGetParam (path, val, 9))
00074 oTimes.WriteTotalTimeoutConstant = atoi (val);
00075
00076 if (SetCommTimeouts (hPort, &oTimes) == 0)
00077 {
00078 nLastErrCode = GetLastError ();
00079 isInitErr = true;
00080 }
00081 }
00082
00083 BnSerialPort::~BnSerialPort ()
00084 {
00085 ClosePort ();
00086 }
00087
00088 void BnSerialPort::ClosePort ()
00089 {
00090 if (IsPortOpen ())
00091 {
00092 nLastErrCode = CloseHandle (hPort);
00093 hPort = NULL;
00094 }
00095 }
00096
00097 void BnSerialPort::AddLastErrorCode ()
00098 {
00099 if (nLastErrCode)
00100 {
00101 char buff [50];
00102 sprintf (buff, " Error Number: %d", nLastErrCode);
00103 sRetMsg += buff;
00104 }
00105 }
00106
00107 BufferHolder BnSerialPort::Read (BufferHolder address) throw (BufferHolder)
00108 {
00109 if (isInitErr)
00110 {
00111 sRetMsg = "Cannot open " + sPortName;
00112 AddLastErrorCode ();
00113 throw BufferHolder (sRetMsg.c_str ());
00114 }
00115
00116 sRetMsg.clear ();
00117
00118 if (address.empty ())
00119 {
00120 char buff[100];
00121 unsigned long ret;
00122 do
00123 {
00124 ReadFile (hPort, buff, 99, &ret, NULL);
00125 buff [ret] = 0;
00126 sRetMsg += buff;
00127 }
00128 while (ret > 0);
00129 nLastErrCode = GetLastError ();
00130 return BufferHolder (sRetMsg.c_str ());
00131 }
00132
00133 string cmd (address.pointer, address.size);
00134 if (cmd == "_LastError")
00135 {
00136 if (!nLastErrCode)
00137 sRetMsg = "OK";
00138 else
00139 AddLastErrorCode ();
00140 }
00141
00142 return BufferHolder (sRetMsg.c_str ());
00143 }
00144
00145 bool BnSerialPort::Write (BufferHolder address, BufferHolder value) throw (BufferHolder)
00146 {
00147 if (isInitErr)
00148 {
00149 sRetMsg = "Cannot open " + sPortName;
00150 AddLastErrorCode ();
00151 throw BufferHolder (sRetMsg.c_str ());
00152 }
00153
00154 if (address.empty ()) return false;
00155 unsigned long ret;
00156 nLastErrCode = 0;
00157 WriteFile (hPort, address.pointer, address.size, &ret, NULL);
00158 if (ret == 0)
00159 nLastErrCode = GetLastError ();
00160 return nLastErrCode == 0;
00161 }
00162