📄 o22siomm.cpp
字号:
} } else { delete [] pbyDataTemp; return SIOMM_ERROR_RESPONSE_BAD; }} LONG O22SnapIoMemMap::ReadQuad(DWORD dwDestOffset, DWORD * pdwQuadlet)//-------------------------------------------------------------------------------------------------// Read a quadlet of data from a location in the SNAP I/O memory map.//-------------------------------------------------------------------------------------------------{ BYTE byReadQuadletRequest[SIOMM_SIZE_READ_QUAD_REQUEST]; BYTE byReadQuadletResponse[SIOMM_SIZE_READ_QUAD_RESPONSE]; BYTE byTransactionLabel; BYTE byResponseCode; DWORD dwQuadletTemp; LONG nResult; fd_set fds; // Check that we have a valid socket if (INVALID_SOCKET == m_Socket) { return SIOMM_ERROR_NOT_CONNECTED; } // Increment the transaction label UpdateTransactionLabel(); FD_ZERO(&fds); FD_SET(m_Socket, &fds); // Build the request packet BuildReadQuadletRequest(byReadQuadletRequest, m_byTransactionLabel, dwDestOffset); // Send the packet to the Snap I/O unit nResult = send(m_Socket, (char*)byReadQuadletRequest, SIOMM_SIZE_READ_QUAD_REQUEST, 0 /*??*/ ); if (SOCKET_ERROR == nResult) {#ifdef _WIN32 nResult = WSAGetLastError(); // for checking the specific error#endif return SIOMM_ERROR; // This probably means we're not connected. }//#ifdef _LINUX // In the call to select, Linux modifies m_tvTimeOut. It needs to be reset every time. m_tvTimeOut.tv_sec = m_nTimeOutMS / 1000; m_tvTimeOut.tv_usec = (m_nTimeOutMS % 1000) * 1000;//#endif // Is the recv ready? if (0 == select(m_Socket + 1, &fds, NULL, NULL, &m_tvTimeOut)) // ?? Param#1 is ignored by Windows. What should it be in other systems? { // we timed-out return SIOMM_TIME_OUT; } // The response is ready, so recv it. nResult = recv(m_Socket, (char*)byReadQuadletResponse, SIOMM_SIZE_READ_QUAD_RESPONSE, 0 /*??*/); if (SIOMM_SIZE_READ_QUAD_RESPONSE != nResult) { // we got the wrong number of bytes back! return SIOMM_ERROR_RESPONSE_BAD; } // Unpack the response. nResult = UnpackReadQuadletResponse(byReadQuadletResponse, &byTransactionLabel, &byResponseCode, &dwQuadletTemp); // Check that the response was okay if ((SIOMM_OK == nResult) && (SIOMM_RESPONSE_CODE_ACK == byResponseCode) && (m_byTransactionLabel == byTransactionLabel)) { // The response was good, so copy the quadlet *pdwQuadlet = dwQuadletTemp; return SIOMM_OK; } else if ((SIOMM_OK == nResult) && (SIOMM_RESPONSE_CODE_ACK != byResponseCode)) { // If a bad response from the brain, get its last error code long nErrorCode; nResult = GetStatusLastError(&nErrorCode); if (SIOMM_OK == nResult) { return nErrorCode; } else { return nResult; } } else { return SIOMM_ERROR_RESPONSE_BAD; }}LONG O22SnapIoMemMap::WriteBlock(DWORD dwDestOffset, WORD wDataLength, BYTE * pbyData)//-------------------------------------------------------------------------------------------------// Write a block of data to a location in the SNAP I/O memory map.//-------------------------------------------------------------------------------------------------{ BYTE *pbyWriteBlockRequest; BYTE byWriteBlockResponse[SIOMM_SIZE_WRITE_RESPONSE]; BYTE byTransactionLabel; BYTE byResponseCode; LONG nResult; fd_set fds; // Check that we have a valid socket if (INVALID_SOCKET == m_Socket) { return SIOMM_ERROR_NOT_CONNECTED; } // Increment the transaction label UpdateTransactionLabel(); FD_ZERO(&fds); FD_SET(m_Socket, &fds); pbyWriteBlockRequest = new BYTE[SIOMM_SIZE_WRITE_BLOCK_REQUEST + wDataLength]; if (pbyWriteBlockRequest == NULL) return SIOMM_ERROR_OUT_OF_MEMORY; // Couldn't allocate memory! // Build the write request packet BuildWriteBlockRequest(pbyWriteBlockRequest, m_byTransactionLabel, dwDestOffset, wDataLength, pbyData); // Send the packet to the Snap I/O unit nResult = send(m_Socket, (char*)pbyWriteBlockRequest, SIOMM_SIZE_WRITE_BLOCK_REQUEST + wDataLength, 0 /*??*/ ); // Cleanup memory delete [] pbyWriteBlockRequest; // Check the result from send() if (SOCKET_ERROR == nResult) { return SIOMM_ERROR; // oops! }//#ifdef _LINUX // In the call to select, Linux modifies m_tvTimeOut. It needs to be reset every time. m_tvTimeOut.tv_sec = m_nTimeOutMS / 1000; m_tvTimeOut.tv_usec = (m_nTimeOutMS % 1000) * 1000;//#endif // Is the recv ready? if (0 == select(m_Socket + 1, &fds, NULL, NULL, &m_tvTimeOut)) // ?? Param#1 is ignored by Windows. What should it be in other systems? { // we timed-out return SIOMM_TIME_OUT; } // The response is ready, so recv it. nResult = recv(m_Socket, (char*)byWriteBlockResponse, SIOMM_SIZE_WRITE_RESPONSE, 0 /*??*/); if (SIOMM_SIZE_WRITE_RESPONSE != nResult) { // we got the wrong number of bytes back! return SIOMM_ERROR_RESPONSE_BAD; } // Unpack the response nResult = UnpackWriteResponse(byWriteBlockResponse, &byTransactionLabel, &byResponseCode); // Check that the response was okay if ((SIOMM_OK == nResult) && (SIOMM_RESPONSE_CODE_ACK == byResponseCode) && (m_byTransactionLabel == byTransactionLabel)) { // The response was good! return SIOMM_OK; } else if ((SIOMM_OK == nResult) && (SIOMM_RESPONSE_CODE_ACK != byResponseCode)) { // If a bad response from the brain, get its last error code long nErrorCode; nResult = GetStatusLastError(&nErrorCode); if (SIOMM_OK == nResult) { return nErrorCode; } else { return nResult; } } else { return SIOMM_ERROR_RESPONSE_BAD; }}LONG O22SnapIoMemMap::WriteQuad(DWORD dwDestOffset, DWORD dwQuadlet)//-------------------------------------------------------------------------------------------------// Write a quadlet of data to a location in the SNAP I/O memory map.//-------------------------------------------------------------------------------------------------{ BYTE byWriteQuadletRequest[SIOMM_SIZE_WRITE_QUAD_REQUEST]; BYTE byWriteQuadletResponse[SIOMM_SIZE_WRITE_RESPONSE]; BYTE byTransactionLabel; BYTE byResponseCode; LONG nResult; fd_set fds; // Check that we have a valid socket if (INVALID_SOCKET == m_Socket) { return SIOMM_ERROR_NOT_CONNECTED; } // Increment the transaction label UpdateTransactionLabel(); FD_ZERO(&fds); FD_SET(m_Socket, &fds); // Build the write request packet BuildWriteQuadletRequest(byWriteQuadletRequest, m_byTransactionLabel, 1, dwDestOffset, dwQuadlet); // Send the packet to the Snap I/O unit nResult = send(m_Socket, (char*)byWriteQuadletRequest, SIOMM_SIZE_WRITE_QUAD_REQUEST, 0 /*??*/ ); if (SOCKET_ERROR == nResult) { return SIOMM_ERROR; // oops! }//#ifdef _LINUX // In the call to select, Linux modifies m_tvTimeOut. It needs to be reset every time. m_tvTimeOut.tv_sec = m_nTimeOutMS / 1000; m_tvTimeOut.tv_usec = (m_nTimeOutMS % 1000) * 1000;//#endif // Is the recv ready? if (0 == select(m_Socket + 1, &fds, NULL, NULL, &m_tvTimeOut)) // ?? Param#1 is ignored by Windows. What should it be in other systems? { // we timed-out return SIOMM_TIME_OUT; } // The response is ready, so recv it. nResult = recv(m_Socket, (char*)byWriteQuadletResponse, SIOMM_SIZE_WRITE_RESPONSE, 0 /*??*/); if (SIOMM_SIZE_WRITE_RESPONSE != nResult) { // we got the wrong number of bytes back! return SIOMM_ERROR_RESPONSE_BAD; } // Unpack the response nResult = UnpackWriteResponse(byWriteQuadletResponse, &byTransactionLabel, &byResponseCode); // Check that the response was okay if ((SIOMM_OK == nResult) && (SIOMM_RESPONSE_CODE_ACK == byResponseCode) && (m_byTransactionLabel == byTransactionLabel)) { // The response was good! return SIOMM_OK; } else if ((SIOMM_OK == nResult) && (SIOMM_RESPONSE_CODE_ACK != byResponseCode)) { // If a bad response from the brain, get its last error code long nErrorCode; nResult = GetStatusLastError(&nErrorCode); if (SIOMM_OK == nResult) { return nErrorCode; } else { return nResult; } } else { return SIOMM_ERROR_RESPONSE_BAD; }}LONG O22SnapIoMemMap::Close()//-------------------------------------------------------------------------------------------------// Close the connection to the I/O unit//-------------------------------------------------------------------------------------------------{ return CloseSockets();}LONG O22SnapIoMemMap::GetDigPtState(long nPoint, long *pnState)//-------------------------------------------------------------------------------------------------// Get the state of the specified digital point.//-------------------------------------------------------------------------------------------------{ return ReadQuad(SIOMM_DPOINT_READ_STATE + (SIOMM_DPOINT_READ_BOUNDARY * nPoint), (DWORD*)pnState);}LONG O22SnapIoMemMap::GetDigPtOnLatch(long nPoint, long *pnState)//-------------------------------------------------------------------------------------------------// Get the on-latch state of the specified digital point.//-------------------------------------------------------------------------------------------------{ return ReadQuad(SIOMM_DPOINT_READ_ONLATCH_STATE + (SIOMM_DPOINT_READ_BOUNDARY * nPoint), (DWORD*)pnState);}LONG O22SnapIoMemMap::GetDigPtOffLatch(long nPoint, long *pnState)//-------------------------------------------------------------------------------------------------// Get the off-latch state of the specified digital point.//-------------------------------------------------------------------------------------------------{ return ReadQuad(SIOMM_DPOINT_READ_OFFLATCH_STATE + (SIOMM_DPOINT_READ_BOUNDARY * nPoint), (DWORD*)pnState);}LONG O22SnapIoMemMap::GetDigPtCounterState(long nPoint, long *pnState)//-------------------------------------------------------------------------------------------------// Get the active counter state of the specified digital point.//-------------------------------------------------------------------------------------------------{ return ReadQuad(SIOMM_DPOINT_READ_ACTIVE_COUNTER + (SIOMM_DPOINT_READ_BOUNDARY * nPoint), (DWORD*)pnState);}LONG O22SnapIoMemMap::GetDigPtCounts(long nPoint, long *pnValue)//-------------------------------------------------------------------------------------------------// Get the counters of the specified digital point.//-------------------------------------------------------------------------------------------------{ return ReadQuad(SIOMM_DPOINT_READ_COUNTER_DATA + (SIOMM_DPOINT_READ_BOUNDARY * nPoint), (DWORD*)pnValue);}LONG O22SnapIoMemMap::GetDigPtReadAreaEx(long nPoint, SIOMM_DigPointReadArea * pData)//-------------------------------------------------------------------------------------------------// Get the read area for the specified digital point//-------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -