SICK系列激光雷达单点测距仪DT80-311111+SIG200配置和通信
文章目录一、硬件连接与SOPAS连接测距仪二、从SOPAS读取数据三、通过JSON获取数据1. 使用Postman测试接口2. 通过代码实现一、硬件连接与SOPAS连接测距仪首先硬件设备连接如下电源厂家应该是不提供需要自行解决。安装完成后需要使用sick的SOPAS软件进行配置1、通过软件更改设备IP2、从设备导入3、读取数据4、点击“离线”按钮切换为在线状态具体请按照这篇文章来连接二、从SOPAS读取数据连接成功后如下图双击设备进入设备页面如下这两位就是十六进制的距离信息可以自行转换为十进制数与测距仪表面显示屏上的十进制数字进行对比。三、通过JSON获取数据分别使用Python和C来实现了1. 使用Postman测试接口其中{ header: { portNumber: 0 }, data: { processData: in } }获取到如下内容表示成功{ header: { status: 0, message: ok }, data: { processDataIn: [ 0, 0, 123, 212, 252, 1 ], isValid: true } }2. 通过代码实现1、C代码如下#includewindows.h#includewinhttp.h#includeiostream#includestring#includevector#includenlohmann/json.hpp#pragmacomment(lib,winhttp.lib)using jsonnlohmann::json;HINTERNETOpenSession(constwchar_t*userAgent){HINTERNET hSessionWinHttpOpen(userAgent,WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);if(!hSession){std::cerrFailed to open WinHTTP session.std::endl;}returnhSession;}std::stringSendPostRequest(HINTERNET hSession,constwchar_t*serverName,constwchar_t*path,conststd::stringpostData){HINTERNET hConnectWinHttpConnect(hSession,serverName,INTERNET_DEFAULT_HTTP_PORT,0);if(!hConnect){std::cerrFailed to connect to server.std::endl;return;}HINTERNET hRequestWinHttpOpenRequest(hConnect,LPOST,path,NULL,WINHTTP_NO_REFERER,WINHTTP_DEFAULT_ACCEPT_TYPES,0);if(!hRequest){std::cerrFailed to open request.std::endl;WinHttpCloseHandle(hConnect);return;}constwchar_t*headersLContent-Type: application/json;DWORD dwBytesSent0;if(!WinHttpSendRequest(hRequest,headers,-1L,(LPVOID)postData.c_str(),postData.size(),postData.size(),0)){std::cerrFailed to send request.std::endl;WinHttpCloseHandle(hRequest);WinHttpCloseHandle(hConnect);return;}if(!WinHttpReceiveResponse(hRequest,NULL)){std::cerrFailed to receive response.std::endl;WinHttpCloseHandle(hRequest);WinHttpCloseHandle(hConnect);return;}std::string response;DWORD dwSize0;DWORD dwDownloaded0;LPSTR pszOutBuffer;do{dwSize0;if(!WinHttpQueryDataAvailable(hRequest,dwSize)){std::cerrFailed to query data available.std::endl;break;}pszOutBuffernewchar[dwSize1];if(!pszOutBuffer){std::cerrMemory allocation failed.std::endl;break;}ZeroMemory(pszOutBuffer,dwSize1);if(!WinHttpReadData(hRequest,(LPVOID)pszOutBuffer,dwSize,dwDownloaded)){std::cerrFailed to read data.std::endl;}else{response.append(pszOutBuffer,dwDownloaded);}delete[]pszOutBuffer;}while(dwSize0);WinHttpCloseHandle(hRequest);WinHttpCloseHandle(hConnect);returnresponse;}intmain(){HINTERNET hSessionOpenSession(LMyAppName/1.0);if(!hSession){return1;}constwchar_t*serverNameL169.254.102.101;constwchar_t*pathL/iolink/sickv1/readPort;json payload{{header,{{portNumber,0}}},{data,{{processData,in}}}};std::string postDatapayload.dump();std::string responseSendPostRequest(hSession,serverName,path,postData);if(!response.empty()){try{json datajson::parse(response);constjson processDataIndata.at(data).at(processDataIn);if(processDataIn.size()4){intthirdDataprocessDataIn[2].getint();intfourthDataprocessDataIn[3].getint();std::coutThird data: thirdDatastd::endl;std::coutFourth data: fourthDatastd::endl;}else{std::coutResponse does not contain enough data.std::endl;}}catch(conststd::exceptione){std::cerrFailed to parse JSON response: e.what()std::endl;}}else{std::cerrRequest failed.std::endl;}WinHttpCloseHandle(hSession);return0;}注意nlohmann/json.hpp需要自行去github上把nlohmann下载下来然后去包含目录导入。python比较简单importrequests# 定义请求的URL和请求体urlhttp://192.168.0.20/iolink/sickv1/readPortpayload{header:{portNumber:0},data:{processData:in}}# 发送POST请求responserequests.post(url,jsonpayload)# 检查请求是否成功ifresponse.status_code200:# 解析返回的JSON数据dataresponse.json()# 获取processDataIn列表process_data_indata.get(data,{}).get(processDataIn,[])# 检查列表长度是否足够iflen(process_data_in)4:# 获取第3和第4个数据索引为2和3third_dataprocess_data_in[2]fourth_dataprocess_data_in[3]print(fThird data:{third_data})print(fFourth data:{fourth_data})else:print(Response does not contain enough data.)else:print(fRequest failed with status code:{response.status_code})运行结果即为所求