612 lines
14 KiB
C++
Raw Normal View History

// test_sdk.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include "phoskey_gl.h"
#include <vector>
#include <string>
#include <fstream>
#include <thread>
#include <string.h>
#include <memory>
#if _WIN32
#include <Windows.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
int lDeviceId = 0;
void test_GLX8_GetVersion();
void test_GLX8_EthernetOpen();
void test_GLX8_RebootController();
void test_GLX8_GetHeadTemperature();
void test_ip();
void test_type_2();
void test_reg();
void test_Xpitch();
void test_batch_datas(); // 获取最新批处理数据
void test_deal_atch_datas(); // 开始批处理测量,使用软件输入
void test_deal_atch_datas_IO(); // 开始批处理测量使用IO输入
void set_batch_model();
void help() {
printf("\n------------mm---------------\n");
printf("0 quit \n");
printf("1 GLX8_Initialize\n");
printf("2 GLX8_Finalize\n");
printf("3 test_GLX8_GetVersion\n");
printf("4 test_GLX8_EthernetOpen 连接\n");
printf("5 test_GLX8_RebootController 重启\n");
printf("7 test_GLX8_GetHeadTemperature 温度\n");
printf("8 test_ip 环境设定 \n");
printf("9 test_type_2 公共设定 \n");
printf("10 test_reg 设定 \n");
printf("11 test_Xpitch \n");
printf("12 set_batch_model 开启批处理测量 \n");
printf("13 test_batch_datas 获取批处理数据,使用软件输入 \n");
printf("14 test_batch_datas 获取批处理数据使用IO输入 \n");
printf("------------nn---------------\n");
}
int main()
{
#if _WIN32
CreateDirectory(L"log", NULL);
#else
mkdir("log", S_IRWXU | S_IRWXG | S_IRWXO);
#endif
GLX8_Initialize_Config(false, true);
//glx8_stop_watch sw;
int iw = 0;
while (true)
{
help();
std::cout << "请输入功能码: \n";
std::cin >> iw;
std::cout << "输入功能码是: " << iw << std::endl;
//sw.restart();
switch (iw)
{
case 12:
set_batch_model();
break;
case 13:
test_deal_atch_datas();
break;
case 14:
test_deal_atch_datas_IO();
break;
case 11:
test_Xpitch();
break;
case 10:
test_reg();
break;
case 9:
test_type_2();
break;
case 8:
test_ip();
break;
case 7:
test_GLX8_GetHeadTemperature();
break;
case 5:
test_GLX8_RebootController();
break;
case 0:
return -1;
case 1:
GLX8_Initialize();
break;
case 2:
GLX8_Finalize();
break;
case 3:
test_GLX8_GetVersion();
break;
case 4:
test_GLX8_EthernetOpen();
break;
default:
printf("不存在功能码 \n");
break;
}
//sw.stop();
//printf("运行时间: %f s \n", sw.elapsed_second());
printf("----------------------------end--------------------------- \n");
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
return 0;
}
void test_GLX8_GetVersion()
{
GLX8_VERSION_INFO info = GLX8_GetVersion();
printf("test_GLX8_GetVersion = %d.%d.%d.%d \n", info.nMajorNumber, info.nMinorNumber, info.nRevisionNumber, info.nBuildNumber);
}
void test_GLX8_EthernetOpen()
{
GLX8_ETHERNET_CONFIG cnf;
/*cnf.abyIpAddress[0] = 127;
cnf.abyIpAddress[1] = 0;
cnf.abyIpAddress[2] = 0;
cnf.abyIpAddress[3] = 1;
cnf.wPortNo = 5002;*/
cnf.abyIpAddress[0] = 192;
cnf.abyIpAddress[1] = 168;
cnf.abyIpAddress[2] = 1;
cnf.abyIpAddress[3] = 10;
cnf.wPortNo = 7;
auto bak = GLX8_EthernetOpen(lDeviceId, &cnf);
printf("GLX8_EthernetOpen code = %d \n", int(bak));
}
void test_GLX8_RebootController() {
auto bak = GLX8_RebootController(lDeviceId);
printf("GLX8_RebootController code = %d \n", int(bak));
}
void test_GLX8_GetHeadTemperature()
{
uint16_t p1;
uint16_t p2;
uint16_t p3;
auto bak = GLX8_GetHeadTemperature(lDeviceId, &p1, &p2, &p3);
printf("test_GLX8_GetHeadTemperature code =%d %d - %d - %d\n", bak, int(p1), int(p2), int(p3));
}
void test_ip() {
int iw = 0;
std::cout << "请输入功能码(1: 查询, 2写入) \n";
std::cin >> iw;
std::cout << "输入功能码是: " << iw << std::endl;
GLX8_TARGET_SETTING sett;
if (iw == 1)
{
std::cout << "请输入查询码456(04h IP 地址) /05h (子网掩码)/ 06h (默认网关)) \n";
std::cin >> iw;
std::cout << "输入查询码是: " << iw << std::endl;
sett.byCategory = 0;
sett.byItem = iw;
sett.byType = 1;
char ip[4];
auto bak = GLX8_GetSetting(lDeviceId, 1, sett, ip, 4);
printf("GLX8_GetSetting code = %d, %d.%d.%d.%d \n", int(bak), (uint8_t)ip[0], (uint8_t)ip[1], (uint8_t)ip[2], (uint8_t)ip[3]);
}
else if (iw == 2) {
std::cout << "请输入查询码456(04h IP 地址) /05h (子网掩码)/ 06h (默认网关)) \n";
std::cin >> iw;
std::cout << "输入查询码是: " << iw << std::endl;
sett.byCategory = 0;
sett.byItem = iw;
sett.byType = 1;
char ip[4];
std::cout << "请输入 1 \n";
std::cin >> iw;
ip[0] = iw;
std::cout << "请输入 2 \n";
std::cin >> iw;
ip[1] = iw;
std::cout << "请输入 3 \n";
std::cin >> iw;
ip[2] = iw;
std::cout << "请输入 4 \n";
std::cin >> iw;
ip[3] = iw;
uint32_t err = 0;
auto bak = GLX8_SetSetting(lDeviceId, 1, sett, ip, 4, &err);
printf("GLX8_SetSetting code = %d \n", int(bak));
}
printf("--------------end-------------\n");
}
void test_type_2()
{
int iw = 0;
std::cout << "请输入功能码(1: 查询, 2写入) \n";
std::cin >> iw;
std::cout << "输入功能码是: " << iw << std::endl;
GLX8_TARGET_SETTING sett;
sett.byCategory = 0;
sett.byType = 2;
int byItem = 0;
std::cout << "7 : 请输入查询码ENCODER 最短输入时间) \n";
std::cout << "8 : 控制端子最短输入时间) \n";
std::cin >> byItem;
std::cout << "输入查询码是: " << byItem << std::endl;
sett.byItem = byItem;
uint32_t bval;
char inval[4];
if (iw == 1)
{
auto bak = GLX8_GetSetting(0, 1, sett, inval, 4);
printf("GLX8_GetSetting code = %d \n", int(bak));
if (bak == GLX8_Rc::Ok)
{
uint32_t bval;
memcpy(&bval, inval, 4);
printf("val = %d \n", int(bval));
}
}
else
{
int sendval = 0;
std::cout << "输入: \n";
std::cin >> sendval;
std::cout << "输入是: " << sendval << std::endl;
bval = sendval;
memcpy(inval, &bval, 4);
uint32_t err = 0;
auto bak = GLX8_SetSetting(lDeviceId, 1, sett, inval, 4, &err);
printf("GLX8_SetSetting code = %d \n", int(bak));
}
}
void test_reg()
{
int iw = 0;
std::cout << "请输入功能码(1: 查询, 2写入 , 3: 保存到flash) \n";
std::cin >> iw;
std::cout << "输入功能码是: " << iw << std::endl;
GLX8_TARGET_SETTING sett;
sett.byType = (int)GLX8_SettingType::Program00;
uint32_t err = 0;
if (iw == 3)
{
auto bak = GLX8_SetSetting(lDeviceId, 2, sett, nullptr, 0, &err);
printf("GLX8_SetSetting code = %d \n", int(bak));
return;
}
int Category = 0;
std::cout << "请输入Category (查看文档) (0: 触发, 1拍摄 , 2: 轮廓) \n";
std::cin >> Category;
std::cout << "输入Category是 " << Category << std::endl;
sett.byCategory = Category;
int byItem = 0;
std::cout << "tiem : 菜单(查看文档) \n";
std::cin >> byItem;
std::cout << "输入tiem是 " << byItem << std::endl;
sett.byItem = byItem;
uint32_t bval;
char inval[4];
if (iw == 1)
{
auto bak = GLX8_GetSetting(lDeviceId, 1, sett, inval, 4);
printf("GLX8_GetSetting code = %d \n", int(bak));
if (bak == GLX8_Rc::Ok)
{
uint32_t bval;
memcpy(&bval, inval, 4);
printf("val = %d \n", int(bval));
}
}
else
{
sett.byItem = byItem;
int sendval = 0;
std::cout << "输入: \n";
std::cin >> sendval;
std::cout << "输入是: " << sendval << std::endl;
bval = sendval;
memcpy(inval, &bval, 4);
auto bak = GLX8_SetSetting(lDeviceId, 1, sett, inval, 4, &err);
printf("GLX8_SetSetting code = %d \n", int(bak));
}
}
void test_Xpitch()
{
int iw = 0;
std::cout << "请输入功能码 1: 查询, 2: 写入 : \n";
std::cin >> iw;
std::cout << "输入功能码是: " << iw << std::endl;
uint32_t xpitch = 0;
if (iw == 1)
{
auto bak = GLX8_GetXpitch(lDeviceId, &xpitch);
printf("GLX8_GetXpitch code = %d ,val = %d\n", int(bak), (int)xpitch);
}
else
{
int sendval = 0;
std::cout << "输入: \n";
std::cin >> sendval;
std::cout << "输入是: " << sendval << std::endl;
xpitch = sendval;
auto bak = GLX8_SetXpitch(lDeviceId, xpitch);
printf("GLX8_GetXpitch code = %d ,val = %d\n", int(bak), (int)xpitch);
}
}
void test_batch_datas()
{
// GLX8_StartMeasure(lDeviceId);
GLX8_GET_BATCH_PROFILE_REQUEST pReq;
GLX8_GET_BATCH_PROFILE_RESPONSE pRsp;
GLX8_PROFILE_INFO pProfileInfo;
const int outline_len = 4096; // 一行轮廓点数
const int one_buf_maxsize = 1000; // 一次最多返回批处理行数
pReq.byPositionMode = 0x00;
std::vector<char> outline_buf;
outline_buf.resize(one_buf_maxsize * outline_len * 4);
std::vector<char> gray_buf;
gray_buf.resize(one_buf_maxsize * outline_len);
std::vector<GLX8_PROFILE_HEADER> msgs_buf;
msgs_buf.resize(1000);
std::vector<std::vector<int32_t>> all_batchdatas;
while (true)
{
GLX8_Rc res = GLX8_GetBatchSimpleArray(lDeviceId, &pReq, &pRsp, &pProfileInfo, (char*)msgs_buf.data(), outline_buf.data(), gray_buf.data());
if (res == GLX8_Rc::Ok)
{
if (pRsp.byGetProfileCount > 0)
{
printf("save ---- %d\n", pRsp.byGetProfileCount);
std::vector<int32_t> outline1;
outline1.resize(pRsp.byGetProfileCount * outline_len);
memcpy(outline1.data(), outline_buf.data(), outline1.size() * sizeof(int32_t));
int index = 0;
for (size_t i = 0; i < pRsp.byGetProfileCount; i++)
{
all_batchdatas.push_back(std::vector<int32_t>(outline1.data() + index, outline1.data() + index + 4096));
index += 4096;
}
#if 1 // 是否保存成文件
int index1 = 0;
std::string line1 = "";
{
std::fstream f;
f.open("outline_" + std::to_string(pRsp.dwGetBatchNo) + ".md", std::ios::out | std::ios::app);
if (f.is_open())
{
for (size_t i = 0; i < pRsp.byGetProfileCount; i++)
{
line1 = std::to_string(msgs_buf[i].dwTriggerCount) + "," + std::to_string(msgs_buf[i].lEncoderCount) + ",";
for (size_t col = 0; col < outline_len; col++)
{
line1 += std::to_string(outline1[index1++]) + ",";
}
line1 += std::to_string(pRsp.dwGetBatchTopProfileNo) + "," + std::to_string(pRsp.dwGetBatchNo);
f << line1 << std::endl;
}
f.close();
}
}
{
index1 = 0;
line1 = "";
std::fstream f;
f.open("gray_" + std::to_string(pRsp.dwGetBatchNo) + ".md", std::ios::out | std::ios::app);
if (f.is_open())
{
for (size_t i = 0; i < pRsp.byGetProfileCount; i++)
{
line1 = std::to_string(msgs_buf[i].dwTriggerCount) + "," + std::to_string(msgs_buf[i].lEncoderCount) + ",";
for (size_t col = 0; col < outline_len; col++)
{
line1 += std::to_string(gray_buf[index1++]) + ",";
}
line1 += std::to_string(pRsp.dwGetBatchTopProfileNo) + "," + std::to_string(pRsp.dwGetBatchNo);
f << line1 << std::endl;
}
f.close();
}
}
#endif
printf("save ---- %d end ---\n", pRsp.byGetProfileCount);
// 本次完毕
if (pRsp.byCurrentBatchCommited == 1)
{
// 处理数据 all_batchdatas
printf("本次完毕 :: all_batchdatas.size() = %d \n", (int)all_batchdatas.size());
break;
}
else // 未完毕
{
// 指定位置获取
pReq.byPositionMode = 0x02;
pReq.dwGetBatchNo = pRsp.dwGetBatchNo;
pReq.dwGetProfileNo = pRsp.dwGetBatchTopProfileNo + pRsp.byGetProfileCount;
}
}
else if (pRsp.byCurrentBatchCommited == 1) // 本次完毕
{
break;
}
else
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));
printf("null ---- \n");
}
}
else
{
printf("null ---- 获取数据异常 \n");
break;
}
}
}
void test_deal_atch_datas()
{
// 开启批处理测量
set_batch_model();
while (true)
{
GLX8_Rc res = GLX8_StartMeasure(lDeviceId); // 启动批处理
if (res != GLX8_Rc::Ok)
{
return; // 异常,通信失败
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
test_batch_datas();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
void test_deal_atch_datas_IO()
{
// 开启批处理测量
set_batch_model();
while (true)
{
GLX8_Rc res = GLX8_StartIOMeasure(lDeviceId, 0); // 等待硬件IO输入 0表示无中止等待
if (res != GLX8_Rc::Ok)
{
return; // 异常,通信失败
}
test_batch_datas();
}
}
void set_batch_model()
{
// 开启批处理测量
GLX8_TARGET_SETTING sett;
sett.byType = (int)GLX8_SettingType::Program00;
sett.byCategory = 0;
sett.byItem = 3;
char inval[4];
uint32_t ival = 1; // 开启
memcpy(inval, &ival, 4);
uint32_t err = 0;
auto bak = GLX8_SetSetting(lDeviceId, 1, sett, inval, 4, &err);
printf("GLX8_SetSetting code = %d \n", int(bak));
if (bak != GLX8_Rc::Ok)
{
return;
}
// 批处理数量
sett.byItem = 0x0a;
ival = 300;
memcpy(inval, &ival, 4);
bak = GLX8_SetSetting(lDeviceId, 1, sett, inval, 4, &err);
printf("设置 code = %d \n", int(bak));
if (bak != GLX8_Rc::Ok)
{
return;
}
// 带亮度输出
sett.byType = 2;
sett.byItem = 0x0b;
ival = 1;
memcpy(inval, &ival, 4);
bak = GLX8_SetSetting(lDeviceId, 1, sett, inval, 4, &err);
printf("设置亮度输出 = %d \n", int(bak));
if (bak != GLX8_Rc::Ok)
{
return;
}
}