//查询灯箱状态 private void btnSearchState_Click(object sender, RoutedEventArgs e) { LBModel model = null; if (LightBoxHelper.listModel.Count > 0) { model = LightBoxHelper.listModel[0]; } else { model = LightBoxHelper.SelectedModel; } foreach (LBMsg msg in model.ListLBMsg) { byte[] byDate = new byte[3]; byDate[0] = 4; byDate[1] = (byte)msg.Id; byDate[2] = (byte)(msg.Id >> 8); CanDataWithInfo can = new CanDataWithInfo(0x01, byDate, "查状态"); ECANHelper.ins.sendMessageWithInfo(can); } //全网一条指令查询,可能造成同时回复数据过多,导致模块内缓存溢出 //byte[] byDate = new byte[3]; //byDate[0] = 6; //CanDataWithInfo can = new CanDataWithInfo(0x01, byDate, ""); //ECANHelper.ins.sendMessageWithInfo(can); }
public void getBoxId() { byte[] byDate = new byte[3]; byDate[0] = 6; CanDataWithInfo can = new CanDataWithInfo(0x01, byDate, ""); ECANHelper.ins.sendMessageWithInfo(can); }
public void searchBoxes() { byte[] byData = new byte[3] { 0x06, 0, 0 }; CanDataWithInfo can = new CanDataWithInfo(0x01, byData, ""); ECANHelper.ins.sendMessageWithInfo(can); }
//发送字节数据 public void runBox(object obj) { LBMsg lBMsg = obj as LBMsg; int canId = createCanId(lBMsg.Id); byte[] by = new byte[8]; //正转时间 int runTime = (int)(lBMsg.RunTime); //保持时间 int keepTime = (int)lBMsg.KeepTime; //Console.WriteLine("id:" + lBMsg.Id +"-" + runTime + ":" + keepTime); //灯箱属于 canid-电机id对应表中的那一列, 共4列 int boxColumnInCanIdTable = createBoxByteIndex(lBMsg.Id); //正转 //运转命令 00保留, 01正转, 10反转, 11停留 CanDataWithInfo can = createRunBySend(boxColumnInCanIdTable, canId, 1, runTime); can.info = "id:" + lBMsg.Id + " 正 time:" + runTime; ECANHelper.ins.sendMessageWithInfo(can); //Console.WriteLine("send " + Untils.ToHexString(can.bytes)); //sendMessageWithInfo(bySend, "id:" + lBMsg.Id + " 正"); //Console.WriteLine("id:" + lBMsg.Id + "-正"); // if (runTime > 1) { // Thread.Sleep((runTime - 1) * 1000); // } // //停留 // CanDataWithInfo can1 = createRunBySend(boxColumnInCanIdTable, canId, 3, keepTime); // can1.info = "id:" + lBMsg.Id + " 停"; // ECANHelper.ins.sendMessageWithInfo(can1); //// Console.WriteLine("send " + Untils.ToHexString(can1.bytes)); // //sendMessageWithInfo(bySend1, "id:" + lBMsg.Id + " 停"); // //Console.WriteLine("id:" + lBMsg.Id + "-停"); // if (keepTime > 1) { // Thread.Sleep((keepTime - 1) * 1000); // } Thread.Sleep((runTime + keepTime) * 1000); //反转, 反转时间+2 , 保证转到底 CanDataWithInfo can2 = createRunBySend(boxColumnInCanIdTable, canId, 2, runTime + 2); can2.info = "id:" + lBMsg.Id + " 反 "; ECANHelper.ins.sendMessageWithInfo(can2); //Thread.Sleep(runTime * 1000); //boxEndCount = boxEndCount + 1; //Console.WriteLine("boxEndCount " + boxEndCount); //sendMessageWithInfo(bySend2, "id:" + lBMsg.Id + " 反"); //Console.WriteLine("id:" + lBMsg.Id + "-反"); }
/// <summary> /// 计算生成控制灯箱命令的数据部分 /// </summary> /// <param name="boxIndex">灯箱在canid对应表中的那一列</param> /// <param name="canId"></param> /// <param name="runOrder">运行命令 00保留, 01正转, 10反转, 11停留</param> /// <param name="time">运行时间, 单位秒</param> /// <returns></returns> private CanDataWithInfo createRunBySend(int boxIndex, int canId, byte runOrder, int time) { byte[] by = new byte[8]; byte[] byBox = new byte[2]; byBox[0] = (byte)time; byBox[1] = (byte)(runOrder << 6 | time >> 10); by[boxIndex + 1] = byBox[1]; by[boxIndex] = byBox[0]; CanDataWithInfo can = new CanDataWithInfo((uint)canId, by, ""); return(can); }
/// <summary> /// 设置灯箱ID /// </summary> /// <param name="oldId"></param> /// <param name="newId"></param> public CanDataWithInfo setBoxId(int oldId, int newId) { byte[] byData = new byte[8]; byData[0] = 1; byData[1] = (byte)oldId; byData[2] = (byte)(oldId >> 8); byData[3] = (byte)newId; byData[4] = (byte)(newId >> 8); CanDataWithInfo can = new CanDataWithInfo(0x03, byData, ""); ECANHelper.ins.sendMessageWithInfo(can); return(can); }
private void send(object obj) { CAN_OBJ frameinfo = new CAN_OBJ(); while (sendRunning) { while (!queueSendBuffer.IsEmpty) { CanDataWithInfo bi = null; bool result = queueSendBuffer.TryDequeue(out bi); //Console.WriteLine("queue count : " + queueSendBuffer.Count); if (!result || null == bi) { continue; } byte[] bySend; if (bi.bytes.Length != 8) { bySend = new byte[8]; Array.Copy(bi.bytes, 0, bySend, 0, bi.bytes.Length); } else { bySend = bi.bytes; } frameinfo.ID = bi.canId; frameinfo.SendType = sendType; frameinfo.RemoteFlag = 0; frameinfo.ExternFlag = 0; frameinfo.DataLen = (byte)bi.bytes.Length; frameinfo.data = bySend; //frameinfo.Reserved = new byte[2]; //logger.Info(Untils.ToHexString(frameinfo.data) + bi.info); //Console.WriteLine(bi.info); sendMsg(frameinfo, bi); Thread.Sleep(1); } Thread.Sleep(5); } }
private void analysis(object obj) { while (analysisRunning) { while (!queueReceiveBuffer.IsEmpty) { CanDataWithInfo bi = null; bool result = queueReceiveBuffer.TryDequeue(out bi); if (!result || null == bi) { continue; } if (bi.canId == 0) { continue; } ByteEventArgs byteEventArgs = new ByteEventArgs(DateTime.Now, new BytesWithInfo(bi.bytes, "")); eventReceived?.Invoke(this, byteEventArgs); //写入文件记录 logger.Info(byteEventArgs.getBytesString()); if (bi.canId == 0x04) { //读6个字节的数据 byte[] byData = bi.bytes; int boxId = byData[1] << 8 | byData[0]; int boxState = byData[2]; eventBoxStateChanged?.Invoke(this, new BoxStateEventArgs(boxId, boxState)); } else if (bi.canId == 0x06) { //第一个视频开始播放命令 LightBoxHelper.videoBegin = true; } Thread.Sleep(1); } Thread.Sleep(1); } }
private void receive(object obj) { CAN_OBJ vco1 = new CAN_OBJ(); while (receiveRunning) { //CAN_OBJ vco1 = new CAN_OBJ(); uint result = ECANDLL.Receive(4, 0, 0, out vco1, 1, 10000); //Console.WriteLine("GetReceiveNum result : " + result); Console.WriteLine("GetReceiveNum result data: " + Untils.ToHexString(vco1.data)); if (result != 0xFFFFFFFF && result != 0) { CanDataWithInfo can = new CanDataWithInfo(vco1.ID, vco1.data, ""); queueReceiveBuffer.Enqueue(can); } else { //Console.WriteLine("receive fault"); } Thread.Sleep(10); } }
private void sendMsg(CAN_OBJ frameinfo, CanDataWithInfo bi) { if (ECANDLL.Transmit(4, 0, 0, ref frameinfo, (ushort)1) != ECANStatus.STATUS_OK) { Console.WriteLine("Transmit : fault"); ERR_INFO info = new ERR_INFO(); if (ECANDLL.ReadErrInfo(4, 0, 0, out info) != ECANStatus.STATUS_OK) { Console.WriteLine("ReadErrInfo : fault"); } else { Console.WriteLine("ReadErrInfo : code " + info.ErrCode); } } else { //写入文件记录 //logger.Info(Untils.ToHexString(frameinfo.data) + bi.info); eventSended?.Invoke(this, new ByteEventArgs(DateTime.Now, new BytesWithInfo(frameinfo.data, bi.info))); } }
public void runBoxRepeat(object obj) { LBMsg lBMsg = obj as LBMsg; Console.WriteLine(">>>>一个灯箱运行开始, id= " + lBMsg.Id); if (lBMsg.RunTime > 0) { int canId = createCanId(lBMsg.Id); Thread.Sleep((int)lBMsg.WaitTime * 1000); for (int i = 0; i < lBMsg.RepeatCount; i++) { byte[] by = new byte[8]; //正转时间 int runTime = (int)(lBMsg.RunTime); //保持时间 int keepTime = (int)lBMsg.KeepTime; //灯箱属于 canid-电机id对应表中的那一列, 共4列 int boxColumnInCanIdTable = createBoxByteIndex(lBMsg.Id); //正转 //运转命令 00保留, 01正转, 10反转, 11停留 CanDataWithInfo can = createRunBySend(boxColumnInCanIdTable, canId, 1, runTime); can.info = "id:" + lBMsg.Id + " 正 time:" + runTime; ECANHelper.ins.sendMessageWithInfo(can); Thread.Sleep((runTime + keepTime) * 1000); //反转, 反转时间+2 , 保证转到底 CanDataWithInfo can2 = createRunBySend(boxColumnInCanIdTable, canId, 2, runTime + 1); can2.info = "id:" + lBMsg.Id + " 反 "; ECANHelper.ins.sendMessageWithInfo(can2); Thread.Sleep((runTime + 1) * 1000); Console.WriteLine(">>>>id= " + lBMsg.Id + " 的灯箱运行完 " + (i + 1) + " 次"); } } boxEndCount = boxEndCount + 1; Console.WriteLine("====一个灯箱运行结束, id= " + lBMsg.Id + ", boxEndCount = " + boxEndCount); }
private void btnAllRun_Click(object sender, RoutedEventArgs e) { var idText = tbId.Text; if (string.IsNullOrWhiteSpace(idText)) { MessageBox.Show("灯箱id为空"); } var runTimeText = tbRunTime.Text; if (string.IsNullOrWhiteSpace(runTimeText)) { MessageBox.Show("运行时间为空"); } var keepTimeText = tbKeepTime.Text; if (string.IsNullOrWhiteSpace(keepTimeText)) { MessageBox.Show("停留时间为空"); } try { var runTime = Convert.ToInt32(runTimeText); var keepTime = Convert.ToInt32(keepTimeText); LBMsg box = new LBMsg(); box.Id = Convert.ToInt32(idText); box.RunTime = runTime; box.KeepTime = keepTime; byte time = (byte)(box.RunTime); CanDataWithInfo can1 = new CanDataWithInfo(0x700, new byte[] { time, 0x40, }, "can:0x700, len:7 全网正"); ECANHelper.ins.sendMessageWithInfo(can1); Thread.Sleep((int)(box.RunTime * 1000)); Thread.Sleep((int)(box.KeepTime * 1000)); CanDataWithInfo can3 = new CanDataWithInfo(0x700, new byte[] { time, 0x80, }, "can:0x700, len:7 全网反"); ECANHelper.ins.sendMessageWithInfo(can3); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void menuAllBack_Click(object sender, RoutedEventArgs e) { CanDataWithInfo can3 = new CanDataWithInfo(0x700, new byte[] { 8, 0x80, }, "can:0x700, len:7 全网反"); ECANHelper.ins.sendMessageWithInfo(can3); }
private static void analysis() { LBMsg lastLBMsg = null; while (!quit && isRunning) { try { //BaseConfig.ins.IsPowerOn = true; //如果开机时间没到,不往下运行 if (!BaseConfig.ins.IsPowerOn || WAITED) { //Console.WriteLine(" off time"); Console.WriteLine(" Power Off"); logger.Info(" Power Off"); try { Thread.Sleep(1000); } catch (Exception) { return; } continue; } Console.WriteLine(" Power On"); logger.Info(" Power On"); //videoBegin = true; if (ConfigHelper.IS_WAIT_VIDEO) { //视频未开始, 等待 if (!videoBegin) { Thread.Sleep(200); continue; } else { videoBegin = false; } } if (null == SelectedModel) { //Thread.Sleep(1000); nextModel(null); continue; } //MessageHelper.ins.beginModel(); //CanDataWithInfo can = new CanDataWithInfo((uint)0x02, new byte[] {0x02, 0, 0 }, "can:0x02, len:3 停全网"); //Debug.Log("模式开始:" + SelectedModel.ModelName); //SelectedModel.cleanEndCount(); //获取模式中延时时间的集合 //List<double> listWaitTime = SelectedModel.listWaitTime; if (SelectedModel.runAll) { LBMsg lBMsg = SelectedModel.ListLBMsg[0]; byte time = (byte)(lBMsg.RunTime); CanDataWithInfo can1 = new CanDataWithInfo(0x700, new byte[] { time, 0x40, }, "can:0x700, len:7 全网正"); ECANHelper.ins.sendMessageWithInfo(can1); Thread.Sleep((int)(lBMsg.RunTime * 1000)); CanDataWithInfo can2 = new CanDataWithInfo(0x700, new byte[] { time, 0xC0, }, "can:0x700, len:7 全网停"); ECANHelper.ins.sendMessageWithInfo(can2); Thread.Sleep((int)(lBMsg.KeepTime * 1000)); CanDataWithInfo can3 = new CanDataWithInfo(0x700, new byte[] { time, 0x80, }, "can:0x700, len:7 全网反"); ECANHelper.ins.sendMessageWithInfo(can3); Thread.Sleep((int)((lBMsg.RunTime + 2) * 1000)); } else { List <double> listWaitTime = SelectedModel.getListWaitTime(); for (int i = 0; i < listWaitTime.Count; i++) { double waitTime = 0; double totalTime = listWaitTime[i]; if (i == 0) { waitTime = totalTime; } else { waitTime = totalTime - listWaitTime[i - 1]; } Thread.Sleep((int)(waitTime * 1000)); foreach (LBMsg lbMsg in SelectedModel.ListLBMsg) { if (lbMsg.WaitTime == totalTime) { //Debug.Log("灯箱 :" + lbMsg.Row1 + ":" + lbMsg.Column + " 开始"); //lbMsg.runAble = true; //DataComm.ins.startRunBox(lbMsg); if (lbMsg.RunTime > 0) { MessageHelper.ins.startRunBox(lbMsg); lastLBMsg = lbMsg; } } } } if (null != lastLBMsg) { int lastTime = (int)(lastLBMsg.RunTime * 2 + lastLBMsg.KeepTime); Thread.Sleep(lastTime * 1000); } } //等待灯箱转完 //while (!MessageHelper.ins.modelIsEnd()) { // Thread.Sleep(20); //} //Console.WriteLine("model is end "); //int outTimeCount = 0; //while (!SelectedModel.isRunEnd() && outTimeCount < 60) { // Thread.Sleep(2000); // outTimeCount++; //} if (RUN_ONCE) { //临时运行一次结束 RUN_ONCE = false; //暂停 MainWindow.ins.pasued(); } else { int modelWaitTime = SelectedModel.getWaitTimeMS(); byte backTime = 1; if (modelWaitTime > 1) { backTime = (byte)(modelWaitTime - 1); } //nextModelThread(); //nextModel(null); //全网反转 CanDataWithInfo can = new CanDataWithInfo(0x700, new byte[] { backTime, 0x80, }, "can:0x700, len:7 退全网"); //ECANHelper.ins.sendMessageWithInfo(can); //Debug.Log("进入等待时间 :" + modelWaitTime); //这里的模式停留时间要从最后一个灯箱转完算起 Thread.Sleep(modelWaitTime); nextModel(null); } //Thread.Sleep(2000); //Debug.Log("等待时间结束 :" + modelWaitTime); } catch (Exception e) { Console.WriteLine(e.StackTrace + ":" + e.Message); return; } } }
public void sendMessageWithInfo(uint canId, byte[] by, string info) { CanDataWithInfo bi = new CanDataWithInfo(canId, by, info); queueSendBuffer.Enqueue(bi); }
public void sendMessageWithInfo(CanDataWithInfo bi) { queueSendBuffer.Enqueue(bi); }
private static void analysis2() { LBMsg lastLBMsg = null; while (!quit && isRunning) { try { //BaseConfig.ins.IsPowerOn = true; //如果开机时间没到,不往下运行 if (!BaseConfig.ins.IsPowerOn || WAITED) { logger.Info(" Power Off"); try { Thread.Sleep(1000); } catch (Exception) { return; } continue; } logger.Info(" Power On"); //videoBegin = true; if (ConfigHelper.IS_WAIT_VIDEO) { //视频未开始, 等待 if (!videoBegin) { Thread.Sleep(200); continue; } else { videoBegin = false; } } if (null == SelectedModel) { //Thread.Sleep(1000); nextModel(null); continue; } MessageHelper.ins.beginModel(); //获取模式中延时时间的集合 if (SelectedModel.runAll) { LBMsg lBMsg = SelectedModel.ListLBMsg[0]; byte time = (byte)(lBMsg.RunTime); CanDataWithInfo can1 = new CanDataWithInfo(0x700, new byte[] { time, 0x40, }, "can:0x700, len:7 全网正"); ECANHelper.ins.sendMessageWithInfo(can1); Thread.Sleep((int)(lBMsg.RunTime * 1000)); CanDataWithInfo can2 = new CanDataWithInfo(0x700, new byte[] { time, 0xC0, }, "can:0x700, len:7 全网停"); ECANHelper.ins.sendMessageWithInfo(can2); Thread.Sleep((int)(lBMsg.KeepTime * 1000)); CanDataWithInfo can3 = new CanDataWithInfo(0x700, new byte[] { time, 0x80, }, "can:0x700, len:7 全网反"); ECANHelper.ins.sendMessageWithInfo(can3); Thread.Sleep((int)((lBMsg.RunTime + 2) * 1000)); } else { foreach (LBMsg lbMsg in SelectedModel.ListLBMsg) { MessageHelper.ins.startRunBox(lbMsg); lastLBMsg = lbMsg; } while (!MessageHelper.ins.modelIsEnd()) { Thread.Sleep(200); } } Console.WriteLine("model运行结束!"); if (RUN_ONCE) { //临时运行一次结束 RUN_ONCE = false; //暂停 MainWindow.ins.pasued(); } else { int modelWaitTime = SelectedModel.getWaitTimeMS(); byte backTime = 1; if (modelWaitTime > 1) { backTime = (byte)(modelWaitTime - 1); } //全网反转 CanDataWithInfo can = new CanDataWithInfo(0x700, new byte[] { backTime, 0x80, }, "can:0x700, len:7 退全网"); //ECANHelper.ins.sendMessageWithInfo(can); //Debug.Log("进入等待时间 :" + modelWaitTime); if (!ConfigHelper.IS_WAIT_VIDEO) { Thread.Sleep(modelWaitTime); } nextModel(null); } //Thread.Sleep(2000); //Debug.Log("等待时间结束 :" + modelWaitTime); } catch (Exception e) { Console.WriteLine(e.StackTrace + ":" + e.Message); return; } } }