918 lines
29 KiB
C#
918 lines
29 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Data;
|
|||
|
|
|||
|
using Model;
|
|||
|
|
|||
|
|
|||
|
namespace elelab
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
public class DNLights
|
|||
|
{
|
|||
|
public delegate void lightReturn(int orderNo, int eleId, int color, int eleAddress,int count );
|
|||
|
public static event lightReturn lightOffEvent;
|
|||
|
|
|||
|
static Thread pick_work;
|
|||
|
static Thread pick_work_support;
|
|||
|
|
|||
|
|
|||
|
// static bool begain_pick = false;
|
|||
|
|
|||
|
public static byte[,] ele_lab = new byte[3000, 100];//ele_id address type 0 拣选 1 播种 2 装车
|
|||
|
static uart_dis_data[] ele_lab_data = new uart_dis_data[10000];//ele_id address type 0 拣选 1 播种 2 装车
|
|||
|
static int receive_add = 0;
|
|||
|
static int manage_add = 0;
|
|||
|
static int add_max = 10000;
|
|||
|
|
|||
|
|
|||
|
// public static int port_max = 100;
|
|||
|
|
|||
|
static bool pick_task = true;
|
|||
|
|
|||
|
static List<int> activeComports = new List<int>() ;//激活的com口
|
|||
|
|
|||
|
|
|||
|
// static Dictionary<string, bool> CURRENT_LIGHTS_STATUS = new Dictionary<string, bool>();
|
|||
|
static Dictionary<string, bool> CURRENT_PICK_STATUS = new Dictionary<string, bool>();
|
|||
|
|
|||
|
|
|||
|
static int ORDER_NO = 1;
|
|||
|
|
|||
|
//static Dictionary orderDic
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///清除旧数据,准备接收新一组亮灯数据
|
|||
|
///复位端口
|
|||
|
///清除标签数据
|
|||
|
/// </summary>
|
|||
|
/// <param name="pickType">分拣类型默认0</param>
|
|||
|
public static void initPort(int pickType=0)
|
|||
|
{
|
|||
|
elelab.unart_manage.init_sys();
|
|||
|
init_sys_port(pickType);
|
|||
|
CURRENT_PICK_STATUS.Clear();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 点亮标签
|
|||
|
/// 以端口号和颜色为单位发送亮灯数据
|
|||
|
/// </summary>
|
|||
|
/// <param name="port">标签端口号</param>
|
|||
|
/// <param name="color">亮灯颜色</param>
|
|||
|
/// <param name="eleLabId">标签ID</param>
|
|||
|
/// <param name="eleAddress">标签坐标(行/列)</param>
|
|||
|
/// <param name="counts">显示数量</param>
|
|||
|
/// <param name="pickType">分拣类型,默认0</param>
|
|||
|
/// <returns>是否成功点亮标签</returns>
|
|||
|
public static bool lightLabels( int port,int color, int[] eleLabId, int[] eleAddress,
|
|||
|
int[] counts , int pickType=0)
|
|||
|
{
|
|||
|
// init_port(pickType);
|
|||
|
if (activeComports.Count == 0 || eleLabId.Length == 0)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
byte portB = Convert.ToByte(port);
|
|||
|
int orderNo = port;
|
|||
|
|
|||
|
uart_dis_data[] ele_data_comment = new uart_dis_data[eleLabId.Length];
|
|||
|
for (int j = 0; j < eleLabId.Length; j++)
|
|||
|
{
|
|||
|
ele_data_comment[j].channel_id = 0;//暂不使用 默认为0
|
|||
|
ele_data_comment[j].dis_type = get_byte( (color).ToString());// 灯的颜色 0 不亮 1 红灯 2 绿灯 3 蓝灯 4 红闪 5 绿闪 6 蓝闪
|
|||
|
ele_data_comment[j].ele_id = Convert.ToInt16(eleLabId[j].ToString());
|
|||
|
ele_data_comment[j].kucun_id = 0;
|
|||
|
ele_data_comment[j].little_ele_id = Convert.ToByte(eleAddress[j].ToString());
|
|||
|
ele_data_comment[j].order = Convert.ToByte(orderNo);// get_byte((port).ToString());// Convert.ToByte(ele_order_id);
|
|||
|
ele_data_comment[j].port = "1";// (port).ToString();// "1";
|
|||
|
ele_data_comment[j].receive_count = 0;
|
|||
|
|
|||
|
ele_data_comment[j].send_count = counts[j] ;
|
|||
|
ele_data_comment[j].state = 1;
|
|||
|
ele_data_comment[j].WarehousingID = 0;
|
|||
|
ele_lab[ele_data_comment[j].ele_id, ele_data_comment[j].little_ele_id] = get_byte( pickType .ToString()) ;
|
|||
|
|
|||
|
string key = getLightKey(orderNo,color, eleLabId[j], eleAddress[j], counts[j]);
|
|||
|
CURRENT_PICK_STATUS[key] =false;
|
|||
|
|
|||
|
|
|||
|
}// //向串口发送数据
|
|||
|
|
|||
|
|
|||
|
clear_order order = new clear_order();
|
|||
|
order.order = Convert.ToByte(orderNo); //portB;// get_byte((port).ToString());//Convert.ToByte(ele_order_id);
|
|||
|
order.state = 1;
|
|||
|
|
|||
|
/*
|
|||
|
orderNo += 1;
|
|||
|
if (orderNo >= 255)
|
|||
|
{
|
|||
|
orderNo = 1;
|
|||
|
}*/
|
|||
|
|
|||
|
if (unart_manage.com_manage[portB].clear_order(order) == false)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
if (unart_manage.com_manage[portB].write_data(ele_data_comment) == false)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
string result;
|
|||
|
bool isLightsSuccess = unart_manage.com_manage[port].wait_ack(out result);
|
|||
|
Console.WriteLine("亮灯成功。。。。");
|
|||
|
return isLightsSuccess;
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
public static bool lightLabels(int orderNo,int port, int[] colors, int[] eleLabId, int[] eleAddress,
|
|||
|
int[] counts, int pickType = 0)
|
|||
|
{
|
|||
|
|
|||
|
// init_port(pickType);
|
|||
|
if (activeComports.Count == 0 || eleLabId.Length == 0)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
byte portB = Convert.ToByte(port);
|
|||
|
//int tmp = orderNo;
|
|||
|
|
|||
|
uart_dis_data[] ele_data_comment = new uart_dis_data[eleLabId.Length];
|
|||
|
for (int j = 0; j < eleLabId.Length; j++)
|
|||
|
{
|
|||
|
ele_data_comment[j].channel_id = 0;//暂不使用 默认为0
|
|||
|
ele_data_comment[j].dis_type = get_byte(colors[j].ToString());// 灯的颜色 0 不亮 1 红灯 2 绿灯 3 蓝灯 4 红闪 5 绿闪 6 蓝闪
|
|||
|
ele_data_comment[j].ele_id = Convert.ToInt16(eleLabId[j].ToString());
|
|||
|
ele_data_comment[j].kucun_id = 0;
|
|||
|
ele_data_comment[j].little_ele_id = Convert.ToByte(eleAddress[j].ToString());
|
|||
|
ele_data_comment[j].order = Convert.ToByte(orderNo);// get_byte((port).ToString());// Convert.ToByte(ele_order_id);
|
|||
|
ele_data_comment[j].port = "1";// (port).ToString();// "1";
|
|||
|
ele_data_comment[j].receive_count = 0;
|
|||
|
|
|||
|
ele_data_comment[j].send_count = counts[j];
|
|||
|
ele_data_comment[j].state = 1;
|
|||
|
ele_data_comment[j].WarehousingID = 0;
|
|||
|
ele_lab[ele_data_comment[j].ele_id, ele_data_comment[j].little_ele_id] = get_byte(pickType.ToString());
|
|||
|
|
|||
|
//初始化标签返回事件的状态
|
|||
|
string key = getLightKey(orderNo, colors[j], eleLabId[j], eleAddress[j], counts[j]);
|
|||
|
CURRENT_PICK_STATUS[key] = false;
|
|||
|
|
|||
|
}// //向串口发送数据
|
|||
|
|
|||
|
clear_order order = new clear_order();
|
|||
|
order.order = Convert.ToByte(orderNo); //portB;// get_byte((port).ToString());//Convert.ToByte(ele_order_id);
|
|||
|
order.state = 1;
|
|||
|
|
|||
|
/*
|
|||
|
orderNo += 1;
|
|||
|
if (orderNo >= 255)
|
|||
|
{
|
|||
|
orderNo = 1;
|
|||
|
}
|
|||
|
Console.WriteLine("next clean orderNo is。。。。" + orderNo);
|
|||
|
*/
|
|||
|
if (unart_manage.com_manage[portB].clear_order(order) == false)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
if (unart_manage.com_manage[portB].write_data(ele_data_comment) == false)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
/*
|
|||
|
string result;
|
|||
|
bool isLightsSuccess = unart_manage.com_manage[portB].wait_ack(out result);
|
|||
|
if (isLightsSuccess)
|
|||
|
Console.WriteLine("亮灯成功。。。。");
|
|||
|
return isLightsSuccess;
|
|||
|
*/
|
|||
|
return true;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public static bool clearOrder(int orderNo, int port)
|
|||
|
{
|
|||
|
clear_order order = new clear_order();
|
|||
|
order.order = Convert.ToByte(orderNo);
|
|||
|
order.state = 1;
|
|||
|
|
|||
|
if (unart_manage.com_manage[Convert.ToByte(port)].clear_order(order) == false)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 点亮标签
|
|||
|
/// 以端口号和颜色为单位发送亮灯数据
|
|||
|
/// </summary>
|
|||
|
/// <param name="port">标签端口号</param>
|
|||
|
/// <param name="colors">亮灯颜色</param>
|
|||
|
/// <param name="eleLabId">标签ID</param>
|
|||
|
/// <param name="eleAddress">标签坐标(行/列)</param>
|
|||
|
/// <param name="counts">显示数量</param>
|
|||
|
/// <param name="pickType">分拣类型,默认0</param>
|
|||
|
/// <returns>是否成功点亮标签</returns>
|
|||
|
public static bool lightLabels(int port, int[] colors, int[] eleLabId, int[] eleAddress,
|
|||
|
int[] counts, int pickType = 0)
|
|||
|
{
|
|||
|
|
|||
|
if (ORDER_NO > 200)
|
|||
|
{
|
|||
|
ORDER_NO = 1;
|
|||
|
}
|
|||
|
return lightLabels(ORDER_NO, port, colors, eleLabId, eleAddress, counts, pickType);
|
|||
|
ORDER_NO++;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static List<int> getActiveComports()
|
|||
|
{
|
|||
|
return activeComports;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 按灭标签,接收回传数据
|
|||
|
/// 用户可以在此进行标签灭灯事件的数据处理
|
|||
|
/// </summary>
|
|||
|
public static void returnData()
|
|||
|
{
|
|||
|
|
|||
|
List<string> statuKeys = new List<string>();
|
|||
|
int eleId; //标签ID
|
|||
|
int eleAddress;//标签坐标(层/列)
|
|||
|
int count;//数量
|
|||
|
int color;//颜色
|
|||
|
int orderNo; //端口
|
|||
|
|
|||
|
// Dictionary<string, bool> tmp = CURRENT_PICK_STATUS;
|
|||
|
while (receive_add != manage_add)
|
|||
|
{
|
|||
|
count = Convert.ToInt16(receive_data_dot(Convert.ToInt32(ele_lab_data[manage_add].receive_count), Convert.ToInt16(ele_lab_data[manage_add].dot)));
|
|||
|
eleId = Convert.ToInt16(ele_lab_data[manage_add].ele_id.ToString()); //label id
|
|||
|
color = ele_lab_data[manage_add].dis_type;
|
|||
|
eleAddress = Convert.ToInt16(ele_lab_data[manage_add].little_ele_id.ToString()); //ele address
|
|||
|
int pickType = elelab.DNLights.ele_lab[eleId, eleAddress];
|
|||
|
|
|||
|
orderNo = Convert.ToInt16(ele_lab_data[manage_add].order.ToString()); //port
|
|||
|
|
|||
|
// int pno = Convert.ToInt16(ele_lab_data[manage_add].port.ToString()); //port
|
|||
|
string pno = ele_lab_data[manage_add].port;
|
|||
|
string key;
|
|||
|
|
|||
|
if (eleId == 0)
|
|||
|
{
|
|||
|
manage_add = receive_add;
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
key = getLightKey(orderNo, color, eleId, eleAddress, count);
|
|||
|
|
|||
|
manage_add++;
|
|||
|
|
|||
|
if (manage_add >= add_max)
|
|||
|
{
|
|||
|
manage_add = 0;
|
|||
|
}
|
|||
|
|
|||
|
if (CURRENT_PICK_STATUS.ContainsKey(key))
|
|||
|
{
|
|||
|
if (CURRENT_PICK_STATUS[key])
|
|||
|
{
|
|||
|
Console.WriteLine("---------------------------- get key :" + key + ", orderNo: +" + orderNo);
|
|||
|
continue;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
Console.WriteLine("------------------------------ key dose not exists :" + key);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
//客户在此处进行的数据处理
|
|||
|
//.....
|
|||
|
|
|||
|
// 步骤3,调用delegate
|
|||
|
|
|||
|
lightReturn handler = lightOffEvent;
|
|||
|
if (handler != null)
|
|||
|
{
|
|||
|
handler(orderNo, eleId, color, eleAddress, count);
|
|||
|
CURRENT_PICK_STATUS[key] = true;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 熄灭指定ID的标签
|
|||
|
/// </summary>
|
|||
|
/// <param name="ltId">默认熄灭所有</param>
|
|||
|
public static void turnOffLight(int ltId =65535)
|
|||
|
{
|
|||
|
Model.dis_id id;
|
|||
|
id.order = 1;
|
|||
|
id.ele_id = ltId; //65535;
|
|||
|
id.state = 1;
|
|||
|
|
|||
|
foreach (int port in activeComports)
|
|||
|
{
|
|||
|
if (port == 0)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
unart_manage.com_manage[port].init_port.write_clear_comment(id);
|
|||
|
}
|
|||
|
|
|||
|
if (ltId != 65535) //当熄灭端口所有标签时,熄灭端口的通道灯
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
id.order = 1;
|
|||
|
id.ele_id = ltId;// 65535;
|
|||
|
id.state = 1;
|
|||
|
|
|||
|
|
|||
|
foreach (int port in activeComports)
|
|||
|
{
|
|||
|
if (port == 0 )
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
elelab.unart_manage.com_manage[port].init_port.write_clear_comment(id);
|
|||
|
|
|||
|
if (!channalLights.ContainsKey(port))
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
foreach (int lt in channalLights[port])
|
|||
|
{
|
|||
|
Model.close_channel_led idx;
|
|||
|
idx.channel_id = lt;
|
|||
|
idx.state = 1;
|
|||
|
elelab.unart_manage.com_manage[port].init_port.write_close_channel_led(idx);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// a.write_clear_comment(id);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 熄灭指定端口标签
|
|||
|
/// 当熄灭端口所有标签时,熄灭端口的通道灯
|
|||
|
/// </summary>
|
|||
|
/// <param name="port">端口号</param>
|
|||
|
/// <param name="ltId">标签ID,默认熄灭全部标签</param>
|
|||
|
public static void turnOffLightOfPort(int portNo,int ltId = 65535)
|
|||
|
{
|
|||
|
Model.dis_id id;
|
|||
|
id.order = 1;
|
|||
|
id.ele_id = ltId; //65535;
|
|||
|
id.state = 1;
|
|||
|
|
|||
|
foreach (int port in activeComports)
|
|||
|
{
|
|||
|
if (port != portNo)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
unart_manage.com_manage[port].init_port.write_clear_comment(id);
|
|||
|
break;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
if (ltId != 65535) //当熄灭端口所有标签时,熄灭端口的通道灯
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
id.order = 1;
|
|||
|
id.ele_id = ltId;// 65535;
|
|||
|
id.state = 1;
|
|||
|
|
|||
|
|
|||
|
foreach (int port in activeComports)
|
|||
|
{
|
|||
|
if (port == 0 || port != portNo)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
elelab.unart_manage.com_manage[port].init_port.write_clear_comment(id);
|
|||
|
|
|||
|
if (!channalLights.ContainsKey(port))
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
|
|||
|
foreach (int lt in channalLights[port])
|
|||
|
{
|
|||
|
Model.close_channel_led idx;
|
|||
|
idx.channel_id = lt;
|
|||
|
idx.state = 1;
|
|||
|
elelab.unart_manage.com_manage[port].init_port.write_close_channel_led(idx);
|
|||
|
|
|||
|
}
|
|||
|
break;
|
|||
|
// a.write_clear_comment(id);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 熄灭端口的多个标签
|
|||
|
/// 通道灯需要手动控制熄灭
|
|||
|
/// </summary>
|
|||
|
/// <param name="portNo">端口号</param>
|
|||
|
/// <param name="ids">标签ID数组</param>
|
|||
|
public static void turnOffLights(int portNo,int[] ids)
|
|||
|
{
|
|||
|
foreach (int id in ids)
|
|||
|
{
|
|||
|
turnOffLightOfPort(portNo, id);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 复位指定所有标签、通道灯设备
|
|||
|
/// </summary>
|
|||
|
/// <param name="port">端口号</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static void resetDevice()
|
|||
|
{
|
|||
|
foreach (int i in activeComports)
|
|||
|
{
|
|||
|
resetDevice(i);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 复位指定端口的标签和通道灯
|
|||
|
/// </summary>
|
|||
|
/// <param name="port">端口号</param>
|
|||
|
/// <returns></returns>
|
|||
|
public static string resetDevice(int port)
|
|||
|
{
|
|||
|
int port_num;
|
|||
|
try
|
|||
|
{
|
|||
|
port_num = Convert.ToInt16(port);
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
port_num = 1;
|
|||
|
}
|
|||
|
if (elelab.unart_manage.com_manage[port_num] != null)
|
|||
|
{
|
|||
|
elelab.unart_manage.com_manage[port_num].write_reset_device();
|
|||
|
}
|
|||
|
return "<result>执行成功</result>";
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public static Dictionary<int, List<int>> channalLights = new Dictionary<int, List<int>>();
|
|||
|
public static void searchLight()
|
|||
|
{
|
|||
|
channalLights.Clear();
|
|||
|
|
|||
|
Model.modify_id id;
|
|||
|
|
|||
|
// System.Threading.Thread.Sleep(50);
|
|||
|
foreach (int port in DNLights.activeComports)
|
|||
|
{
|
|||
|
if (port == 0)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
for (int i = 5000; i <= 5100; i++)
|
|||
|
{
|
|||
|
id.order = 0;
|
|||
|
id.new_ele_id = 0;
|
|||
|
id.old_ele_id = i;
|
|||
|
id.state = 1;
|
|||
|
bool ret = unart_manage.com_manage[port].init_port.search_light_id(id);
|
|||
|
if (ret)
|
|||
|
{
|
|||
|
if (!channalLights.ContainsKey(port))
|
|||
|
{
|
|||
|
channalLights[port] = new List<int>();
|
|||
|
}
|
|||
|
channalLights[port].Add(i);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
#region privatefuncs
|
|||
|
|
|||
|
private static string getLightKey(int orderNo,int color, int labelId, int address,int count)
|
|||
|
{
|
|||
|
return string.Format("{0}-{1}-{2}", orderNo, color, labelId);
|
|||
|
|
|||
|
// return string.Format("{0}-{1}-{2}-{3}-{4}",orderNo, color, labelId, address,count);
|
|||
|
}
|
|||
|
|
|||
|
private static byte get_byte(string data)
|
|||
|
{
|
|||
|
int length = data.Length;
|
|||
|
List<string> str = new List<string>();
|
|||
|
for (int i = 0; i < length; i++)
|
|||
|
{
|
|||
|
string result = data.Substring(i, 1);
|
|||
|
char aaaq = Convert.ToChar(result);
|
|||
|
if ((aaaq >= 0x30) && (aaaq <= 0x39))
|
|||
|
{
|
|||
|
str.Add(data.Substring(i, 1));
|
|||
|
}
|
|||
|
}
|
|||
|
if (str.Count == 1)
|
|||
|
{
|
|||
|
return Convert.ToByte(str[0]);
|
|||
|
}
|
|||
|
else if (str.Count == 2)
|
|||
|
{
|
|||
|
int len = Convert.ToInt16(str[0]);
|
|||
|
int len1 = Convert.ToInt16(str[1]);
|
|||
|
len = len * 10 + len1;
|
|||
|
return Convert.ToByte(len);
|
|||
|
}
|
|||
|
else if (str.Count == 3)
|
|||
|
{
|
|||
|
int len = Convert.ToInt16(str[0]);
|
|||
|
int len1 = Convert.ToInt16(str[1]);
|
|||
|
int len2 = Convert.ToInt16(str[2]);
|
|||
|
|
|||
|
len = len * 100 + len1 * 10 + len2;
|
|||
|
return Convert.ToByte(len);
|
|||
|
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private static void init_sys_port(int pickType)
|
|||
|
{
|
|||
|
|
|||
|
activeComports.Clear();
|
|||
|
clear_order order = new clear_order();
|
|||
|
int port;
|
|||
|
for (int i = 0; i < unart_manage.com_count; i++)//init com0-10
|
|||
|
{
|
|||
|
int com = Convert.ToByte(i);
|
|||
|
if (elelab.unart_manage.init_com_sys(com, true) == true)
|
|||
|
{
|
|||
|
port = Convert.ToInt16(i);
|
|||
|
// order = Convert.ToInt16(dt.Rows[i]["ele_order"]);
|
|||
|
order.order = Convert.ToByte(i);
|
|||
|
order.state = Convert.ToByte( pickType);
|
|||
|
if (unart_manage.com_manage[port].clear_order(order))
|
|||
|
{
|
|||
|
activeComports.Add(port);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
init_pick();
|
|||
|
// clear_order(dt);
|
|||
|
}
|
|||
|
private static void get_data(string data, out int dot_station, out int total_num)
|
|||
|
{
|
|||
|
data = Convert.ToDouble(data).ToString();
|
|||
|
string[] count = data.Split('.');//Out_count
|
|||
|
int full = Convert.ToInt16(count[0]);
|
|||
|
int dot_num;
|
|||
|
dot_station = 0;
|
|||
|
total_num = full;
|
|||
|
if (count.Length > 1)
|
|||
|
{
|
|||
|
dot_num = Convert.ToInt16(count[1]);
|
|||
|
if (dot_num > 0)
|
|||
|
{
|
|||
|
if (dot_num < 10)
|
|||
|
{
|
|||
|
total_num = full * 10 + dot_num;
|
|||
|
dot_station = 1;
|
|||
|
}
|
|||
|
else if ((dot_num > 9) && (dot_num < 100))
|
|||
|
{
|
|||
|
total_num = full * 100 + dot_num;
|
|||
|
dot_station = 2;
|
|||
|
}
|
|||
|
else if ((dot_num > 99) && (dot_num < 1000))
|
|||
|
{
|
|||
|
total_num = full * 1000 + dot_num;
|
|||
|
dot_station = 3;
|
|||
|
}
|
|||
|
else if ((dot_num > 999) && (dot_num < 10000))
|
|||
|
{
|
|||
|
total_num = full * 10000 + dot_num;
|
|||
|
dot_station = 4;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
dot_station = 0;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private static void read_single(int address)
|
|||
|
{
|
|||
|
while (unart_manage.com_manage[address].manage_address != unart_manage.com_manage[address].receive_address)
|
|||
|
{
|
|||
|
ele_lab_data[receive_add] = unart_manage.com_manage[address].return_data[unart_manage.com_manage[address].manage_address];
|
|||
|
receive_add++;
|
|||
|
if (receive_add >= add_max)
|
|||
|
{
|
|||
|
receive_add = 0;
|
|||
|
}
|
|||
|
unart_manage.com_manage[address].manage_address++;
|
|||
|
if (unart_manage.com_manage[address].manage_address >= unart_manage.com_manage[address].return_max)
|
|||
|
{
|
|||
|
unart_manage.com_manage[address].manage_address = 0;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private static void receive_data()
|
|||
|
{
|
|||
|
for (int i = 0; i < unart_manage.com_manage.Length; i++)
|
|||
|
{
|
|||
|
if (unart_manage.com_manage[i] != null)
|
|||
|
{
|
|||
|
if (unart_manage.com_manage[i].wait_data == true)
|
|||
|
{//读取数据
|
|||
|
unart_manage.com_manage[i].wait_data = false;
|
|||
|
read_single(i);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private static string receive_data_dot(int count, int dot_station)
|
|||
|
{
|
|||
|
string result = "0";
|
|||
|
if (dot_station == 0)
|
|||
|
{
|
|||
|
result = count.ToString();
|
|||
|
}
|
|||
|
else if (dot_station == 1)
|
|||
|
{
|
|||
|
result = (count / 10).ToString() + "." + (count % 10).ToString();
|
|||
|
}
|
|||
|
else if (dot_station == 2)
|
|||
|
{
|
|||
|
result = (count / 100).ToString() + "." + (count % 100).ToString();
|
|||
|
}
|
|||
|
else if (dot_station == 3)
|
|||
|
{
|
|||
|
result = (count / 1000).ToString() + "." + (count % 1000).ToString();
|
|||
|
}
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
private static void manage_data()
|
|||
|
{
|
|||
|
returnData();
|
|||
|
}
|
|||
|
private static bool read_state()
|
|||
|
{
|
|||
|
for (int i = 0; i < unart_manage.com_manage.Length; i++)
|
|||
|
{
|
|||
|
if (unart_manage.com_manage[i] != null)
|
|||
|
{
|
|||
|
if (unart_manage.com_manage[i].wait_data == true)
|
|||
|
{//读取数据
|
|||
|
return true;
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
#endregion
|
|||
|
public static void init_pick()
|
|||
|
{
|
|||
|
//为数据处理开辟线程
|
|||
|
// Thread workTicketThread;
|
|||
|
//Thread workTicketThread;
|
|||
|
if (pick_work != null)
|
|||
|
return;
|
|||
|
pick_work = new Thread(pick_sys);
|
|||
|
// Make this a background thread, so it will terminate when the main thread/process is de-activated
|
|||
|
pick_work.IsBackground = true;
|
|||
|
//pick_work.SetApartmentState(ApartmentState.STA);
|
|||
|
pick_work.SetApartmentState(ApartmentState.MTA);
|
|||
|
// Start the Work
|
|||
|
pick_work.Start();
|
|||
|
//workerThreads[i] = workTicketThread;
|
|||
|
}
|
|||
|
|
|||
|
public static void init_pick_support()
|
|||
|
{
|
|||
|
//为数据处理开辟线程
|
|||
|
// Thread workTicketThread;
|
|||
|
//Thread workTicketThread;
|
|||
|
|
|||
|
|
|||
|
if (pick_work_support != null)
|
|||
|
return;
|
|||
|
|
|||
|
pick_work_support = new Thread(pick_sys_support);
|
|||
|
// Make this a background thread, so it will terminate when the main thread/process is de-activated
|
|||
|
pick_work_support.IsBackground = true;
|
|||
|
//pick_work.SetApartmentState(ApartmentState.STA);
|
|||
|
pick_work_support.SetApartmentState(ApartmentState.MTA);
|
|||
|
// Start the Work
|
|||
|
pick_work_support.Start();
|
|||
|
//workerThreads[i] = workTicketThread;
|
|||
|
}
|
|||
|
|
|||
|
public static void wake_thread()
|
|||
|
{
|
|||
|
|
|||
|
// if (Convert.ToInt16(pick_work.ThreadState) == 68)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
Console.WriteLine("pick_work.ThreadState " + pick_work.ThreadState);
|
|||
|
if (Convert.ToInt16(pick_work.ThreadState) == 68)
|
|||
|
{
|
|||
|
pick_work.Resume();
|
|||
|
Console.WriteLine("2. pick_work.Resumed" );
|
|||
|
Console.WriteLine("pick_work.ThreadState " + pick_work.ThreadState);
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception er)
|
|||
|
{
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public static void wake_support_thread()
|
|||
|
{
|
|||
|
Console.WriteLine("pick_work_support.ThreadState " + pick_work_support.ThreadState);
|
|||
|
if (Convert.ToInt16(pick_work_support.ThreadState) == 68)
|
|||
|
{
|
|||
|
pick_work_support.Resume();
|
|||
|
Console.WriteLine("5. pick_work_support.Resumed");
|
|||
|
}
|
|||
|
}
|
|||
|
public static bool clear_dis(byte port, byte ele_order_id)
|
|||
|
{
|
|||
|
clear_order order = new clear_order();
|
|||
|
order.order = Convert.ToByte(ele_order_id);
|
|||
|
order.state = 1;
|
|||
|
return unart_manage.com_manage[port].clear_order(order);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
public static void pick_sys()
|
|||
|
{
|
|||
|
//int count;Threads_state
|
|||
|
// Threads_state = true;
|
|||
|
//out_start_state = false;
|
|||
|
elelab.DNLights.init_pick_support();
|
|||
|
while (true)
|
|||
|
{
|
|||
|
// unart_manage.com_manage[17].return_data();
|
|||
|
pick_work_support.Suspend();
|
|||
|
//System.Threading.Thread.Sleep(15000);
|
|||
|
Console.WriteLine("4. receive and manage data");
|
|||
|
receive_data();
|
|||
|
manage_data();
|
|||
|
wake_support_thread();
|
|||
|
Console.WriteLine("6. pick_work.Suspend ");
|
|||
|
pick_work.Suspend();
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
public static void pick_sys_support()
|
|||
|
{
|
|||
|
//int count;Threads_state
|
|||
|
while (true)
|
|||
|
{
|
|||
|
System.Threading.Thread.Sleep(2000); //wm 2000 is bigger?
|
|||
|
Console.WriteLine("Thread.Sleeped 2000 ");
|
|||
|
if (read_state() == true)
|
|||
|
{
|
|||
|
wake_thread();
|
|||
|
}
|
|||
|
Console.WriteLine(" pick_work_support.Suspend ");
|
|||
|
pick_work_support.Suspend();
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 关闭通道灯
|
|||
|
/// </summary>
|
|||
|
/// <param name="port">端口号</param>
|
|||
|
/// <param name="id">通道灯ID</param>
|
|||
|
public static void close_channel_led(int port,int id)
|
|||
|
{
|
|||
|
dnwms dn = (unart_manage.com_manage[get_byte((port).ToString() )] ).init_port;
|
|||
|
byte[] aa = new byte[5];
|
|||
|
int count = 0;
|
|||
|
aa[count++] = 0;
|
|||
|
aa[count++] = Convert.ToByte(id/ 256);
|
|||
|
aa[count++] = Convert.ToByte(id % 256);
|
|||
|
aa[count++] = 0x30;
|
|||
|
dn.manage_usart_package(aa) ;
|
|||
|
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 点亮通道灯
|
|||
|
/// </summary>
|
|||
|
/// <param name="port">端口号</param>
|
|||
|
/// <param name="id">通道灯ID</param>
|
|||
|
public static void open_channel_led(int port, int id)
|
|||
|
{
|
|||
|
dnwms dn = (unart_manage.com_manage[get_byte((port).ToString())]).init_port;
|
|||
|
|
|||
|
if (dn == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
// dn.on_close_channel += new dn_wms.dnrj.close_channel(channelLightEvent);
|
|||
|
// dn.on_open_channel += new dn_wms.dnrj.open_channel(channelLightEvent);
|
|||
|
byte[] aa = new byte[5];
|
|||
|
int count = 0;
|
|||
|
aa[count++] = 0;
|
|||
|
aa[count++] = Convert.ToByte(id / 256);
|
|||
|
aa[count++] = Convert.ToByte(id % 256);
|
|||
|
aa[count++] = 0x31;
|
|||
|
dn.manage_usart_package(aa);
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public static bool clear_order(int port)
|
|||
|
{//7e 02 0002 24 1d 0a 05 05 05 06 06 03 04 04 00 7f
|
|||
|
byte[] aa = new byte[6];
|
|||
|
int count = 0;
|
|||
|
aa[count++] = Convert.ToByte(port); ;// id_data.order;
|
|||
|
aa[count++] = Convert.ToByte(0xea);
|
|||
|
aa[count++] = Convert.ToByte(0xff);
|
|||
|
aa[count++] = 0x2d;//命令字
|
|||
|
aa[count++] = 0x0;
|
|||
|
dnwms dn = (unart_manage.com_manage[get_byte((port).ToString())]).init_port;
|
|||
|
|
|||
|
if (dn == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return (dn.manage_usart_package(aa));
|
|||
|
// return true;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|