using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace ocean
{
public struct CoreConfig
{//模块参数设置
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string Config; //料号
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string ConfigD; //料号
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string lot; //产品批次/料号
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string machine; //设备ID
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string sitel; //厂商
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string project; //项目代码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string sublotname; //子产品批次
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string BinA; //BinA设定值
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string serverip; //测试服务端IP地址
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string visionip; //视觉服务端IP地址
public int bNodeLog; //是否记录Breakdown日志
public int testType; //测试模式 0 默认模式 1-AAB 2-ABC
public int bUpMes; //是否上传MES 治具用
public int bUpMTcp; //是否打开MTCP 治具用
public int bUpNoise; //是否上传噪音数据
public int bSingleTestFilter; //是否开启单测试过滤
public int bDirDate; //是否生成日期文件夹
public int bFourPic; //是否每次拍4个产品, 默认拍4个产品
public int bSavePos; //是否缓存位置 默认缓存
public int uphFrequency; //实时uph刷新频率
public int b2nd; //是否为二代机
public int bTestlog; //是否打印测试通讯日志
public int bVisionlog; //是否打印视觉通讯日志
public int idualindex; //分组序号 A组 1~(3-6)
public int bPassTestB; //是否测试pass才进入B组测试
public int bBindB; //B组是否为绑定模式 TC1->TCB1 TC2->TCB2
public int iSimYield; //模拟测试良率 [0-100] 默认100
public int iSimCT; //模拟测试时间
public int bSimTest1; //模拟测试治具
public int bSimTest2;
public int bSimTest3;
public int bSimTest4;
public int bSimTest5;
public int bSimTest6;
};
//struct TestResult
//{//测试结果,测试机台传递回来
// int bpass; //1pass 0ng
// char csvInfo[MAX_BUF_LEN];
// char failItem[MAX_BUF_LEN];
// char testItem[MAX_BUF_LEN];
//};
public enum TestStatus
{//治具状态
TestNoConn = 0, //未连接
TestRunning, //测试中
TestIdle, //就绪
TestWarn, //治具报警
TestHoming, //治具回原
};
public struct TestMachine
{//测试治具
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string ate; //治具编码
public int benable; //是否禁用
public TestStatus status; //治具状态
public int lastct; //最后一次CT
public int total; //测量总数
public int pass; //pass总数
public float yield; //单治具良率
};
public enum EMapPos
{//产品处于什么位置?
LocationVacm = 0, //吸嘴
LocationInput, //进料仓
LocationOk, //ok仓
LocationNg1, //NG1仓
LocationNg2, //NG2仓
LocationNg3, //NG3仓
LocationTest, //测试治具
};
public struct TProduct
{//产品数据
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string sn; //二维码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string lot; //产品批次/料号
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string ate1; //治具编码1
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string ate2; //治具编码2 NG1次才有此编码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string ate3; //治具编码3 NG2次才有此编码
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string start; //取料时间 YYYY/MM/DD HH:MM:SS
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string end; //放料时间 YYYY/MM/DD HH:MM:SS
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string level; //产品等级 -- 分bin使用
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string failItems;//测试不良项目 多项以;分割
public int status; //最终状态 1pass 0ng
public EMapPos pos; //当前处于哪个位置
public int index; //对应位置的序号 比如治具第几个?料盘上的第几个?
public int count; //测试了几次?
public int takeIndex; //取料时在料盘中的位置
};
public delegate void VisionMsgCallback(int source,int index, string msg,int dir);
public class corework
{
/****************************************************
*Function: 初始化
*Intput: NULL
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "core_init", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int init();
/****************************************************
*Function: 释放模块资源
*Intput: NULL
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "core_deinit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int deinit();
/****************************************************
*Function: 获取版本号
*Intput: NULL
*Output: NULL
*Return: 返回当前版本号(4位整数)
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "core_version", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int core_version();
public static string version()
{//获取版本
string str;
string beta = "";
int iver = core_version();
if (iver > 10000)
{
beta = "beta" + Convert.ToString(iver % 10);
iver %= 10000;
}
else if (iver <= 0)
{
iver = 1001;
}
str = "V" + Convert.ToString(iver / 1000) + "."
+ Convert.ToString((iver / 100) % 10) + "."
+ Convert.ToString(iver % 100);
if (beta != "")
{
str += " " + beta;
}
return str;
}
/****************************************************
*Function: 获取当前测试模式
*Intput: NULL
*Output: NULL
*Return: 返回测试模式 字符串
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "get_test_model", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr get_test_model();
public static string getTestModel()
{
IntPtr p = get_test_model();
return Marshal.PtrToStringAnsi(p);
}
/****************************************************
*Function: 设置参数
*Intput: cfg 参数结构体
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "core_set_cfg", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int core_set_cfg(ref CoreConfig cfg);
/****************************************************
*Function: 获取参数
*Intput: cfg 参数结构体
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "core_get_cfg", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int core_get_cfg(ref CoreConfig cfg);
/****************************************************
*Function: 获取当前产品信息
*Intput: sn 二维码
pdata 缓冲区
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "get_product_info", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int get_product_info(ref TProduct cfg);
/****************************************************
*Function: 根据位置获取产品信息
*Intput: pos 料仓位置 EMapPos
index 所在位置序号
pdata 缓冲区
*Output: NULL
*Return: 有产品返回0,失败返回错误代码<0
*PS: pdata传入NULL可判断产品有无
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "get_product_by_pos", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int get_product_by_pos(EMapPos pos, int index, ref TProduct pdata);
[DllImport(@"corework.dll", EntryPoint = "get_product_by_pos", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int chk_product_by_pos(EMapPos pos, int index, IntPtr p);
public static bool IsHaveProduct(EMapPos pos, int index)
{
return 0 == chk_product_by_pos(pos, index, IntPtr.Zero) ? true : false;
}
/****************************************************
*Function: 获取某个位置的产品列表 -- 进料仓时,sn为null
*Intput: pos 料仓位置 EMapPos
pdata 缓冲区
nsize 缓冲区大小
*Output: NULL
*Return: 返回当前产品数量
*PS:可以先传NULL进去获取数量,然后申请内存再次获取
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "get_product_list", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int get_product_list(EMapPos pos, IntPtr p, int nsize = 0);
public static int GetProductListCount(EMapPos pos)
{
return get_product_list(pos, IntPtr.Zero);
}
public static int GetProductList(EMapPos pos, TProduct[] pst)
{
IntPtr ptr;
int len = 0;
int count = 0;
TProduct info = new TProduct();
count = get_product_list(pos, IntPtr.Zero);
if (count > 0)
{
len = Marshal.SizeOf(info);
ptr = Marshal.AllocHGlobal(len * count);
count = get_product_list(pos, ptr, count);
for (int i = 0; i < count; i++)
{
pst[i] = (TProduct)Marshal.PtrToStructure(
ptr + len * i, typeof(TProduct));
}
Marshal.FreeHGlobal(ptr); //释放非托管内存
}
return count;
}
/****************************************************
*Function: 获取当前所有产品信息
*Intput: pdata 缓冲区
nsize 缓冲区大小
*Output: NULL
*Return: 返回当前产品数量
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "get_all_product_info", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
private static extern int get_all_product_info(IntPtr p, int nsize = 0);
public static int GetAllProductCount()
{
return get_all_product_info(IntPtr.Zero);
}
public static int GetAllProduct(TProduct[] pst)
{
IntPtr ptr;
int len = 0;
int count = 0;
TProduct info = new TProduct();
count = get_all_product_info(IntPtr.Zero);
if (count > 0)
{
len = Marshal.SizeOf(info);
ptr = Marshal.AllocHGlobal(len * count);
get_all_product_info(ptr, count);
for (int i = 0; i < count; i++)
{
pst[i] = (TProduct)Marshal.PtrToStructure(
ptr + len * i, typeof(TProduct));
}
Marshal.FreeHGlobal(ptr); //释放非托管内存
}
return count;
}
/****************************************************
*Function: 启用/禁用一个治具
*Intput: index 治具编号 [1-6]
benable 1启用 0禁用
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "test_enable", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int test_enable(int index, int benable);
/****************************************************
*Function: 获取测试治具信息
*Intput: index 治具编号 [1-6]
*Output: pdata 治具信息
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "get_test_data", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int get_test_data(int index, ref TestMachine pdata);
/****************************************************
*Function: 调试一个测试治具
*Intput: index 治具编号 [1-6]
sn 产品二维码
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
*PS: 根据sn生产数据,并从1号吸嘴创建,并将数据移入治具,开始测试
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "test_debug", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int test_debug(int index, string sn);
/****************************************************
*Function: 通知一个测试回原
*Intput: index 治具编号 [1-6]
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "test_home", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int test_home(int index);
/****************************************************
*Function: 通知一个测试复位
*Intput: index 治具编号 [1-6]
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "test_reset", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int test_reset(int index);
/****************************************************
*Function: 记录日志
*Intput: keyword 操作模式
errcode 错误代码 0无效
errmsg errcode!=0时的错误描述
msg 日志内容
*Output: NULL
*Return: 成功返回0,失败返回错误代码<0
******************************************************/
[DllImport(@"corework.dll", EntryPoint = "wlog", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int wlog(string keyword,int errcode,string errmsg,string msg);
///
/// 增加对相机的连接状态的返回
///
///
[DllImport(@"corework.dll", EntryPoint = "get_cam_status", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int get_cam_status();
///
/// 增加对测试软件的连接状态的返回
///
///
[DllImport(@"corework.dll", EntryPoint = "get_test_status", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int get_test_status();
[DllImport(@"corework.dll", EntryPoint = "registercallbackmsg", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
public static extern int registercallbackmsg(VisionMsgCallback visionmsg);
}
}