diff --git a/ProductionSystem/Forms/HomeForm.cs b/ProductionSystem/Forms/HomeForm.cs index 77b2b8f..ff9643e 100644 --- a/ProductionSystem/Forms/HomeForm.cs +++ b/ProductionSystem/Forms/HomeForm.cs @@ -546,7 +546,8 @@ namespace ProductionSystem.Forms } if (readM248.val.Length == 0) { - txtProductCode.Text = "暂无条码"; + UpdateControlContent(txtProductCode, "暂无条码"); + } diff --git a/ProductionSystem/Forms/SystemSetting/EditParaColForm.Designer.cs b/ProductionSystem/Forms/SystemSetting/EditParaColForm.Designer.cs index a7a8fc3..fa41d21 100644 --- a/ProductionSystem/Forms/SystemSetting/EditParaColForm.Designer.cs +++ b/ProductionSystem/Forms/SystemSetting/EditParaColForm.Designer.cs @@ -94,7 +94,6 @@ this.测试步骤 = new DevExpress.XtraLayout.LayoutControlItem(); this.Loading = new DevExpress.XtraSplashScreen.SplashScreenManager(this, typeof(global::ProductionSystem.Forms.CommonForms.WaitForm1), true, true); this.waiterTime = new DevExpress.XtraLayout.LayoutControlItem(); - this.comboBoxEdit1 = new DevExpress.XtraEditors.ComboBoxEdit(); ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).BeginInit(); this.layoutControl1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.testStepEdit.Properties)).BeginInit(); @@ -148,7 +147,6 @@ ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem10)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.测试步骤)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.waiterTime)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.comboBoxEdit1.Properties)).BeginInit(); this.SuspendLayout(); // // layoutControl1 @@ -1053,15 +1051,6 @@ this.waiterTime.Text = "等待时间取自PLC地址"; this.waiterTime.TextSize = new System.Drawing.Size(174, 22); // - // comboBoxEdit1 - // - this.comboBoxEdit1.Location = new System.Drawing.Point(0, 0); - this.comboBoxEdit1.Name = "comboBoxEdit1"; - this.comboBoxEdit1.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] { - new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)}); - this.comboBoxEdit1.Size = new System.Drawing.Size(125, 24); - this.comboBoxEdit1.TabIndex = 3; - // // EditParaColForm // this.Appearance.BackColor = System.Drawing.Color.White; @@ -1069,7 +1058,6 @@ this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 18F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1590, 637); - this.Controls.Add(this.comboBoxEdit1); this.Controls.Add(this.layoutControl1); this.Margin = new System.Windows.Forms.Padding(6); this.Name = "EditParaColForm"; @@ -1077,7 +1065,6 @@ this.Text = "EditParaColForm"; this.Load += new System.EventHandler(this.EditParaColForm_Load); this.Controls.SetChildIndex(this.layoutControl1, 0); - this.Controls.SetChildIndex(this.comboBoxEdit1, 0); ((System.ComponentModel.ISupportInitialize)(this.layoutControl1)).EndInit(); this.layoutControl1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.testStepEdit.Properties)).EndInit(); @@ -1131,7 +1118,6 @@ ((System.ComponentModel.ISupportInitialize)(this.layoutControlItem10)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.测试步骤)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.waiterTime)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.comboBoxEdit1.Properties)).EndInit(); this.ResumeLayout(false); } @@ -1190,7 +1176,6 @@ private DevExpress.XtraLayout.LayoutControlItem layoutControlItem13; private DevExpress.XtraEditors.ComboBoxEdit protocolTypeBoxEdit; private DevExpress.XtraLayout.LayoutControlItem layoutControlItem10; - private DevExpress.XtraEditors.ComboBoxEdit comboBoxEdit1; private DevExpress.XtraEditors.ComboBoxEdit testStepEdit; private DevExpress.XtraLayout.LayoutControlItem 测试步骤; } diff --git a/ProductionSystem/ProductionSystem.csproj b/ProductionSystem/ProductionSystem.csproj index 0a7e3fa..5ba7f2c 100644 --- a/ProductionSystem/ProductionSystem.csproj +++ b/ProductionSystem/ProductionSystem.csproj @@ -349,6 +349,7 @@ + diff --git a/ProductionSystem/Untils/MFSerial.cs b/ProductionSystem/Untils/MFSerial.cs index 83bd75c..62134be 100644 --- a/ProductionSystem/Untils/MFSerial.cs +++ b/ProductionSystem/Untils/MFSerial.cs @@ -1,8 +1,15 @@ -using System.Net; +using System; +using System.Collections.Generic; +using System.Net; using System.Net.Sockets; - -namespace ConsoleApp6; using System.IO.Ports; +using System.Threading; + +namespace ConsoleApp6 +{ + + + public class MFCan_DevInfo @@ -29,7 +36,7 @@ using System.IO.Ports; { public const int CAN_MSG_TYPE_SD = 0; public const int CAN_MSG_TYPE_SR = 1; - public const int CAN_MSG_TYPE_ED = 2; + public const int CAN_MSG_TYPE_ED = 2; public const int CAN_MSG_TYPE_ER = 3; public byte Ch; @@ -44,11 +51,12 @@ using System.IO.Ports; { Dat = new byte[64]; } + public override String ToString() { String str; str = string.Format("Ch={0:D2},T={1:X8},", Ch, TimeStamp); - switch(Type) + switch (Type) { case CAN_MSG_TYPE_SD: str += string.Format("SD,ID={0:X3},L={1:D2},Dat=", Id, Dlc); @@ -63,17 +71,19 @@ using System.IO.Ports; str += string.Format("ER,ID={0:X8},L={1:D2},Dat=", Id, Dlc); break; } + for (int i = 0; i < Dlc; i++) str += string.Format(" {0:X2}", Dat[i]); return str; } } + public class MFCan_CFG { //old define public const int CHECK_MODE_NORMAL = 0; public const int CHECK_MODE_SILENT = 1; - public const int CHECK_MODE_LOOPBACK_OUT = 2; + public const int CHECK_MODE_LOOPBACK_OUT = 2; public const int CHECK_MODE_LOOPBACK_IN = 3; public const int CHECK_TYPE_NOMAL = 0; @@ -113,6 +123,7 @@ using System.IO.Ports; Ack = false; single = false; } + public MFCan_CFG(Byte ch, int nbps, int dbps) { Ch = ch; @@ -123,6 +134,7 @@ using System.IO.Ports; Ack = false; single = false; } + public MFCan_CFG() { Ch = 0; @@ -134,6 +146,7 @@ using System.IO.Ports; single = false; } } + public class MFLin_DevInfo { public int Lin_Count; @@ -152,6 +165,7 @@ using System.IO.Ports; } } + public class MFLin_MSG { public UInt32 TimeStamp; @@ -165,6 +179,7 @@ using System.IO.Ports; { Dat = new byte[64]; } + public override String ToString() { String str; @@ -174,23 +189,24 @@ using System.IO.Ports; return str; } } - + public class MFLin_CFG { public const int CHECK_MODE_CLASSIC = 0; public const int CHECK_MODE_ENHANCE = 1; public Byte Ch; - public bool Master; + public bool Master; public Byte Check; public int Bps; - - public MFLin_CFG(Byte ch,bool ms, Byte chk,int bps) + + public MFLin_CFG(Byte ch, bool ms, Byte chk, int bps) { Ch = ch; Master = ms; Check = chk; Bps = bps; } + public MFLin_CFG() { Ch = 0; @@ -199,15 +215,18 @@ using System.IO.Ports; Bps = 19200; } } + public class MFLin_IDLength { public Byte Id; public Byte Len; - public MFLin_IDLength(byte id,byte len) + + public MFLin_IDLength(byte id, byte len) { Id = id; Len = len; } + public MFLin_IDLength() { Id = 0; @@ -215,7 +234,7 @@ using System.IO.Ports; } } - + public class MFSerial { @@ -233,45 +252,45 @@ using System.IO.Ports; private const int MSG_HEAD = 0x68; private const int MSG_TAIL = 0x16; - private const int MSG_STA_HEAD = 0; + private const int MSG_STA_HEAD = 0; private const int MSG_STA_LENGTH = 1; - private const int MSG_STA_DAT = 2; - private const int MSG_STA_CS = 3; - private const int MSG_STA_TAIL = 4; + private const int MSG_STA_DAT = 2; + private const int MSG_STA_CS = 3; + private const int MSG_STA_TAIL = 4; //command code for can - private const int U2CAN_COM_ENTER = 0; - private const int U2CAN_COM_LINK = 1; - private const int U2CAN_COM_SYNC = 9; - private const int U2CAN_COM_SEND = 2; - private const int U2CAN_COM_RECV = 3; - private const int U2CAN_COM_SETCFG = 4; - private const int U2CAN_COM_SETMODE = 5; - private const int U2CAN_COM_READCFG = 6; + private const int U2CAN_COM_ENTER = 0; + private const int U2CAN_COM_LINK = 1; + private const int U2CAN_COM_SYNC = 9; + private const int U2CAN_COM_SEND = 2; + private const int U2CAN_COM_RECV = 3; + private const int U2CAN_COM_SETCFG = 4; + private const int U2CAN_COM_SETMODE = 5; + private const int U2CAN_COM_READCFG = 6; private const int U2CAN_COM_FILTER_ID = 7; - private const int U2CAN_COM_ADVSET = 10; + private const int U2CAN_COM_ADVSET = 10; private const int U2CAN_COM_ADVMSG = 11; - private const int U2LIN_COM_ENTER = 0; - private const int U2LIN_COM_LINK = 1; - private const int U2LIN_COM_SYNC = 9; + private const int U2LIN_COM_ENTER = 0; + private const int U2LIN_COM_LINK = 1; + private const int U2LIN_COM_SYNC = 9; //command code for lin - private const int U2LIN_COM_RECV = 0x10; - private const int U2LIN_COM_SEND = 0X11; - private const int U2LIN_COM_SETRES = 0x12; - private const int U2LIN_COM_SETMODE = 0x13; - private const int U2LIN_COM_EVENT = 0x14; - private const int U2LIN_COM_SETLEN = 0x15; - private const int U2LIN_COM_SETBPS = 0x16; - private const int U2LIN_COM_SERLEN = 0x17; + private const int U2LIN_COM_RECV = 0x10; + private const int U2LIN_COM_SEND = 0X11; + private const int U2LIN_COM_SETRES = 0x12; + private const int U2LIN_COM_SETMODE = 0x13; + private const int U2LIN_COM_EVENT = 0x14; + private const int U2LIN_COM_SETLEN = 0x15; + private const int U2LIN_COM_SETBPS = 0x16; + private const int U2LIN_COM_SERLEN = 0x17; private const int U2LIN_CH_MAX = 4; private const int U2CAN_CH_MAX = 4; - private Queue Q_can_ack; //can - private Queue Q_lin_ack; - private Queue Q_uni_ack; //common ack + private Queue Q_can_ack; //can + private Queue Q_lin_ack; + private Queue Q_uni_ack; //common ack private Queue Q_can_dat; private Queue Q_lin_dat; @@ -291,15 +310,18 @@ using System.IO.Ports; public string Device_CanVersion; public string Device_LinVersion; public string Device_Name; + public string Device_SN; + //lin public MFLin_CFG Lin_Cfg; public bool Lin_Is_Opened; - public int Lin_Count; - public int Lin_Count_Max = 0; - public int UL_RES_DAT_MAX = 64; + public int Lin_Count; + public int Lin_Count_Max = 0; + public int UL_RES_DAT_MAX = 64; private byte[] IdLengthBuff; + //can public bool Can_Is_Opened; public int Can_Send_Count; @@ -308,11 +330,11 @@ using System.IO.Ports; public MFSerial() { - + send_buff = new Byte[1024]; recv_buff = new Byte[1024]; msg_buff = new Byte[1024]; - IdLengthBuff = new Byte[64* U2LIN_CH_MAX]; + IdLengthBuff = new Byte[64 * U2LIN_CH_MAX]; for (int i = 0; i < IdLengthBuff.Length; i++) IdLengthBuff[i] = 0; @@ -340,18 +362,18 @@ using System.IO.Ports; LinkChar = (Byte)'L'; } - + public bool SyncTimeStamp(UInt32 ts) { Byte[] msg = new Byte[1024]; msg[0] = 0x09; - msg[1] = (Byte)(ts >> 0) ; + msg[1] = (Byte)(ts >> 0); msg[2] = (Byte)(ts >> 8); msg[3] = (Byte)(ts >> 16); msg[4] = (Byte)(ts >> 24); - write_msg(msg, 5); + write_msg(msg, 5); return true; } @@ -359,8 +381,8 @@ using System.IO.Ports; { if (DataStream != null) return false; if (Can_Is_Opened) return true; - - if(Serial == null) + + if (Serial == null) { Serial = new SerialPort(); Serial.Parity = System.IO.Ports.Parity.None; @@ -374,9 +396,10 @@ using System.IO.Ports; Serial.WriteTimeout = 50; Serial.DtrEnable = true; } + if (!Serial.IsOpen) { - + try { Serial.PortName = portname; @@ -427,6 +450,7 @@ using System.IO.Ports; if (c == 0) break; msg_buff[i] = c; } + msg_buff[i] = 0; Device_Name = System.Text.Encoding.Default.GetString(msg_buff, 0, i); @@ -438,18 +462,25 @@ using System.IO.Ports; if (c == 0) break; msg_buff[i] = c; } + msg_buff[i] = 0; Device_SN = System.Text.Encoding.Default.GetString(msg_buff, 0, i); } + return true; } } + try { - if(Lin_Is_Opened == false) Serial.Close(); + if (Lin_Is_Opened == false) Serial.Close(); + } + catch + { } - catch { }; + + ; return false; } @@ -469,10 +500,11 @@ using System.IO.Ports; if (Can_Is_Opened) return true; if (read_thread == null) - { + { read_thread = new Thread(new ThreadStart(read_task)); read_thread.Start(); } + Queuemutex.WaitOne(); Q_can_ack.Clear(); Queuemutex.ReleaseMutex(); @@ -505,6 +537,7 @@ using System.IO.Ports; if (c == 0) break; msg_buff[i] = c; } + msg_buff[i] = 0; Device_Name = System.Text.Encoding.Default.GetString(msg_buff, 0, i); @@ -516,17 +549,20 @@ using System.IO.Ports; if (c == 0) break; msg_buff[i] = c; } + msg_buff[i] = 0; Device_SN = System.Text.Encoding.Default.GetString(msg_buff, 0, i); } + return true; } } - + return false; } + public MFCan_DevInfo MFCanGetInfo() { MFCan_DevInfo dev; @@ -541,6 +577,7 @@ using System.IO.Ports; return dev; } + public bool CanClose() { Can_Is_Opened = false; @@ -553,14 +590,23 @@ using System.IO.Ports; Device_SN = ""; if (Serial != null) { - try { Serial.Close(); } catch { } + try + { + Serial.Close(); + } + catch + { + } + Serial = null; } + if (DataStream != null) DataStream = null; - + if (read_thread != null) read_thread.Abort(); read_thread = null; } + return true; } @@ -575,7 +621,7 @@ using System.IO.Ports; Queuemutex.ReleaseMutex(); - buff[0] = 0x40+ U2CAN_COM_SETCFG; + buff[0] = 0x40 + U2CAN_COM_SETCFG; buff[1] = cfg.Ch; buff[2] = (Byte)(cfg.NormalBps >> 0); buff[3] = (Byte)(cfg.NormalBps >> 8); @@ -595,12 +641,14 @@ using System.IO.Ports; Queuemutex.ReleaseMutex(); if (rmsg[1] == U2CAN_COM_SETCFG) return true; } + Thread.Sleep(1); } return false; } + public bool CanAdvSet(MFCan_CFG cfg) { Byte[] buff = new Byte[1024]; @@ -637,12 +685,14 @@ using System.IO.Ports; Queuemutex.ReleaseMutex(); if (rmsg[1] == U2CAN_COM_ADVSET) return true; } + Thread.Sleep(1); } return false; } + public bool CanSendOne(MFCan_MSG msg) { if (Can_Is_Opened == false) return false; @@ -669,7 +719,7 @@ using System.IO.Ports; case MFCan_MSG.CAN_MSG_TYPE_ER: buff[6] = 0xC0; break; - default:break; + default: break; } @@ -688,11 +738,12 @@ using System.IO.Ports; return false; } + public bool CanSendAdvOne(MFCan_MSG msg) { if (Can_Is_Opened == false) return false; Byte[] buff = new Byte[256]; - + buff[0] = U2CAN_COM_ADVMSG; buff[1] = (byte)msg.Ch; buff[2] = 1; @@ -703,7 +754,7 @@ using System.IO.Ports; buff[6] = (Byte)(msg.Id >> 24); buff[7] = msg.Type; - + byte n = msg.Dlc; if (n > 64) n = msg.Dlc; buff[8] = n; @@ -713,11 +764,12 @@ using System.IO.Ports; buff[9 + i] = msg.Dat[i]; - write_msg(buff, n+9); + write_msg(buff, n + 9); return false; } - public int MFCanGetMsg(MFCan_MSG[] msg,int num) + + public int MFCanGetMsg(MFCan_MSG[] msg, int num) { int cnt = 0; while (Q_can_dat.Count > 0) @@ -728,13 +780,14 @@ using System.IO.Ports; if (buff[0] == U2CAN_COM_RECV) { msg[cnt].Ch = buff[1]; - msg[cnt].TimeStamp = (uint)(buff[3 ]) + (uint)(buff[4] << 8) + (uint)(buff[5] << 16) + (uint)(buff[6] << 24); + msg[cnt].TimeStamp = (uint)(buff[3]) + (uint)(buff[4] << 8) + (uint)(buff[5] << 16) + + (uint)(buff[6] << 24); msg[cnt].Dlc = buff[9]; if ((buff[10] & 0x80) > 0) //ext { - if ((buff[10 ] & 0x40) > 0) //remote + if ((buff[10] & 0x40) > 0) //remote { msg[cnt].Type = MFCan_MSG.CAN_MSG_TYPE_ER; } @@ -744,7 +797,7 @@ using System.IO.Ports; } } - else //std + else //std { if ((buff[10] & 0x40) > 0) //remote { @@ -757,7 +810,8 @@ using System.IO.Ports; } - msg[cnt].Id = (uint)(buff[11 ]) + (uint)(buff[12] << 8) + (uint)(buff[13] << 16) + (uint)(buff[14] << 24); + msg[cnt].Id = (uint)(buff[11]) + (uint)(buff[12] << 8) + (uint)(buff[13] << 16) + + (uint)(buff[14] << 24); for (int i = 0; i < 8; i++) @@ -765,16 +819,18 @@ using System.IO.Ports; msg[cnt].Dat[i] = buff[15 + i]; } - - if(Can_Recv_Count > 0) Can_Recv_Count--; - cnt++; + + if (Can_Recv_Count > 0) Can_Recv_Count--; + cnt++; } else if (buff[0] == U2CAN_COM_ADVMSG) { msg[cnt].Ch = buff[1]; - msg[cnt].TimeStamp_100us = (uint)(buff[3]) + (uint)(buff[4] << 8) + (uint)(buff[5] << 16) + (uint)(buff[6] << 24); - msg[cnt].TimeStamp = msg[cnt].TimeStamp_100us/10; - msg[cnt].Id = (uint)(buff[7]) + (uint)(buff[8] << 8) + (uint)(buff[9] << 16) + (uint)(buff[10] << 24); + msg[cnt].TimeStamp_100us = (uint)(buff[3]) + (uint)(buff[4] << 8) + (uint)(buff[5] << 16) + + (uint)(buff[6] << 24); + msg[cnt].TimeStamp = msg[cnt].TimeStamp_100us / 10; + msg[cnt].Id = (uint)(buff[7]) + (uint)(buff[8] << 8) + (uint)(buff[9] << 16) + + (uint)(buff[10] << 24); msg[cnt].Type = buff[11]; msg[cnt].Dlc = buff[12]; @@ -787,17 +843,19 @@ using System.IO.Ports; if (Can_Recv_Count > 0) Can_Recv_Count--; cnt++; } + if (cnt >= num) break; - } + } + return cnt; } - public bool Lin_Open(string portname,int bps) + public bool Lin_Open(string portname, int bps) { if (DataStream != null) return false; if (Lin_Is_Opened == true) return true; - + if (Serial == null) { Serial = new SerialPort(); @@ -812,9 +870,10 @@ using System.IO.Ports; Serial.WriteTimeout = 50; Serial.DtrEnable = true; } + if (!Serial.IsOpen) { - + try { Serial.PortName = portname; @@ -826,12 +885,14 @@ using System.IO.Ports; return false; } } + if (read_thread == null) { read_thread = new Thread(new ThreadStart(read_task)); read_thread.Start(); } + Queuemutex.WaitOne(); Q_lin_ack.Clear(); Queuemutex.ReleaseMutex(); @@ -842,7 +903,7 @@ using System.IO.Ports; write_msg(msg, 2); Thread.Sleep(100); - + while (Q_lin_ack.Count > 0) { Queuemutex.WaitOne(); @@ -852,42 +913,52 @@ using System.IO.Ports; { Lin_Is_Opened = true; Lin_Count = Q_lin_dat.Count; - Device_LinVersion = System.Text.Encoding.Default.GetString(buff, 1,8); + Device_LinVersion = System.Text.Encoding.Default.GetString(buff, 1, 8); if (buff.Length > 9) { - int i,pos; + int i, pos; for (i = 0; i < 32; i++) { byte c = buff[10 + i]; if (c == 0) break; msg_buff[i] = c; } + msg_buff[i] = 0; Device_Name = System.Text.Encoding.Default.GetString(msg_buff, 0, i); pos = i + 10 + 1; for (i = 0; i < 32; i++) - { - if(pos + i >= buff.Length) break; + { + if (pos + i >= buff.Length) break; byte c = buff[pos + i]; if (c == 0) break; msg_buff[i] = c; } + msg_buff[i] = 0; Device_SN = System.Text.Encoding.Default.GetString(msg_buff, 0, i); } + return true; } } + try { if (Can_Is_Opened == false) Serial.Close(); - } catch { }; + } + catch + { + } + + ; return false; } + public bool Lin_Open(NetworkStream stream) { if (Serial != null) return false; @@ -907,6 +978,7 @@ using System.IO.Ports; read_thread = new Thread(new ThreadStart(read_task)); read_thread.Start(); } + Queuemutex.WaitOne(); Q_lin_ack.Clear(); Queuemutex.ReleaseMutex(); @@ -938,6 +1010,7 @@ using System.IO.Ports; if (c == 0) break; msg_buff[i] = c; } + msg_buff[i] = 0; Device_Name = System.Text.Encoding.Default.GetString(msg_buff, 0, i); @@ -949,10 +1022,12 @@ using System.IO.Ports; if (c == 0) break; msg_buff[i] = c; } + msg_buff[i] = 0; Device_SN = System.Text.Encoding.Default.GetString(msg_buff, 0, i); } + return true; } } @@ -960,6 +1035,7 @@ using System.IO.Ports; return false; } + public MFLin_DevInfo MFLinGetInfo() { MFLin_DevInfo dev; @@ -973,30 +1049,39 @@ using System.IO.Ports; return dev; } - + public bool MFLinIsOpen() { return Lin_Is_Opened; } - + public bool LinClose() { Lin_Is_Opened = false; Device_LinVersion = ""; if (Can_Is_Opened == false) - { + { Device_Name = ""; Device_SN = ""; if (Serial != null) { - try { Serial.Close(); } catch { } + try + { + Serial.Close(); + } + catch + { + } + Serial = null; } + if (DataStream != null) DataStream = null; if (read_thread != null) read_thread.Abort(); read_thread = null; - + } + return true; } @@ -1019,23 +1104,24 @@ using System.IO.Ports; if (cfg.Check == MFLin_CFG.CHECK_MODE_ENHANCE) buff[2] |= 1 << 2; write_msg(buff, 3); - - for(int i = 0; i< 200; i++) + + for (int i = 0; i < 200; i++) { - if(Q_lin_ack.Count > 0) + if (Q_lin_ack.Count > 0) { Queuemutex.WaitOne(); Byte[] rmsg = Q_lin_ack.Dequeue(); Queuemutex.ReleaseMutex(); if (rmsg[0] == U2LIN_COM_SETMODE) goto setbps; } + Thread.Sleep(1); } return false; -setbps: + setbps: buff[0] = 0xd6; buff[1] = cfg.Ch; @@ -1059,7 +1145,7 @@ setbps: if ((rmsg[2] & 0x01) != 0) Lin_Cfg.Master = true; else Lin_Cfg.Master = false; - if (((rmsg[2]>>2)& 0x03) == 0x01) Lin_Cfg.Check = MFLin_CFG.CHECK_MODE_ENHANCE; + if (((rmsg[2] >> 2) & 0x03) == 0x01) Lin_Cfg.Check = MFLin_CFG.CHECK_MODE_ENHANCE; else Lin_Cfg.Check = MFLin_CFG.CHECK_MODE_CLASSIC; Lin_Cfg.Bps = rmsg[5] << 0 | rmsg[6] << 8 | rmsg[7] << 16 | rmsg[8] << 24; @@ -1067,6 +1153,7 @@ setbps: return true; } } + Thread.Sleep(1); } @@ -1074,11 +1161,11 @@ setbps: return false; } - public bool LinSetIdLength(byte ch,MFLin_IDLength[] set) + public bool LinSetIdLength(byte ch, MFLin_IDLength[] set) { Byte[] buff = new Byte[1024]; - if(ch >= U2LIN_CH_MAX) return false; + if (ch >= U2LIN_CH_MAX) return false; if (Lin_Is_Opened == false) return false; Queuemutex.WaitOne(); @@ -1092,8 +1179,8 @@ setbps: len = set[i].Len; if (id < 0x40 && len < UL_RES_DAT_MAX) { - IdLengthBuff[ch*64+id] = len; - } + IdLengthBuff[ch * 64 + id] = len; + } } buff[0] = U2LIN_COM_SETLEN | 0x40; @@ -1109,14 +1196,17 @@ setbps: Queuemutex.WaitOne(); Byte[] rmsg = Q_lin_ack.Dequeue(); Queuemutex.ReleaseMutex(); - if (rmsg[0] == U2LIN_COM_ENTER && rmsg[1] == U2LIN_COM_SETLEN) return true; + if (rmsg[0] == U2LIN_COM_ENTER && rmsg[1] == U2LIN_COM_SETLEN) return true; } + Thread.Sleep(1); } + return false; } - public bool LinUpLoad(MFLin_MSG Msg,bool ack) + + public bool LinUpLoad(MFLin_MSG Msg, bool ack) { Byte[] buff = new Byte[1024]; @@ -1126,14 +1216,14 @@ setbps: Q_lin_ack.Clear(); Queuemutex.ReleaseMutex(); - buff[0] = U2LIN_COM_SETRES ; + buff[0] = U2LIN_COM_SETRES; if (ack) buff[0] |= 0x40; buff[1] = Msg.Ch; buff[2] = 1; buff[3] = Msg.Id; if (Msg.Dlc > UL_RES_DAT_MAX) Msg.Dlc = (byte)UL_RES_DAT_MAX; buff[4] = Msg.Dlc; - + Array.Copy(Msg.Dat, 0, buff, 5, Msg.Dlc); write_msg(buff, Msg.Dlc + 5); @@ -1149,21 +1239,24 @@ setbps: Queuemutex.ReleaseMutex(); if (rmsg[0] == U2LIN_COM_ENTER && rmsg[1] == U2LIN_COM_SETRES) return true; } + Thread.Sleep(1); } + return false; } - public bool LinSlaveDisable( byte ch, byte id) + + public bool LinSlaveDisable(byte ch, byte id) { MFLin_MSG res_msg = new MFLin_MSG(); res_msg.Ch = ch; res_msg.Id = id; res_msg.Dlc = 0; - return LinUpLoad(res_msg,true); + return LinUpLoad(res_msg, true); } - public bool LinMasterSend( byte ch, byte id) + public bool LinMasterSend(byte ch, byte id) { Byte[] buff = new Byte[64]; @@ -1179,8 +1272,8 @@ setbps: public bool LinMasterSendOne(MFLin_MSG Msg) { - LinUpLoad(Msg,true); - LinMasterSend( Msg.Ch, Msg.Id); + LinUpLoad(Msg, true); + LinMasterSend(Msg.Ch, Msg.Id); return true; } @@ -1196,7 +1289,8 @@ setbps: if (buff[0] == U2LIN_COM_RECV) { msg[cnt].Ch = buff[1]; - msg[cnt].TimeStamp = (uint)(buff[2]) + (uint)(buff[3] << 8) + (uint)(buff[4] << 16) + (uint)(buff[5] << 24); + msg[cnt].TimeStamp = (uint)(buff[2]) + (uint)(buff[3] << 8) + (uint)(buff[4] << 16) + + (uint)(buff[5] << 24); msg[cnt].Err = buff[6]; msg[cnt].Id = buff[7]; msg[cnt].Dlc = (byte)buff[8]; @@ -1208,19 +1302,23 @@ setbps: else if (buff[0] == U2LIN_COM_EVENT) { msg[cnt].Ch = buff[1]; - msg[cnt].TimeStamp = (uint)(buff[2]) + (uint)(buff[3] << 8) + (uint)(buff[4] << 16) + (uint)(buff[5] << 24); + msg[cnt].TimeStamp = (uint)(buff[2]) + (uint)(buff[3] << 8) + (uint)(buff[4] << 16) + + (uint)(buff[5] << 24); msg[cnt].Err = (byte)(buff[6] + 6); msg[cnt].Id = 0; msg[cnt].Dlc = 0; } + if (Lin_Count > 0) Lin_Count--; if (++cnt >= num) break; } + return cnt; } + // for class self public bool write_msg(Byte[] msg, int num) { @@ -1237,6 +1335,7 @@ setbps: send_buff[3 + i] = msg[i]; csum += msg[i]; } + send_buff[3 + i] = (Byte)(csum & 0xff); send_buff[3 + i + 1] = MSG_TAIL; num += 5; @@ -1244,27 +1343,32 @@ setbps: try { if (DataStream != null) DataStream.Write(send_buff, 0, num); - else if(Serial != null) Serial.Write(send_buff, 0, num); - + else if (Serial != null) Serial.Write(send_buff, 0, num); + + } + catch + { } - catch { }; - + + ; + Writemutex.ReleaseMutex(); return true; } - + private void read_task() { int ret; while (true) - { + { ret = read_dat_proc(); - if(ret < 0) Thread.Sleep(2); - else Thread.Sleep(20); + if (ret < 0) Thread.Sleep(2); + else Thread.Sleep(20); } } + private int read_dat_proc() { int i; @@ -1279,6 +1383,7 @@ setbps: { return -1; } + for (i = 0; i < num; i++) { switch (MsgState) @@ -1290,6 +1395,7 @@ setbps: MsgRp = 0; MsgLen = 2; } + break; case MSG_STA_LENGTH: msg_buff[MsgRp] = recv_buff[i]; @@ -1309,6 +1415,7 @@ setbps: MsgRp = 0; } } + break; case MSG_STA_DAT: msg_buff[MsgRp] = recv_buff[i]; @@ -1318,6 +1425,7 @@ setbps: { MsgState = MSG_STA_CS; } + break; case MSG_STA_CS: if ((MsgCs & 0xff) == recv_buff[i]) MsgState = MSG_STA_TAIL; @@ -1328,6 +1436,7 @@ setbps: { msg_push(msg_buff, MsgLen); } + MsgState = MSG_STA_HEAD; break; default: @@ -1336,12 +1445,14 @@ setbps: } } + return num; } + private void msg_push(Byte[] msg, int len) { - if ((msg[0] & 0x80) == 0) return;//no dir flag + if ((msg[0] & 0x80) == 0) return; //no dir flag msg[0] = (Byte)(msg[0] & 0x3f); Byte[] pbuff = new Byte[len]; Array.Copy(msg, pbuff, len); @@ -1351,11 +1462,13 @@ setbps: { case 0x00: if (msg[1] >= 0x02 && msg[1] <= 0x0f) - { //can ack + { + //can ack Q_can_ack.Enqueue(pbuff); } else if (msg[1] >= 0x10 && msg[1] <= 0x1e) - { //lin ack + { + //lin ack Q_lin_ack.Enqueue(pbuff); } else @@ -1363,6 +1476,7 @@ setbps: //uni_ack Q_uni_ack.Enqueue(pbuff); } + break; case 0x01: //link if (msg[3] == 'C') @@ -1371,66 +1485,68 @@ setbps: Q_can_ack.Enqueue(pbuff); } + if (msg[3] == 'L') { //lin ack Q_lin_ack.Enqueue(pbuff); } + break; - case 0x03: //can data + case 0x03: //can data + { + int n = pbuff[2]; + int pos = 3; + for (int i = 0; i < n; i++) { - int n = pbuff[2]; - int pos = 3; - for(int i = 0; i< n; i++) - { - if (Can_Recv_Count > Can_Recv_Count_Max) break; - byte[] mbuff = new Byte[23]; - mbuff[0] = pbuff[0]; - mbuff[1] = pbuff[1]; - mbuff[2] = 1; - if (pos+20 > pbuff.Length) break; - Array.Copy(pbuff, pos, mbuff ,3, 20); - Q_can_dat.Enqueue(mbuff); - - pos += 20; - Can_Recv_Count++; - } - + if (Can_Recv_Count > Can_Recv_Count_Max) break; + byte[] mbuff = new Byte[23]; + mbuff[0] = pbuff[0]; + mbuff[1] = pbuff[1]; + mbuff[2] = 1; + if (pos + 20 > pbuff.Length) break; + Array.Copy(pbuff, pos, mbuff, 3, 20); + Q_can_dat.Enqueue(mbuff); + + pos += 20; + Can_Recv_Count++; } + + } break; case 0x0b: //adv can data - { - int n = pbuff[2]; - int pos = 3; - - for (int i = 0; i < n; i++) - { - if (Can_Recv_Count > Can_Recv_Count_Max) break; + { + int n = pbuff[2]; + int pos = 3; - if (pos + 5 > pbuff.Length) break; + for (int i = 0; i < n; i++) + { + if (Can_Recv_Count > Can_Recv_Count_Max) break; - int datlen = pbuff[pos + 9]; - if (datlen > 64) break; + if (pos + 5 > pbuff.Length) break; - byte[] mbuff = new Byte[3 + 10 + datlen]; + int datlen = pbuff[pos + 9]; + if (datlen > 64) break; - mbuff[0] = pbuff[0]; - mbuff[1] = pbuff[1]; - mbuff[2] = 1; + byte[] mbuff = new Byte[3 + 10 + datlen]; - if (pos + 10 + datlen > pbuff.Length) break; + mbuff[0] = pbuff[0]; + mbuff[1] = pbuff[1]; + mbuff[2] = 1; - Array.Copy(pbuff, pos, mbuff, 3, 10 + datlen); - Q_can_dat.Enqueue(mbuff); + if (pos + 10 + datlen > pbuff.Length) break; - pos += 10 + datlen; - Can_Recv_Count++; + Array.Copy(pbuff, pos, mbuff, 3, 10 + datlen); + Q_can_dat.Enqueue(mbuff); + pos += 10 + datlen; + Can_Recv_Count++; - } } + } + break; case 0x10: case 0x14: @@ -1439,53 +1555,61 @@ setbps: break; default: if (msg[0] >= 0x02 && msg[0] <= 0x0f) - { //can ack + { + //can ack Q_can_ack.Enqueue(pbuff); } else if (msg[0] >= 0x10 && msg[0] <= 0x1e) - { //link ack + { + //link ack Q_lin_ack.Enqueue(pbuff); } else - {// other + { + // other Q_uni_ack.Enqueue(pbuff); } + break; } + Queuemutex.ReleaseMutex(); } - + } - class MFNetDevice + class MFNetDevice { public string Name; public string ServerPort; - public string Ip; + public string Ip; + } + + struct UdpState + { + public UdpClient u; + public IPEndPoint e; } - struct UdpState - { - public UdpClient u; - public IPEndPoint e; - } class MFNetDeviceScanner { public bool Scanning; private UdpState sta; - List DevList; + List DevList; public delegate void ScanDevResult(MFNetDevice dev); public event ScanDevResult FoundADevice; + public MFNetDeviceScanner() { Scanning = false; DevList = new List(); } - public void ReceiveCallback(IAsyncResult ar) + + public void ReceiveCallback(IAsyncResult ar) { UdpClient u = ((UdpState)(ar.AsyncState)).u; IPEndPoint e = ((UdpState)(ar.AsyncState)).e; @@ -1494,15 +1618,16 @@ setbps: { receiveBytes = u.EndReceive(ar, ref e); } - catch { + catch + { Scanning = false; return; } - + string receiveString = System.Text.Encoding.ASCII.GetString(receiveBytes); string[] strs = receiveString.Split(' '); - if(strs.Length == 2) + if (strs.Length == 2) { MFNetDevice dev = new MFNetDevice(); dev.Ip = e.Address.ToString(); @@ -1510,20 +1635,17 @@ setbps: dev.ServerPort = strs[1]; MFNetDevice result = DevList.Find( - delegate (MFNetDevice d) - { - return d.Name == dev.Name; - } - ); + delegate(MFNetDevice d) { return d.Name == dev.Name; } + ); if (result == null) { DevList.Add(dev); if (FoundADevice != null) FoundADevice(dev); - } + } } - + try { u.BeginReceive(new AsyncCallback(ReceiveCallback), sta); @@ -1532,9 +1654,10 @@ setbps: { Scanning = false; } - - + + } + public bool Start(int port) { if (Scanning) return true; @@ -1549,18 +1672,28 @@ setbps: { u.BeginReceive(new AsyncCallback(ReceiveCallback), sta); } - catch { return false; } + catch + { + return false; + } + Scanning = true; return true; } + public bool Stop() { try { - if(Scanning) sta.u.Close(); + if (Scanning) sta.u.Close(); } - catch { }; + catch + { + } + + ; Scanning = false; return true; } - } \ No newline at end of file + } +} \ No newline at end of file diff --git a/ProductionSystem_UserControl/ExtendedControls/MyParaControl.Designer.cs b/ProductionSystem_UserControl/ExtendedControls/MyParaControl.Designer.cs index b5f3d5e..4f28cc1 100644 --- a/ProductionSystem_UserControl/ExtendedControls/MyParaControl.Designer.cs +++ b/ProductionSystem_UserControl/ExtendedControls/MyParaControl.Designer.cs @@ -125,7 +125,7 @@ this.txtMaxVal.Location = new System.Drawing.Point(200, 5); this.txtMaxVal.Name = "txtMaxVal"; this.txtMaxVal.Properties.AutoHeight = false; - this.txtMaxVal.Properties.NullValuePrompt = "上限"; + this.txtMaxVal.Properties.NullValuePrompt = "电流上限"; this.txtMaxVal.Size = new System.Drawing.Size(64, 27); this.txtMaxVal.StyleController = this.layoutControl1; this.txtMaxVal.TabIndex = 5; @@ -135,7 +135,7 @@ this.txtMinVal.Location = new System.Drawing.Point(274, 5); this.txtMinVal.Name = "txtMinVal"; this.txtMinVal.Properties.AutoHeight = false; - this.txtMinVal.Properties.NullValuePrompt = "下限"; + this.txtMinVal.Properties.NullValuePrompt = "电流下限"; this.txtMinVal.Size = new System.Drawing.Size(64, 27); this.txtMinVal.StyleController = this.layoutControl1; this.txtMinVal.TabIndex = 6;