/**
  ******************************************************************************
  * @file           : testmain.cpp
  * @author         : xin
  * @brief          : None
  * @attention      : None
  * @date           : 2024/8/14
  ******************************************************************************
  */
#include "IS3_INST.h"
#include "CSerialPort/SerialPort.h"
#include "CSerialPort/SerialPortInfo.h"
#include "stdio.h"
#include "vector"
using namespace itas109;
CSerialPort *serialport;

size_t SerialWrite(u_char* data,size_t lenth)
{
    printf("write data:");
    for (int i = 0; i < lenth; ++i) {
        printf("%02x ",data[i]);
    }
    printf("\n");
    fflush(stdout);

    serialport->flushReadBuffers();
    return serialport->writeData(data,lenth);



}
size_t SerailRead(u_char* data,size_t lenth)
{


    size_t lenthread=serialport->readData(data,lenth);
    // printf("read data:");
    // for (int i = 0; i < lenthread; ++i)
    // {
    //     printf("%02x ",data[i]);
    // }
    // printf("\n");



   // printf("read data: %d\n",lenthread);
   // int indexnow=serialport->getReadBufferUsedLen();

   // printf("read data index: %d\n",indexnow);
    //fflush(stdout);

    return lenthread;
}



int main() {
    serialport = new CSerialPort();
    //获取串口列表

    std::vector<SerialPortInfo> portNameList = CSerialPortInfo::availablePortInfos();
    for (int i = 0; i < portNameList.size(); ++i) {
        printf("portName:%s\n",portNameList[i].portName);
    }
    std::string portname="COM24";
    serialport->init(portname.c_str(),BaudRate921600 ,ParityNone,DataBits8,StopOne,FlowNone,512*512);
    serialport->setOperateMode(SynchronousOperate);
     //serialport->setReadIntervalTimeout(10000);
    if (serialport->open())
    {        printf("open success\n");
}else {
    printf("open fail \n may be  port: %s is busy or not exist\n",portname.c_str());
    return 0;
}




   // IS11SensorInit();
   printf("Hello World\n");
    IS3Set_Serial_FUN(SerialWrite,SerailRead);
    IS3SensorInit();
    uint16_t *buffforread=new uint16_t[1000];
    // while (1) {
    //     IS3SetShutterOpen(0);
    //     IS3GetData(buffforread,1000);
    //     IS3SetShutterOpen(1);
    // }




   STRsensorinfo_C info= IS3Get_SensorInfo();
    //输出info
    printf("--------------------------------------\nSensorName:%s\n",info.SensorName);
    printf("serialnumber:%s\n",info.serialnumber);
    printf("maxValue:%d\n",info.maxValue);
    printf("a1:%f\n",info.a1);
    printf("a2:%f\n",info.a2);
    printf("a3:%f\n",info.a3);
    printf("a4:%f\n",info.a4 );
    double a[4]={0.0,0.001,1,350};
    IS3SetWeaveLenthCoeff(a,4);
    info= IS3Get_SensorInfo();
    //输出info
    printf("SensorName:%s\n",info.SensorName);
    printf("serialnumber:%s\n",info.serialnumber);
    printf("maxValue:%d\n",info.maxValue);
    printf("a1:%f\n",info.a1);
    printf("a2:%f\n",info.a2);
    printf("a3:%f\n",info.a3);
    printf("a4:%f\n",info.a4 );

    IS3OptSnenser(80);
    while (1) {
        IS3GetData(buffforread,200);
    }

   return 0;
}





//
// Created by xin on 2024/8/14.
//
