Now you can download a copy of these docs so you can use them offline! Download now
SerialPort.cpp
00001 /*----------------------------------------------------------------------------*/ 00002 /* Copyright (c) FIRST 2008. All Rights Reserved. */ 00003 /* Open Source Software - may be modified and shared by FRC teams. The code */ 00004 /* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */ 00005 /*----------------------------------------------------------------------------*/ 00006 00007 #include "SerialPort.h" 00008 00009 #include "visa/visa.h" 00010 00011 //static ViStatus _VI_FUNCH ioCompleteHandler (ViSession vi, ViEventType eventType, ViEvent event, ViAddr userHandle); 00012 00021 SerialPort::SerialPort(UINT32 baudRate, UINT8 dataBits, SerialPort::Parity parity, SerialPort::StopBits stopBits) 00022 : m_resourceManagerHandle (0) 00023 , m_portHandle (0) 00024 , m_consoleModeEnabled (false) 00025 { 00026 ViStatus localStatus = VI_SUCCESS; 00027 localStatus = viOpenDefaultRM((ViSession*)&m_resourceManagerHandle); 00028 wpi_setError(localStatus); 00029 00030 localStatus = viOpen(m_resourceManagerHandle, "ASRL1::INSTR", VI_NULL, VI_NULL, (ViSession*)&m_portHandle); 00031 wpi_setError(localStatus); 00032 if (localStatus != 0) 00033 { 00034 m_consoleModeEnabled = true; 00035 return; 00036 } 00037 00038 localStatus = viSetAttribute(m_portHandle, VI_ATTR_ASRL_BAUD, baudRate); 00039 wpi_setError(localStatus); 00040 localStatus = viSetAttribute(m_portHandle, VI_ATTR_ASRL_DATA_BITS, dataBits); 00041 wpi_setError(localStatus); 00042 localStatus = viSetAttribute(m_portHandle, VI_ATTR_ASRL_PARITY, parity); 00043 wpi_setError(localStatus); 00044 localStatus = viSetAttribute(m_portHandle, VI_ATTR_ASRL_STOP_BITS, stopBits); 00045 wpi_setError(localStatus); 00046 00047 // Set the default timeout to 5 seconds. 00048 SetTimeout(5.0f); 00049 00050 // Don't wait until the buffer is full to transmit. 00051 SetWriteBufferMode(kFlushOnAccess); 00052 00053 EnableTermination(); 00054 00055 //viInstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler, this); 00056 //viEnableEvent(m_portHandle, VI_EVENT_IO_COMPLETION, VI_HNDLR, VI_NULL); 00057 } 00058 00062 SerialPort::~SerialPort() 00063 { 00064 if (!m_consoleModeEnabled) 00065 { 00066 //viUninstallHandler(m_portHandle, VI_EVENT_IO_COMPLETION, ioCompleteHandler, this); 00067 viClose(m_portHandle); 00068 } 00069 viClose(m_resourceManagerHandle); 00070 } 00071 00077 void SerialPort::SetFlowControl(SerialPort::FlowControl flowControl) 00078 { 00079 if (!m_consoleModeEnabled) 00080 { 00081 ViStatus localStatus = viSetAttribute (m_portHandle, VI_ATTR_ASRL_FLOW_CNTRL, flowControl); 00082 wpi_setError(localStatus); 00083 } 00084 } 00085 00095 void SerialPort::EnableTermination(char terminator) 00096 { 00097 if (!m_consoleModeEnabled) 00098 { 00099 viSetAttribute(m_portHandle, VI_ATTR_TERMCHAR_EN, VI_TRUE); 00100 viSetAttribute(m_portHandle, VI_ATTR_TERMCHAR, terminator); 00101 viSetAttribute(m_portHandle, VI_ATTR_ASRL_END_IN, VI_ASRL_END_TERMCHAR); 00102 } 00103 } 00104 00108 void SerialPort::DisableTermination() 00109 { 00110 if (!m_consoleModeEnabled) 00111 { 00112 viSetAttribute(m_portHandle, VI_ATTR_TERMCHAR_EN, VI_FALSE); 00113 viSetAttribute(m_portHandle, VI_ATTR_ASRL_END_IN, VI_ASRL_END_NONE); 00114 } 00115 } 00116 00122 INT32 SerialPort::GetBytesReceived() 00123 { 00124 INT32 bytes = 0; 00125 if (!m_consoleModeEnabled) 00126 { 00127 ViStatus localStatus = viGetAttribute(m_portHandle, VI_ATTR_ASRL_AVAIL_NUM, &bytes); 00128 wpi_setError(localStatus); 00129 } 00130 return bytes; 00131 } 00132 00140 void SerialPort::Printf(const char *writeFmt, ...) 00141 { 00142 if (!m_consoleModeEnabled) 00143 { 00144 va_list args; 00145 va_start (args, writeFmt); 00146 ViStatus localStatus = viVPrintf(m_portHandle, (ViString)writeFmt, args); 00147 va_end (args); 00148 wpi_setError(localStatus); 00149 } 00150 } 00151 00159 void SerialPort::Scanf(const char *readFmt, ...) 00160 { 00161 if (!m_consoleModeEnabled) 00162 { 00163 va_list args; 00164 va_start (args, readFmt); 00165 ViStatus localStatus = viVScanf(m_portHandle, (ViString)readFmt, args); 00166 va_end (args); 00167 wpi_setError(localStatus); 00168 } 00169 } 00170 00178 UINT32 SerialPort::Read(char *buffer, INT32 count) 00179 { 00180 UINT32 retCount = 0; 00181 if (!m_consoleModeEnabled) 00182 { 00183 ViStatus localStatus = viBufRead(m_portHandle, (ViPBuf)buffer, count, (ViPUInt32)&retCount); 00184 switch (localStatus) 00185 { 00186 case VI_SUCCESS_TERM_CHAR: 00187 case VI_SUCCESS_MAX_CNT: 00188 case VI_ERROR_TMO: // Timeout 00189 break; 00190 default: 00191 wpi_setError(localStatus); 00192 } 00193 } 00194 return retCount; 00195 } 00196 00204 UINT32 SerialPort::Write(const char *buffer, INT32 count) 00205 { 00206 UINT32 retCount = 0; 00207 if (!m_consoleModeEnabled) 00208 { 00209 ViStatus localStatus = viBufWrite(m_portHandle, (ViPBuf)buffer, count, (ViPUInt32)&retCount); 00210 wpi_setError(localStatus); 00211 } 00212 return retCount; 00213 } 00214 00223 void SerialPort::SetTimeout(float timeout) 00224 { 00225 if (!m_consoleModeEnabled) 00226 { 00227 ViStatus localStatus = viSetAttribute(m_portHandle, VI_ATTR_TMO_VALUE, (UINT32)(timeout * 1e3)); 00228 wpi_setError(localStatus); 00229 } 00230 } 00231 00244 void SerialPort::SetReadBufferSize(UINT32 size) 00245 { 00246 if (!m_consoleModeEnabled) 00247 { 00248 ViStatus localStatus = viSetBuf(m_portHandle, VI_READ_BUF, size); 00249 wpi_setError(localStatus); 00250 } 00251 } 00252 00261 void SerialPort::SetWriteBufferSize(UINT32 size) 00262 { 00263 if (!m_consoleModeEnabled) 00264 { 00265 ViStatus localStatus = viSetBuf(m_portHandle, VI_WRITE_BUF, size); 00266 wpi_setError(localStatus); 00267 } 00268 } 00269 00281 void SerialPort::SetWriteBufferMode(SerialPort::WriteBufferMode mode) 00282 { 00283 if (!m_consoleModeEnabled) 00284 { 00285 ViStatus localStatus = viSetAttribute(m_portHandle, VI_ATTR_WR_BUF_OPER_MODE, mode); 00286 wpi_setError(localStatus); 00287 } 00288 } 00289 00296 void SerialPort::Flush() 00297 { 00298 if (!m_consoleModeEnabled) 00299 { 00300 ViStatus localStatus = viFlush(m_portHandle, VI_WRITE_BUF); 00301 wpi_setError(localStatus); 00302 } 00303 } 00304 00310 void SerialPort::Reset() 00311 { 00312 if (!m_consoleModeEnabled) 00313 { 00314 ViStatus localStatus = viClear(m_portHandle); 00315 wpi_setError(localStatus); 00316 } 00317 } 00318 00319 //void SerialPort::_internalHandler(UINT32 port, UINT32 eventType, UINT32 event) 00320 //{ 00321 //} 00322 00323 //ViStatus _VI_FUNCH ioCompleteHandler (ViSession vi, ViEventType eventType, ViEvent event, ViAddr userHandle) 00324 //{ 00325 // ((SerialPort*) userHandle)->_internalHandler(vi, eventType, event); 00326 // return VI_SUCCESS; 00327 //} 00328
Generated on Thu Jan 12 2012 22:35:24 for WPILibC++ by
1.7.1