Commit 6a9b44ec LN

1

1 个父辈 25fc7d60
正在显示 51 个修改的文件 包含 81 行增加8770 行删除
此文件类型无法预览
此文件类型无法预览
此文件类型无法预览
......@@ -18,21 +18,16 @@ namespace OnlineStore.ACPackingStore
{
public partial class FrmAxisDebug : FrmBase
{
//private KTK_LA_BoxBean store;
{
private ConfigMoveAxis middle = null;
private ConfigMoveAxis updown = null;
//private ConfigMoveAxis compress = null;
private ConfigMoveAxis updown = null;
private ConfigMoveAxis inout = null;
private ConfigMoveAxis comp = null;
//private int compress_Slv = 0;
private BoxBean boxBean = null;
// public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private ConfigMoveAxis comp = null;
private BoxBean boxBean = null;
public FrmAxisDebug(BoxBean boxBean)
{
middle = boxBean.Config.Middle_Axis;
updown = boxBean.Config.UpDown_Axis;
//compress_Slv = boxBean.Config.CompressAxis_Slv;
updown = boxBean.Config.UpDown_Axis;
comp = boxBean.Config.Comp_Axis;
inout = boxBean.Config.InOut_Axis;
......@@ -55,17 +50,11 @@ namespace OnlineStore.ACPackingStore
txtUpDownSpeed.Text = (updown.TargetSpeed / 5).ToString();
txtComSpeed.Text = (comp.TargetSpeed/5).ToString();
timer1.Start();
}
/// <summary>
/// 判断进出轴是否在P1点
/// </summary>
}
private bool InOutIsIsP1()
{
int InOutDefaultPosition = ConfigAppSettings.GetIntValue(Setting_Init.InOutDefaultPosition);
//if (InOutDefaultPosition > 3000 || InOutDefaultPosition.Equals(0))
//{
// InOutDefaultPosition = 3000;
//}
int currValue = ACServerManager.GetActualtPosition(inout.DeviceName, inout.GetAxisValue());
if (currValue <= InOutDefaultPosition)
{
......
此文件的差异太大,无法显示。
......@@ -49,7 +49,7 @@ namespace OnlineStore.ACPackingStore
}
this.groupBox3.Text = "设备[" + BoxBean.CID + "]状态";
this.Text = BoxBean.Name;
axisMoveControl1.LoadData( BoxBean.moveAxisList.ToArray());
axisMoveControl1.LoadData( new AxisBean[] { });
ACBoxPosition ktkPosition = null;
if (BoxBean.PositionNumList.Count > 0)
{
......@@ -57,8 +57,7 @@ namespace OnlineStore.ACPackingStore
cmbPosition.SelectedIndex = 0;
ktkPosition = CSVPositionReader<ACBoxPosition>.GetPositon(cmbPosition.Text);
//BoxBean.PositionNumList = positionNumList;
}
ShelfPosition shelrfP = null;
}
txtMiddleP1.Text = BoxBean.Config.MiddleAxis_P1.ToString();
......@@ -118,7 +117,7 @@ namespace OnlineStore.ACPackingStore
chbDebug.Checked = BoxBean.IsDebug;
LoadOk = true;
}
lblTemp.Text = BoxBean.humBean.currTempStr;
// lblTemp.Text = BoxBean.humBean.currTempStr;
lblWarnMsg.Text = BoxBean.GetWarnMsg();
lblMoveInfo.Text = BoxBean.GetMoveStr();
......@@ -344,7 +343,7 @@ namespace OnlineStore.ACPackingStore
string positionConfigFile = appPath + ConfigAppSettings.GetValue(Setting_Init.Store_Position_Config);
if (!File.Exists(positionConfigFile))
{
string nameStr = BoxBean.DeviceID.ToString();
string nameStr = BoxBean.ID.ToString();
positionConfigFile = positionConfigFile.Replace(".csv", "_" + nameStr + ".csv");
}
bool result = CSVPositionReader<ACBoxPosition>.SavePostion(positionConfigFile, ktkPosition);
......@@ -721,9 +720,9 @@ namespace OnlineStore.ACPackingStore
private void btnSelTemp_Click(object sender, EventArgs e)
{
HumitureParam param = BoxBean.humBean.QueryData();
txtTemp.Text = param.Temperate.ToString();
txtHum.Text = param.Humidity.ToString();
// HumitureParam param = BoxBean.humBean.QueryData();
// txtTemp.Text = param.Temperate.ToString();
// txtHum.Text = param.Humidity.ToString();
}
internal void DebugStatus(bool isDebug)
......@@ -804,7 +803,7 @@ namespace OnlineStore.ACPackingStore
ioIP = BoxBean.Config.DIList[IO_Type.Compress_Check].IO_IP;
ioIndex = BoxBean.Config.DIList[IO_Type.Compress_Check].GetIOAddr();
}
FrmPositionTool frm = new FrmPositionTool(PortName, slvAddr, ioIP, ioIndex, Text, BoxBean.DeviceID);
FrmPositionTool frm = new FrmPositionTool(PortName, slvAddr, ioIP, ioIndex, Text, BoxBean.ID);
frm.ShowDialog();
}
......@@ -827,19 +826,6 @@ namespace OnlineStore.ACPackingStore
}
}
/// <summary>
/// 忽略料叉信号,使出库继续运行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClearcurState_Click(object sender, EventArgs e)
{
if (BoxBean.MoveInfo.MoveStep.Equals(StoreMoveStep.SO_07_CheckComSig))
{
BoxBean.IsIgnoreComSig = true;
BoxBean.MoveInfo.EndStepWait();
}
}
}
}
......@@ -19,38 +19,28 @@
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.button1 = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnNGlineStop = new System.Windows.Forms.Button();
this.btnNgLine = new System.Windows.Forms.Button();
this.btnDoorStop = new System.Windows.Forms.Button();
this.btnOpenAxisBreak = new System.Windows.Forms.Button();
this.btnBlow = new System.Windows.Forms.Button();
this.btnStoreOn = new System.Windows.Forms.Button();
this.btnStopMove = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.btnCamerLed = new System.Windows.Forms.Button();
this.btnTopDown = new System.Windows.Forms.Button();
this.btnTopUp = new System.Windows.Forms.Button();
this.btnNGDoorDown = new System.Windows.Forms.Button();
this.btnNGDoorUp = new System.Windows.Forms.Button();
this.txtSlaveId = new System.Windows.Forms.TextBox();
this.btnOpenDo = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.txtDOIndex = new System.Windows.Forms.TextBox();
this.txtDoName = new System.Windows.Forms.TextBox();
this.label17 = new System.Windows.Forms.Label();
this.btnOpenAxisBreak = new System.Windows.Forms.Button();
this.label14 = new System.Windows.Forms.Label();
this.txtWriteTime = new System.Windows.Forms.TextBox();
this.btnLocationDown = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.btnLocationUp = new System.Windows.Forms.Button();
this.cmbWriteIO = new System.Windows.Forms.ComboBox();
this.btnCloseDoor = new System.Windows.Forms.Button();
this.btnOpenDoor = new System.Windows.Forms.Button();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.btnLineBack = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox4.SuspendLayout();
this.groupBox3.SuspendLayout();
......@@ -76,20 +66,12 @@
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.groupBox1.Controls.Add(this.btnLineBack);
this.groupBox1.Controls.Add(this.btnNGlineStop);
this.groupBox1.Controls.Add(this.btnNgLine);
this.groupBox1.Controls.Add(this.btnDoorStop);
this.groupBox1.Controls.Add(this.btnOpenAxisBreak);
this.groupBox1.Controls.Add(this.btnBlow);
this.groupBox1.Controls.Add(this.btnStoreOn);
this.groupBox1.Controls.Add(this.btnStopMove);
this.groupBox1.Controls.Add(this.button4);
this.groupBox1.Controls.Add(this.btnCamerLed);
this.groupBox1.Controls.Add(this.btnTopDown);
this.groupBox1.Controls.Add(this.btnTopUp);
this.groupBox1.Controls.Add(this.btnNGDoorDown);
this.groupBox1.Controls.Add(this.btnNGDoorUp);
this.groupBox1.Controls.Add(this.txtSlaveId);
this.groupBox1.Controls.Add(this.btnOpenDo);
this.groupBox1.Controls.Add(this.button2);
......@@ -102,8 +84,6 @@
this.groupBox1.Controls.Add(this.label5);
this.groupBox1.Controls.Add(this.btnLocationUp);
this.groupBox1.Controls.Add(this.cmbWriteIO);
this.groupBox1.Controls.Add(this.btnCloseDoor);
this.groupBox1.Controls.Add(this.btnOpenDoor);
this.groupBox1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.groupBox1.Location = new System.Drawing.Point(485, 8);
this.groupBox1.Name = "groupBox1";
......@@ -112,41 +92,17 @@
this.groupBox1.TabStop = false;
this.groupBox1.Text = "DO写入";
//
// btnNGlineStop
//
this.btnNGlineStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnNGlineStop.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnNGlineStop.Location = new System.Drawing.Point(14, 316);
this.btnNGlineStop.Name = "btnNGlineStop";
this.btnNGlineStop.Size = new System.Drawing.Size(125, 34);
this.btnNGlineStop.TabIndex = 298;
this.btnNGlineStop.Text = "NG线体停止";
this.btnNGlineStop.UseVisualStyleBackColor = false;
this.btnNGlineStop.Click += new System.EventHandler(this.btnNGlineStop_Click);
//
// btnNgLine
//
this.btnNgLine.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnNgLine.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnNgLine.Location = new System.Drawing.Point(14, 280);
this.btnNgLine.Name = "btnNgLine";
this.btnNgLine.Size = new System.Drawing.Size(125, 34);
this.btnNgLine.TabIndex = 297;
this.btnNgLine.Text = "NG线体运转";
this.btnNgLine.UseVisualStyleBackColor = false;
this.btnNgLine.Click += new System.EventHandler(this.btnNgLine_Click);
//
// btnDoorStop
//
this.btnDoorStop.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnDoorStop.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnDoorStop.Location = new System.Drawing.Point(217, 163);
this.btnDoorStop.Name = "btnDoorStop";
this.btnDoorStop.Size = new System.Drawing.Size(96, 45);
this.btnDoorStop.TabIndex = 296;
this.btnDoorStop.Text = "移门停止";
this.btnDoorStop.UseVisualStyleBackColor = false;
this.btnDoorStop.Click += new System.EventHandler(this.btnDoorStop_Click);
// btnOpenAxisBreak
//
this.btnOpenAxisBreak.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnOpenAxisBreak.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnOpenAxisBreak.Location = new System.Drawing.Point(14, 447);
this.btnOpenAxisBreak.Name = "btnOpenAxisBreak";
this.btnOpenAxisBreak.Size = new System.Drawing.Size(125, 34);
this.btnOpenAxisBreak.TabIndex = 252;
this.btnOpenAxisBreak.Text = "打开刹车";
this.btnOpenAxisBreak.UseVisualStyleBackColor = false;
this.btnOpenAxisBreak.Click += new System.EventHandler(this.btnOpenAxisBreak_Click);
//
// btnBlow
//
......@@ -172,30 +128,6 @@
this.btnStoreOn.UseVisualStyleBackColor = false;
this.btnStoreOn.Click += new System.EventHandler(this.btnStoreOn_Click);
//
// btnStopMove
//
this.btnStopMove.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnStopMove.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnStopMove.Location = new System.Drawing.Point(217, 221);
this.btnStopMove.Name = "btnStopMove";
this.btnStopMove.Size = new System.Drawing.Size(96, 45);
this.btnStopMove.TabIndex = 292;
this.btnStopMove.Text = "线体停止";
this.btnStopMove.UseVisualStyleBackColor = false;
this.btnStopMove.Click += new System.EventHandler(this.btnStopMove_Click);
//
// button4
//
this.button4.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button4.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button4.Location = new System.Drawing.Point(14, 221);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(96, 45);
this.button4.TabIndex = 291;
this.button4.Text = "线体正转";
this.button4.UseVisualStyleBackColor = false;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// btnCamerLed
//
this.btnCamerLed.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
......@@ -232,30 +164,6 @@
this.btnTopUp.UseVisualStyleBackColor = false;
this.btnTopUp.Click += new System.EventHandler(this.btnTopUp_Click);
//
// btnNGDoorDown
//
this.btnNGDoorDown.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnNGDoorDown.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnNGDoorDown.Location = new System.Drawing.Point(188, 316);
this.btnNGDoorDown.Name = "btnNGDoorDown";
this.btnNGDoorDown.Size = new System.Drawing.Size(125, 34);
this.btnNGDoorDown.TabIndex = 287;
this.btnNGDoorDown.Text = "NG料口门下降";
this.btnNGDoorDown.UseVisualStyleBackColor = false;
this.btnNGDoorDown.Click += new System.EventHandler(this.btnNGDoorDown_Click);
//
// btnNGDoorUp
//
this.btnNGDoorUp.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnNGDoorUp.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnNGDoorUp.Location = new System.Drawing.Point(188, 280);
this.btnNGDoorUp.Name = "btnNGDoorUp";
this.btnNGDoorUp.Size = new System.Drawing.Size(125, 34);
this.btnNGDoorUp.TabIndex = 286;
this.btnNGDoorUp.Text = "NG料口门上升";
this.btnNGDoorUp.UseVisualStyleBackColor = false;
this.btnNGDoorUp.Click += new System.EventHandler(this.btnNGDoorUp_Click);
//
// txtSlaveId
//
this.txtSlaveId.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
......@@ -324,18 +232,6 @@
this.label17.Text = "设备IP:";
this.label17.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// btnOpenAxisBreak
//
this.btnOpenAxisBreak.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnOpenAxisBreak.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnOpenAxisBreak.Location = new System.Drawing.Point(14, 447);
this.btnOpenAxisBreak.Name = "btnOpenAxisBreak";
this.btnOpenAxisBreak.Size = new System.Drawing.Size(125, 34);
this.btnOpenAxisBreak.TabIndex = 252;
this.btnOpenAxisBreak.Text = "打开刹车";
this.btnOpenAxisBreak.UseVisualStyleBackColor = false;
this.btnOpenAxisBreak.Click += new System.EventHandler(this.btnOpenAxisBreak_Click);
//
// label14
//
this.label14.AutoSize = true;
......@@ -411,30 +307,6 @@
this.cmbWriteIO.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.cmbWriteIO_DrawItem);
this.cmbWriteIO.SelectedIndexChanged += new System.EventHandler(this.cmbWriteIO_SelectedIndexChanged);
//
// btnCloseDoor
//
this.btnCloseDoor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnCloseDoor.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnCloseDoor.Location = new System.Drawing.Point(115, 163);
this.btnCloseDoor.Name = "btnCloseDoor";
this.btnCloseDoor.Size = new System.Drawing.Size(96, 45);
this.btnCloseDoor.TabIndex = 249;
this.btnCloseDoor.Text = "入口移门关闭";
this.btnCloseDoor.UseVisualStyleBackColor = false;
this.btnCloseDoor.Click += new System.EventHandler(this.btnCloseDoor_Click);
//
// btnOpenDoor
//
this.btnOpenDoor.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnOpenDoor.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnOpenDoor.Location = new System.Drawing.Point(14, 163);
this.btnOpenDoor.Name = "btnOpenDoor";
this.btnOpenDoor.Size = new System.Drawing.Size(96, 45);
this.btnOpenDoor.TabIndex = 248;
this.btnOpenDoor.Text = "入口移门打开";
this.btnOpenDoor.UseVisualStyleBackColor = false;
this.btnOpenDoor.Click += new System.EventHandler(this.btnOpenDoor_Click);
//
// groupBox4
//
this.groupBox4.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
......@@ -489,18 +361,6 @@
this.tableLayoutPanel1.Size = new System.Drawing.Size(218, 570);
this.tableLayoutPanel1.TabIndex = 102;
//
// btnLineBack
//
this.btnLineBack.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnLineBack.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnLineBack.Location = new System.Drawing.Point(115, 221);
this.btnLineBack.Name = "btnLineBack";
this.btnLineBack.Size = new System.Drawing.Size(96, 45);
this.btnLineBack.TabIndex = 299;
this.btnLineBack.Text = "线体反转";
this.btnLineBack.UseVisualStyleBackColor = false;
this.btnLineBack.Click += new System.EventHandler(this.btnLineBack_Click);
//
// FrmIOStatus
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
......@@ -540,8 +400,6 @@
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ComboBox cmbWriteIO;
private System.Windows.Forms.TextBox txtDoName;
private System.Windows.Forms.Button btnCloseDoor;
private System.Windows.Forms.Button btnOpenDoor;
private System.Windows.Forms.Button btnLocationDown;
private System.Windows.Forms.Button btnLocationUp;
private System.Windows.Forms.Button btnOpenAxisBreak;
......@@ -549,19 +407,11 @@
private System.Windows.Forms.Button btnOpenDo;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox txtSlaveId;
private System.Windows.Forms.Button btnNGDoorDown;
private System.Windows.Forms.Button btnNGDoorUp;
private System.Windows.Forms.Button btnTopDown;
private System.Windows.Forms.Button btnTopUp;
private System.Windows.Forms.Button btnStopMove;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button btnCamerLed;
private System.Windows.Forms.Button btnStoreOn;
private System.Windows.Forms.Button btnBlow;
private System.Windows.Forms.Button btnDoorStop;
private System.Windows.Forms.Button btnNGlineStop;
private System.Windows.Forms.Button btnNgLine;
private System.Windows.Forms.Button btnLineBack;
}
}
......@@ -36,7 +36,7 @@ namespace OnlineStore.ACPackingStore
InitializeComponent();
this.boxBean = store;
this.StoreId = store.DeviceID;
this.StoreId = store.ID;
LoadIOList();
}
......@@ -295,16 +295,11 @@ namespace OnlineStore.ACPackingStore
}
private void button4_Click(object sender, EventArgs e)
{
//BtnMove(button4, "线体正转", "线体反转", IO_Type.Line_Run, IO_Type.Line_BackRun);
boxBean.LineRun();
{
}
private void btnStopMove_Click(object sender, EventArgs e)
{
//boxBean.IOMove(IO_Type.Line_Run, IO_VALUE.LOW);
//boxBean.IOMove(IO_Type.Line_BackRun, IO_VALUE.LOW);
boxBean.LineStop();
{
}
private void btnStoreOn_Click(object sender, EventArgs e)
......@@ -318,8 +313,7 @@ namespace OnlineStore.ACPackingStore
}
private void btnCloseCyDo_Click(object sender, EventArgs e)
{
boxBean.DoorBean.Stop();
{
boxBean.IOMove(IO_Type.EntranceDoor_Open, IO_VALUE.LOW);
boxBean.IOMove(IO_Type.EntranceDoor_Close, IO_VALUE.LOW);
boxBean.IOMove(IO_Type.TopCylinder_Down, IO_VALUE.LOW);
......@@ -329,27 +323,7 @@ namespace OnlineStore.ACPackingStore
// boxBean.IOMove(IO_Type.LocationCylinder_Down, IO_VALUE.LOW);
// boxBean.IOMove(IO_Type.LocationCylinder_Up, IO_VALUE.LOW);
}
private void btnOpenDoor_Click(object sender, EventArgs e)
{
// boxBean.CylinderMove(null, IO_Type.EntranceDoor_Close, IO_Type.EntranceDoor_Open);
if (!boxBean.DoorBean.StartOpen(null))
{
MessageBox.Show("打开移门失败");
}
}
private void btnCloseDoor_Click(object sender, EventArgs e)
{
if (!boxBean.DoorBean.StartClose(null))
{
MessageBox.Show("关闭移门失败");
}
// boxBean.CylinderMove(null, IO_Type.EntranceDoor_Open, IO_Type.EntranceDoor_Close);
}
private void btnDoorStop_Click(object sender, EventArgs e)
{
boxBean.DoorBean.Stop();
}
private void btnNgLine_Click(object sender, EventArgs e)
{
......@@ -362,8 +336,7 @@ namespace OnlineStore.ACPackingStore
}
private void btnLineBack_Click(object sender, EventArgs e)
{
boxBean.LineBackRun();
{
}
}
}
namespace OnlineStore.ACPackingStore
{
partial class FrmRFIPEdit
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmRFIPEdit));
this.label1 = new System.Windows.Forms.Label();
this.btnBack = new System.Windows.Forms.Button();
this.txtIP = new System.Windows.Forms.TextBox();
this.comboxType = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.lblResult = new System.Windows.Forms.Label();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.label1.Location = new System.Drawing.Point(83, 46);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(107, 20);
this.label1.TabIndex = 0;
this.label1.Text = "请选择读卡器:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// btnBack
//
this.btnBack.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnBack.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.btnBack.Location = new System.Drawing.Point(259, 183);
this.btnBack.Name = "btnBack";
this.btnBack.Size = new System.Drawing.Size(120, 45);
this.btnBack.TabIndex = 274;
this.btnBack.Text = "退出";
this.btnBack.UseVisualStyleBackColor = false;
this.btnBack.Click += new System.EventHandler(this.btnBack_Click);
//
// txtIP
//
this.txtIP.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.txtIP.Location = new System.Drawing.Point(195, 88);
this.txtIP.Name = "txtIP";
this.txtIP.Size = new System.Drawing.Size(233, 26);
this.txtIP.TabIndex = 276;
//
// comboxType
//
this.comboxType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboxType.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.comboxType.FormattingEnabled = true;
this.comboxType.Location = new System.Drawing.Point(195, 43);
this.comboxType.Name = "comboxType";
this.comboxType.Size = new System.Drawing.Size(233, 28);
this.comboxType.TabIndex = 277;
this.comboxType.SelectedIndexChanged += new System.EventHandler(this.comboxType_SelectedIndexChanged);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.label2.Location = new System.Drawing.Point(111, 90);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(78, 20);
this.label2.TabIndex = 278;
this.label2.Text = "读卡器IP:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// button1
//
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.button1.Location = new System.Drawing.Point(115, 183);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(120, 45);
this.button1.TabIndex = 281;
this.button1.Text = "读取测试";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// groupBox2
//
this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.groupBox2.Controls.Add(this.lblResult);
this.groupBox2.Controls.Add(this.comboxType);
this.groupBox2.Controls.Add(this.label1);
this.groupBox2.Controls.Add(this.button1);
this.groupBox2.Controls.Add(this.txtIP);
this.groupBox2.Controls.Add(this.label2);
this.groupBox2.Controls.Add(this.btnBack);
this.groupBox2.Location = new System.Drawing.Point(22, 12);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(505, 249);
this.groupBox2.TabIndex = 291;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "读取RFID";
//
// lblResult
//
this.lblResult.AutoSize = true;
this.lblResult.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblResult.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.lblResult.Location = new System.Drawing.Point(195, 131);
this.lblResult.Name = "lblResult";
this.lblResult.Size = new System.Drawing.Size(79, 20);
this.lblResult.TabIndex = 284;
this.lblResult.Text = "读取到数据";
this.lblResult.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// FrmRFIPEdit
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(560, 285);
this.Controls.Add(this.groupBox2);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "FrmRFIPEdit";
this.Text = "托盘编码";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FrmRFIPEdit_FormClosed);
this.Load += new System.EventHandler(this.FrmPwd_Load);
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnBack;
private System.Windows.Forms.TextBox txtIP;
private System.Windows.Forms.ComboBox comboxType;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label lblResult;
}
}
\ No newline at end of file

using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OnlineStore.ACPackingStore
{
internal partial class FrmRFIPEdit : FrmBase
{
internal FrmRFIPEdit()
{
InitializeComponent();
}
private Dictionary<string, string> rfMap = new Dictionary<string, string>();
private void FrmPwd_Load(object sender, EventArgs e)
{
comboxType.Items.Clear();
foreach (AC_BOX_Bean b in StoreManager.Store.BoxMap.Values)
{
string key = b.Name + "-托盘RF";
string value = b.Config.RFID_IP;
rfMap.Add(key, value);
comboxType.Items.Add(key);
}
comboxType.SelectedIndex = 0;
//comType.SelectedIndex = 0;
List<string> List = new List<string>();
List = new List<string>(rfMap.Values);
if (StoreManager.Store.storeRunStatus <= StoreRunStatus.Wait)
{
RFIDManager.Open(List.ToArray());
}
}
private void btnBack_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string ip = txtIP.Text;
RFIDData data = RFIDManager.ReadRFID(ip);
string resul = "";
if (data != null)
{
resul = data.NumStr();
//numNum.Value = data.Num;
//int selIndex = data.RFType - 64;
//if (selIndex >= 0 && selIndex <= comboxType.Items.Count)
//{
// comboxType.SelectedIndex = selIndex;
//}
}
lblResult.Text = resul;
MessageBox.Show("读取到数据:" + resul);
}
private void comboxType_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboxType.SelectedIndex >= 0)
{
string V = rfMap[comboxType.Text];
if (!txtIP.Text.Equals(V))
{
txtIP.Text = V;
}
}
}
private void FrmRFIPEdit_FormClosed(object sender, FormClosedEventArgs e)
{
if (StoreManager.Store.storeRunStatus <= StoreRunStatus.Wait)
{
// RFIDManager.Close();
}
}
}
}
此文件的差异太大,无法显示。
......@@ -32,7 +32,6 @@
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmStore));
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.label1 = new System.Windows.Forms.Label();
this.lblServerMsg = new System.Windows.Forms.Label();
this.chbDoorCanMove = new System.Windows.Forms.CheckBox();
this.chbUseBuzzer = new System.Windows.Forms.CheckBox();
......@@ -91,7 +90,6 @@
//
// tabPage1
//
this.tabPage1.Controls.Add(this.label1);
this.tabPage1.Controls.Add(this.lblServerMsg);
this.tabPage1.Controls.Add(this.chbDoorCanMove);
this.tabPage1.Controls.Add(this.chbUseBuzzer);
......@@ -107,16 +105,6 @@
this.tabPage1.Text = " 日志信息 ";
this.tabPage1.UseVisualStyleBackColor = true;
//
// label1
//
this.label1.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(10, 539);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(219, 29);
this.label1.TabIndex = 202;
this.label1.Text = "label1";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// lblServerMsg
//
this.lblServerMsg.AutoSize = true;
......@@ -130,7 +118,7 @@
//
this.chbDoorCanMove.AutoSize = true;
this.chbDoorCanMove.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.chbDoorCanMove.Location = new System.Drawing.Point(36, 425);
this.chbDoorCanMove.Location = new System.Drawing.Point(40, 107);
this.chbDoorCanMove.Name = "chbDoorCanMove";
this.chbDoorCanMove.Size = new System.Drawing.Size(126, 24);
this.chbDoorCanMove.TabIndex = 197;
......@@ -142,7 +130,7 @@
//
this.chbUseBuzzer.AutoSize = true;
this.chbUseBuzzer.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.chbUseBuzzer.Location = new System.Drawing.Point(36, 343);
this.chbUseBuzzer.Location = new System.Drawing.Point(40, 60);
this.chbUseBuzzer.Name = "chbUseBuzzer";
this.chbUseBuzzer.Size = new System.Drawing.Size(98, 24);
this.chbUseBuzzer.TabIndex = 195;
......@@ -154,7 +142,7 @@
//
this.chkDebug.AutoSize = true;
this.chkDebug.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.chkDebug.Location = new System.Drawing.Point(36, 502);
this.chkDebug.Location = new System.Drawing.Point(40, 157);
this.chkDebug.Name = "chkDebug";
this.chkDebug.Size = new System.Drawing.Size(132, 24);
this.chkDebug.TabIndex = 194;
......@@ -166,7 +154,7 @@
//
this.chbAutoRun.AutoSize = true;
this.chbAutoRun.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.chbAutoRun.Location = new System.Drawing.Point(36, 302);
this.chbAutoRun.Location = new System.Drawing.Point(40, 19);
this.chbAutoRun.Name = "chbAutoRun";
this.chbAutoRun.Size = new System.Drawing.Size(140, 24);
this.chbAutoRun.TabIndex = 94;
......@@ -178,7 +166,7 @@
//
this.btnCopyLog.BackColor = System.Drawing.Color.White;
this.btnCopyLog.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnCopyLog.Location = new System.Drawing.Point(14, 584);
this.btnCopyLog.Location = new System.Drawing.Point(18, 301);
this.btnCopyLog.Name = "btnCopyLog";
this.btnCopyLog.Size = new System.Drawing.Size(105, 35);
this.btnCopyLog.TabIndex = 191;
......@@ -190,7 +178,7 @@
//
this.btnClearLog.BackColor = System.Drawing.Color.White;
this.btnClearLog.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnClearLog.Location = new System.Drawing.Point(125, 584);
this.btnClearLog.Location = new System.Drawing.Point(129, 301);
this.btnClearLog.Name = "btnClearLog";
this.btnClearLog.Size = new System.Drawing.Size(105, 35);
this.btnClearLog.TabIndex = 190;
......@@ -204,9 +192,9 @@
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.logBox.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.logBox.Location = new System.Drawing.Point(241, 88);
this.logBox.Location = new System.Drawing.Point(241, 3);
this.logBox.Name = "logBox";
this.logBox.Size = new System.Drawing.Size(1085, 531);
this.logBox.Size = new System.Drawing.Size(1085, 616);
this.logBox.TabIndex = 106;
this.logBox.Text = "";
this.logBox.VisibleChanged += new System.EventHandler(this.logBox_VisibleChanged);
......@@ -257,54 +245,54 @@
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(177, 6);
this.toolStripSeparator1.Size = new System.Drawing.Size(114, 6);
//
// 启动AToolStripMenuItem
//
this.启动AToolStripMenuItem.Name = "启动AToolStripMenuItem";
this.启动AToolStripMenuItem.Size = new System.Drawing.Size(180, 26);
this.启动AToolStripMenuItem.Size = new System.Drawing.Size(117, 26);
this.启动AToolStripMenuItem.Text = "启动 ";
this.启动AToolStripMenuItem.Click += new System.EventHandler(this.启动所有料仓AToolStripMenuItem_Click);
//
// toolStripSeparator4
//
this.toolStripSeparator4.Name = "toolStripSeparator4";
this.toolStripSeparator4.Size = new System.Drawing.Size(177, 6);
this.toolStripSeparator4.Size = new System.Drawing.Size(114, 6);
//
// 复位RToolStripMenuItem
//
this.复位RToolStripMenuItem.Name = "复位RToolStripMenuItem";
this.复位RToolStripMenuItem.Size = new System.Drawing.Size(180, 26);
this.复位RToolStripMenuItem.Size = new System.Drawing.Size(117, 26);
this.复位RToolStripMenuItem.Text = "复位";
this.复位RToolStripMenuItem.Click += new System.EventHandler(this.复位RToolStripMenuItem_Click);
//
// toolStripSeparator3
//
this.toolStripSeparator3.Name = "toolStripSeparator3";
this.toolStripSeparator3.Size = new System.Drawing.Size(177, 6);
this.toolStripSeparator3.Size = new System.Drawing.Size(114, 6);
//
// 停止TToolStripMenuItem
//
this.停止TToolStripMenuItem.Name = "停止TToolStripMenuItem";
this.停止TToolStripMenuItem.Size = new System.Drawing.Size(180, 26);
this.停止TToolStripMenuItem.Size = new System.Drawing.Size(117, 26);
this.停止TToolStripMenuItem.Text = "停止";
this.停止TToolStripMenuItem.Click += new System.EventHandler(this.停止所有料仓TToolStripMenuItem_Click);
//
// toolStripSeparator5
//
this.toolStripSeparator5.Name = "toolStripSeparator5";
this.toolStripSeparator5.Size = new System.Drawing.Size(177, 6);
this.toolStripSeparator5.Size = new System.Drawing.Size(114, 6);
//
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(177, 6);
this.toolStripSeparator2.Size = new System.Drawing.Size(114, 6);
this.toolStripSeparator2.Visible = false;
//
// 退出ToolStripMenuItem
//
this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem";
this.退出ToolStripMenuItem.Size = new System.Drawing.Size(180, 26);
this.退出ToolStripMenuItem.Size = new System.Drawing.Size(117, 26);
this.退出ToolStripMenuItem.Text = "退出";
this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click_1);
//
......@@ -322,26 +310,26 @@
// toolStripMenuItem2
//
this.toolStripMenuItem2.Name = "toolStripMenuItem2";
this.toolStripMenuItem2.Size = new System.Drawing.Size(180, 26);
this.toolStripMenuItem2.Size = new System.Drawing.Size(160, 26);
this.toolStripMenuItem2.Text = "启用调试";
this.toolStripMenuItem2.Click += new System.EventHandler(this.toolStripMenuItem2_Click);
//
// toolStripSeparator6
//
this.toolStripSeparator6.Name = "toolStripSeparator6";
this.toolStripSeparator6.Size = new System.Drawing.Size(177, 6);
this.toolStripSeparator6.Size = new System.Drawing.Size(157, 6);
//
// 二维码学习ToolStripMenuItem
//
this.二维码学习ToolStripMenuItem.Name = "二维码学习ToolStripMenuItem";
this.二维码学习ToolStripMenuItem.Size = new System.Drawing.Size(180, 26);
this.二维码学习ToolStripMenuItem.Size = new System.Drawing.Size(160, 26);
this.二维码学习ToolStripMenuItem.Text = "二维码学习";
this.二维码学习ToolStripMenuItem.Click += new System.EventHandler(this.二维码学习ToolStripMenuItem_Click);
//
// toolStripSeparator7
//
this.toolStripSeparator7.Name = "toolStripSeparator7";
this.toolStripSeparator7.Size = new System.Drawing.Size(177, 6);
this.toolStripSeparator7.Size = new System.Drawing.Size(157, 6);
//
// 帮助ToolStripMenuItem
//
......@@ -354,7 +342,7 @@
// 版本号ToolStripMenuItem
//
this.版本号ToolStripMenuItem.Name = "版本号ToolStripMenuItem";
this.版本号ToolStripMenuItem.Size = new System.Drawing.Size(180, 26);
this.版本号ToolStripMenuItem.Size = new System.Drawing.Size(144, 26);
this.版本号ToolStripMenuItem.Text = "关于软件";
this.版本号ToolStripMenuItem.Click += new System.EventHandler(this.版本号ToolStripMenuItem_Click);
//
......@@ -414,7 +402,7 @@
this.MainMenuStrip = this.menuStrip1;
this.Name = "FrmStore";
this.Opacity = 0D;
this.Text = "包装料流水线";
this.Text = "DUO料仓客户端";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmMain_FormClosing);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FrmLineStore_FormClosed);
......@@ -467,7 +455,6 @@
private System.Windows.Forms.ToolStripSeparator toolStripSeparator8;
private System.Windows.Forms.CheckBox chbDoorCanMove;
private System.Windows.Forms.Label lblServerMsg;
private System.Windows.Forms.Label label1;
}
}
......@@ -272,7 +272,7 @@ namespace OnlineStore.ACPackingStore
停止所有料仓TToolStripMenuItem_Click(null, null);
}
RFIDManager.Close();
//RFIDManager.Close();
}
private void btnClearLog_Click(object sender, EventArgs e)
{
......
......@@ -85,7 +85,6 @@
<Compile Include="device\halcon\CodeManager.cs" />
<Compile Include="device\IO\AIOBOX\AIOBOXManager.cs" />
<Compile Include="device\IO\IOManager.cs" />
<Compile Include="device\RFIDManager.cs" />
<Compile Include="store\AutoInoutInfo.cs" />
<Compile Include="store\LineMoveP.cs" />
<Compile Include="store\LineAlarm.cs">
......
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Asa.AIOBOX;
using System.Threading;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class AIOBOXManager : IOManager
{
//public static uint DefaultDICount = 16;
//public static uint DefaultDOCount = 16;
public readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public Dictionary<string, AIOBOX_32> AIOMap = new Dictionary<string, AIOBOX_32>();
public string DefaultIP = "";
public Dictionary<string, List<Status>> DIValueMap = new Dictionary<string, List<Status>>();
public Dictionary<string, List<Status>> DOValueMap = new Dictionary<string, List<Status>>();
private object DIMapLock = "";
private object DOMapLock = "";
public System.Timers.Timer timer = null;
private object DILock = "";
private object DOLock = "";
public void ConnectionIP(string ioIp )
{
AIOBOX_32 aioBox = null;
if (AIOMap.ContainsKey(ioIp))
{
aioBox = AIOMap[ioIp];
if (null != aioBox)
{
aioBox.Close();
aioBox = null;
}
AIOMap.Remove(ioIp);
}
if (DIValueMap.ContainsKey(ioIp))
{
DIValueMap.Remove(ioIp);
}
if (DOValueMap.ContainsKey(ioIp))
{
DOValueMap.Remove(ioIp);
}
try
{
// Create new modbus master and add event functions
aioBox = new AIOBOX_32();
aioBox.IP = ioIp;
DefaultIP = ioIp;
aioBox.AutoReadDI = true;
aioBox.AutoReadDO = true;
aioBox.DI_Changed_Event += AioBox_DI_Changed_Event; ;
aioBox.DO_Changed_Event += AioBox_DO_Changed_Event;
LogUtil.info("开始连接IO模块【" + ioIp + "】,尝试重连三次");
for (int i = 1; i <= 3; i++)
{
bool result = aioBox.Connect();
if (result)
{
AIOMap.Add(ioIp, aioBox);
LogUtil.info("第【"+i+"】次连接IO模块【" + ioIp + "】成功:" + aioBox.ErrInfo);
Thread.Sleep(10);
//读取所有的DO
ReadAllDI(ioIp, 0);
break;
}
else
{
LogUtil.error("第【" + i + "】次连接IO模块【" + ioIp + "】失败:" + aioBox.ErrInfo + "");
}
Thread.Sleep(10);
}
}
catch (Exception error)
{
LogUtil.error(LOGGER, "连接IO模块[" + ioIp + "]出错:" + error.ToString());
}
}
private void AioBox_DI_Changed_Event(AIOBOX_32 box, Status[] sta)
{
try
{
UpdateAllDI(box.IP, sta);
}
catch (Exception ex)
{
LogUtil.error("AioBox_DI_Changed_Event出错:" + ex.ToString());
}
}
private void AioBox_DO_Changed_Event(AIOBOX_32 box, Status[] sta)
{
try
{
UpdateAllDO(box.IP, sta);
}
catch (Exception ex)
{
LogUtil.error("AioBox_DO_Changed_Event出错:" + ex.ToString());
}
}
private void UpdateAllDI(string ip, Status[] sta)
{
if (sta != null && sta.Length >= StoreManager.Config.GetDILength(ip))
{
bool needUpdate = false;
List<Status> newList = new List<Status>();
newList.AddRange(sta);
List<Status> oldList = null;
DIValueMap.TryGetValue(ip, out oldList);
if (oldList == null || oldList.Count.Equals(newList.Count).Equals(false))
{
needUpdate = true;
}
else
{
for (int i = 0; i < newList.Count; i++)
{
if (!(oldList[i].Equals(newList[i])))
{
needUpdate = true;
break;
}
}
}
if (needUpdate)
{
lock (DILock)
{
if (DIValueMap.ContainsKey(ip))
{
DIValueMap.Remove(ip);
}
DIValueMap.Add(ip, newList);
}
}
}
}
private void UpdateAllDO(string ip, Status[] sta)
{
if (sta != null && sta.Length >= StoreManager.Config.GetDOLength(ip))
{
bool needUpdate = false;
List<Status> newList = new List<Status>();
newList.AddRange(sta);
List<Status> oldList = null;
DOValueMap.TryGetValue(ip, out oldList);
if (oldList == null || oldList.Count.Equals(newList.Count).Equals(false))
{
needUpdate = true;
}
else
{
for (int i = 0; i < newList.Count; i++)
{
if (!(oldList[i].Equals(newList[i])))
{
needUpdate = true;
break;
}
}
}
if (needUpdate)
{
lock (DOLock)
{
if (DOValueMap.ContainsKey(ip))
{
DOValueMap.Remove(ip);
}
DOValueMap.Add(ip, newList);
}
}
}
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
List<string> list = new List<string>(AIOMap.Keys);
foreach (string io in list)
{
//判断是否连接,如果没有连接自动重连
AIOBOX_32 clinet = AIOMap[io];
if (!clinet.IsConn)
{
LogUtil.error(LOGGER, io + "当前没有连上:" + clinet.ErrInfo);
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦:" + ex.ToString());
}
Thread.Sleep(1);
}
public override void ConnectionIOList(List<string> DIONameList)
{
foreach (string ip in DIONameList)
{
ConnectionIP(ip );
}
}
//关闭所有的DO
public override void CloseAllDO()
{
foreach (AIOBOX_32 aio in AIOMap.Values)
{
Status[] statuses = new Status[16];
for(int i = 0; i < 16; i++)
{
statuses[i] = Status.Off;
}
aio.WriteDO(Addr.DI_1, statuses);
}
}
public override void CloseAllConnection()
{
foreach (AIOBOX_32 aio in AIOMap.Values)
{
aio.Close();
}
AIOMap.Clear();
}
public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff)
{
try
{
AIOBOX_32 aioBox = getAIO(ioIp);
if (aioBox != null)
{
Addr add = GetAddr(StartAddress);
for (int i = 1; i <= 3; i++)
{
bool result = aioBox.WriteDO(GetAddr(StartAddress), GetStatus(onOff));
if (!result)
{
LogUtil.error("AIO WriteSingleDO [" + StartAddress + "] 第" + i + "次失败:" + aioBox.ErrInfo);
}
else
{
break;
}
}
}
else
{
LogUtil.error(LOGGER, "ReadSingleDO出错没有连接IO模块:" + ioIp);
}
}
catch (Exception ex)
{
LOGGER.Error("出错啦:" + ex.ToString());
}
}
public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff, int mSeconds)
{
try
{
AIOBOX_32 aioBox = getAIO(ioIp);
Status currStatus = GetStatus(onOff);
if (aioBox != null)
{
Addr add = GetAddr(StartAddress);
aioBox.WriteDO(GetAddr(StartAddress), currStatus);
//写入之后,等待指定间隔后回写
System.Timers.Timer mytimer = new System.Timers.Timer(mSeconds);
mytimer.Elapsed += (o1, e1) =>
{
try
{
aioBox.WriteDO(GetAddr(StartAddress), aioBox.ReverseStatus(currStatus));
LogUtil.debug(LOGGER, "**********定时回写入 IO【" + ioIp + "," + StartAddress + ",值" + aioBox.ReverseStatus(currStatus) + "】:");
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "**********定时回写入 出错:" + ex.StackTrace);
}
};
mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次;
mytimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件mytask;
}
else
{
LogUtil.error(LOGGER, "WriteSingleDO出错没有连接IO模块:" + ioIp);
}
}
catch (Exception ex)
{
LogUtil.error("WriteSingleDO出错:" + ioIp);
}
}
public override void ReadAllDI(string ioIp, byte slaveId)
{
try
{
AIOBOX_32 aioBox = getAIO(ioIp);
if (aioBox != null)
{
Status[] allDi;
if (aioBox.ReadDI(Addr.DI_1, StoreManager.Config.GetDILength(ioIp), out allDi))
{
UpdateAllDI(ioIp, allDi);
}
else
{
LogUtil.error("读取所有DI出错:" + aioBox.ErrInfo);
}
}
}
catch (Exception ex)
{
LogUtil.error("ReadAllDI出错:" + ioIp);
}
}
public override void ReadAllDO(string ioIp, byte slaveId)
{
try
{
AIOBOX_32 aioBox = getAIO(ioIp);
if (aioBox != null)
{
Status[] allDO;
if (aioBox.ReadDO(Addr.DO_1, StoreManager.Config.GetDOLength(ioIp), out allDO))
{
UpdateAllDO(ioIp, allDO);
}
else
{
LogUtil.error("读取所有DO出错:" + aioBox.ErrInfo);
}
}
}
catch (Exception ex)
{
LogUtil.error("ReadAllDO出错:" + ioIp);
}
}
public override IO_VALUE GetDOValue(string ioIP, byte slaveId, ushort StartAddress)
{
IO_VALUE value = IO_VALUE.LOW;
try
{
AIOBOX_32 aioBox = getAIO(ioIP);
if (aioBox != null)
{
Status status = Status.Off;
Addr addr = GetAddr(StartAddress);
int index =(int)StartAddress - (int)StoreManager.Config.GetDILength(ioIP);
if (DOValueMap.ContainsKey(ioIP) && DOValueMap[ioIP].Count>index)
{
status = DOValueMap[ioIP][index];
}
else
{ aioBox.ReadDO(addr, out status); }
if (status.Equals(Status.On))
{
value = IO_VALUE.HIGH;
}
}
}
catch (Exception ex)
{
LogUtil.error("ReadDI 出错:" + ex.ToString());
}
return value;
}
public override IO_VALUE GetDIValue(string ioIP, byte slaveId, ushort StartAddress)
{
IO_VALUE value = IO_VALUE.LOW;
try
{
AIOBOX_32 aioBox = getAIO(ioIP);
if (aioBox != null)
{
Status status = Status.Off;
Addr addr = GetAddr(StartAddress);
int index = StartAddress;
if (DIValueMap.ContainsKey(ioIP) && DIValueMap[ioIP].Count> index)
{
status = DIValueMap[ioIP][index];
}
else
{
aioBox.ReadDI(addr, out status);
}
if (status.Equals(Status.On))
{
value = IO_VALUE.HIGH;
}
}
}
catch (Exception ex)
{
LogUtil.error("ReadDI 出错:" + ex.ToString());
}
return value;
}
public override IO_VALUE GetIOValue(ConfigIO configIO)
{
IO_VALUE value = IO_VALUE.LOW;
try
{
if (configIO.ProType.Equals(ConfigItemType.DI))
{
return GetDIValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
}
else if (configIO.ProType.Equals(ConfigItemType.DO))
{
return GetDOValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "获取数据出错:" + ex.ToString());
}
return value;
}
private Addr GetAddr(ushort StartAddress)
{
return (Addr)(StartAddress );
}
private Status GetStatus(IO_VALUE onOff)
{
if (onOff.Equals(IO_VALUE.HIGH))
{
return Status.On;
}
else
{
return Status.Off;
}
}
private AIOBOX_32 getAIO(string ioIp)
{
AIOBOX_32 aioBox = null;
if (AIOMap.ContainsKey(ioIp))
{
aioBox = AIOMap[ioIp];
}
return aioBox;
}
}
}
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System.Threading.Tasks;
using Asa.IOModule;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary
{
public class AIOBOXManager : IOManager
{
public Dictionary<string, AIOBOX> AIOMap = new Dictionary<string, AIOBOX>();
public Dictionary<string, List<Box_Sta>> DIValueMap = new Dictionary<string, List<Box_Sta>>();
public Dictionary<string, List<Box_Sta>> DOValueMap = new Dictionary<string, List<Box_Sta>>();
private object DIMapLock = "";
private object DOMapLock = "";
private object DILock = "";
private object DOLock = "";
public override void ConnectionIOList(List<string> DIONameList)
{
foreach (string ip in DIONameList)
{
ConnectionIP(ip);
}
}
private bool isProcess = false;
private DateTime lastTime = DateTime.Now;
public void ConnectionIP(string ioIp)
{
AIOBOX aioBox = null;
if (AIOMap.ContainsKey(ioIp))
{
aioBox = AIOMap[ioIp];
try
{
if (null != aioBox)
{
aioBox.Close();
aioBox = null;
}
}
catch (Exception ex)
{
LogUtil.error("关闭Io模块【" + ioIp + "】出错:" + ex.ToString());
}
AIOMap.Remove(ioIp);
}
if (DIValueMap.ContainsKey(ioIp))
{
DIValueMap.Remove(ioIp);
}
if (DOValueMap.ContainsKey(ioIp))
{
DOValueMap.Remove(ioIp);
}
int DILength = StoreManager.Config.GetDILength(ioIp);
int DOLength = StoreManager.Config.GetDOLength(ioIp);
string logName = "IO模块[" + ioIp + "] DI[" + DILength + "] DO[" + DOLength + "] ";
try
{
aioBox = new AIOBOX("AIOBOX");
aioBox.SetType(Asa.IOModule.Box_Type.DI, DILength, Asa.IOModule.Box_Type.DO, DOLength);
aioBox.IP = ioIp;
aioBox.Upload = false;
aioBox.DI_Changed_Event += AioBox_DI_Changed_Event; ;
aioBox.DO_Changed_Event += AioBox_DO_Changed_Event;
LogUtil.info("开始连接:" + logName + ":");
aioBox.Connect();
AIOMap.Add(ioIp, aioBox);
Thread.Sleep(5);
//读取所有的DO
ReadAllDI(ioIp, 0);
Thread.Sleep(5);
GC.Collect();
}
catch (Exception error)
{
LogUtil.error("连接IO模块 " + logName + " 出错:" + error.ToString());
}
}
private DateTime lastLogTime = DateTime.Now;
private void AioBox_DI_Changed_Event(AIOBOX box, Box_Sta[] sta)
{
try
{
UpdateAllDI(box.IP, sta);
}
catch (Exception ex)
{
LogUtil.error("AioBox_DI_Changed_Event出错:" + ex.ToString());
}
}
private void AioBox_DO_Changed_Event(AIOBOX box, Box_Sta[] sta)
{
try
{
UpdateAllDO(box.IP, sta);
}
catch (Exception ex)
{
LogUtil.error("AioBox_DO_Changed_Event出错:" + ex.ToString());
}
}
private void UpdateAllDI(string ip, Box_Sta[] sta)
{
if (sta != null && sta.Length >= StoreManager.Config.GetDILength(ip))
{
string updateDi = "[" + ip + "]:";
bool needUpdate = false;
List<Box_Sta> newList = new List<Box_Sta>();
newList.AddRange(sta);
List<Box_Sta> oldList = null;
DIValueMap.TryGetValue(ip, out oldList);
if (oldList == null || oldList.Count.Equals(newList.Count).Equals(false))
{
needUpdate = true;
}
else
{
for (int i = 0; i < newList.Count; i++)
{
if (!(oldList[i].Equals(newList[i])))
{
needUpdate = true;
break;
}
}
}
if (needUpdate)
{
lock (DILock)
{
if (DIValueMap.ContainsKey(ip))
{
DIValueMap.Remove(ip);
}
DIValueMap.Add(ip, newList);
}
}
}
}
private void UpdateAllDO(string ip, Box_Sta[] sta)
{
if (sta != null && sta.Length >= StoreManager.Config.GetDOLength(ip))
{
bool needUpdate = false;
List<Box_Sta> newList = new List<Box_Sta>();
newList.AddRange(sta);
List<Box_Sta> oldList = null;
DOValueMap.TryGetValue(ip, out oldList);
if (oldList == null || oldList.Count.Equals(newList.Count).Equals(false))
{
needUpdate = true;
}
else
{
for (int i = 0; i < newList.Count; i++)
{
if (!(oldList[i].Equals(newList[i])))
{
needUpdate = true;
break;
}
}
}
if (needUpdate)
{
lock (DOLock)
{
if (DOValueMap.ContainsKey(ip))
{
DOValueMap.Remove(ip);
}
DOValueMap.Add(ip, newList);
}
}
}
}
//关闭所有的DO
public override void CloseAllDO()
{
foreach (AIOBOX aio in AIOMap.Values)
{
ushort length = StoreManager.Config.GetDOLength(aio.IP);
for (ushort i = 0; i < length; i++)
{
aio.WriteDO(i, Box_Sta.Off);
}
}
}
public override void CloseAllConnection()
{
foreach (AIOBOX aio in AIOMap.Values)
{
try
{
aio.Close();
}
catch (Exception ex)
{
LogUtil.error("aio.Close出错:" + ex.ToString());
}
}
AIOMap.Clear();
}
public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff)
{
try
{
AIOBOX aioBox = getAIO(ioIp);
if (aioBox != null)
{
bool result = aioBox.WriteDO(StartAddress, GetBox_Sta(onOff));
if (!result)
{
LogUtil.error("AIO WriteSingleDO [" + ioIp + "] [" + StartAddress + "] 失败:");
}
}
else
{
LogUtil.error("WriteSingleDO出错 没有连接IO模块:" + ioIp);
}
}
catch (Exception ex)
{
LogUtil.error("AIO WriteSingleDO [" + ioIp + "] [" + StartAddress + "] 出错啦:" + ex.ToString());
}
}
public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff, int mSeconds)
{
try
{
AIOBOX aioBox = getAIO(ioIp);
if (aioBox != null)
{
Box_Sta currBox_Sta = GetBox_Sta(onOff);
aioBox.WriteDO(StartAddress, currBox_Sta);
//写入之后,等待指定间隔后回写
System.Timers.Timer mytimer = new System.Timers.Timer(mSeconds);
mytimer.Elapsed += (o1, e1) =>
{
try
{
aioBox.WriteDO(StartAddress, aioBox.ReverseStatus(currBox_Sta));
LogUtil.debug("**********定时回写入 IO [" + ioIp + "] [" + StartAddress + "]值" + aioBox.ReverseStatus(currBox_Sta) + "】:");
}
catch (Exception ex)
{
LogUtil.error("**********定时回写入 出错:" + ex.ToString());
}
};
mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次;
mytimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件mytask;
}
else
{
LogUtil.error("AIO WriteSingleDO [" + ioIp + "] [" + StartAddress + "] 出错 没有连接IO模块:" + ioIp);
}
}
catch (Exception ex)
{
LogUtil.error("AIO WriteSingleDO [" + ioIp + "] [" + StartAddress + "] 出错:" + ioIp);
}
}
public override void ReadAllDI(string ioIp, byte slaveId)
{
try
{
AIOBOX aioBox = getAIO(ioIp);
if (aioBox != null)
{
Box_Sta[] allDi = aioBox.ReadDI(0, StoreManager.Config.GetDILength(ioIp));
UpdateAllDI(ioIp, allDi);
}
}
catch (Exception ex)
{
LogUtil.error("ReadAllDI [" + ioIp + "]出错:" + ioIp);
}
}
public override void ReadAllDO(string ioIp, byte slaveId)
{
try
{
AIOBOX aioBox = getAIO(ioIp);
if (aioBox != null)
{
// int startIndex = StoreManager.Config.GetDILength(ioIp);
Box_Sta[] allDO = aioBox.ReadDO(0, StoreManager.Config.GetDOLength(ioIp));
UpdateAllDO(ioIp, allDO);
}
}
catch (Exception ex)
{
LogUtil.error("ReadAllDO [" + ioIp + "]出错:" + ioIp);
}
}
public override IO_VALUE GetDOValue(string ioIP, byte slaveId, ushort StartAddress)
{
IO_VALUE value = IO_VALUE.None;
try
{
AIOBOX aioBox = getAIO(ioIP);
if (aioBox != null)
{
Box_Sta sta = Box_Sta.Off;
if (DOValueMap.ContainsKey(ioIP) && DOValueMap[ioIP].Count > StartAddress)
{
sta = DOValueMap[ioIP][StartAddress];
}
else
{ sta = aioBox.ReadDO(StartAddress); }
if (sta.Equals(Box_Sta.On))
{
value = IO_VALUE.HIGH;
}
else
{
value = IO_VALUE.LOW;
}
}
}
catch (Exception ex)
{
LogUtil.error("GetDOValue [" + ioIP + "] [" + StartAddress + "] 出错:" + ex.ToString());
}
return value;
}
public override IO_VALUE GetDIValue(string ioIP, byte slaveId, ushort StartAddress)
{
IO_VALUE value = IO_VALUE.None;
for (int i = 1; i <= 3; i++)
{
try
{
AIOBOX aioBox = getAIO(ioIP);
if (aioBox != null)
{
Box_Sta sta = Box_Sta.Off;
// Box_Addr addr = GetAddr(StartAddress);
int index = StartAddress;
if (DIValueMap.ContainsKey(ioIP) && DIValueMap[ioIP].Count > index)
{
sta = DIValueMap[ioIP][index];
}
else
{
sta = aioBox.ReadDI(StartAddress);
}
if (sta.Equals(Box_Sta.On))
{
value = IO_VALUE.HIGH;
}
else
{
value = IO_VALUE.LOW;
}
}
break;
}
catch (Exception ex)
{
LogUtil.error(" 第【" + i + "】次 GetDIValue [" + ioIP + "] [" + StartAddress + "] 出错:" + ex.ToString());
}
}
return value;
}
public override IO_VALUE GetIOValue(ConfigIO configIO)
{
IO_VALUE value = IO_VALUE.None;
try
{
if (configIO.ProType.Equals(ConfigItemType.DI))
{
return GetDIValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
}
else if (configIO.ProType.Equals(ConfigItemType.DO))
{
return GetDOValue(configIO.DeviceName, configIO.SlaveID, configIO.GetIOAddr());
}
}
catch (Exception ex)
{
LogUtil.error(" GetIOValue [" + configIO.DeviceName + "] [" + configIO.GetIOAddr() + "] 获取数据出错:" + ex.ToString());
}
return value;
}
private Box_Sta GetBox_Sta(IO_VALUE onOff)
{
if (onOff.Equals(IO_VALUE.HIGH))
{
return Box_Sta.On;
}
else
{
return Box_Sta.Off;
}
}
private AIOBOX getAIO(string ioIp)
{
AIOBOX aioBox = null;
if (AIOMap.ContainsKey(ioIp))
{
aioBox = AIOMap[ioIp];
}
return aioBox;
}
}
}
/*
* @Description: 用于AIOBOX-32系列一体化IO模块
* @CreateDate: 2019-02-28
* @UpdateDate: 2019-05-13
* @Author: Asa
* @Version: 1.8
*
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Generic;
using OnlineStore.Common;
namespace Asa.IOModule
{
/// <summary>
/// AIOBOX操作类
/// </summary>
public class AIOBOX
{
private ushort _flag; //ModBusTCP标识
private Socket _client; //客户端
private Box_Type _type; //类型
private byte[] _addr; //地址
private Box_Sta[] _sta; //状态
private int _unrevd; //没有收到数据的时间
private int _unrevdRemote; //本地还是远程没有收到数据
private bool _readDI; //自动读取DI
private bool _readDO; //自动读取DO
private int _readDISleep; //自动读取DI间隔
private int _readDOSleep; //自动读取DO间隔
private List<string> _log; //日志
private List<string> _logRxTx; //日志
//private List<byte[]> _send; //发送的命令
private System.Collections.Concurrent.ConcurrentQueue<byte[]> _send;
private System.Collections.Concurrent.ConcurrentQueue<byte[]> _receive; //接收的数据
private Thread tSend; //发送命令处理
private Thread tReceive; //接收信息处理
private Thread tListen; //监听网络
private Thread tTrigger; //触发DI、DO改变事件
private Thread tReadDI; //自动读取DI线程
private Thread tReadDO; //自动读取DO线程
private Thread tLogOut; //日志输出
private Thread tReconn; //断开重连
private const int SEND_SLEEP = 15; //发送命令间隔
private const int NET_SLEEP = 15; //接收网络间隔
private const int TRIG_SLEEP = 20; //触发事件间隔
/// <summary>
/// 自动读取DI委托
/// </summary>
/// <param name="box">AIOBOX</param>
/// <param name="sta">所有DI状态</param>
public delegate void DI_Changed(AIOBOX box, Box_Sta[] sta);
/// <summary>
/// 自动读取DI事件触发
/// </summary>
public event DI_Changed DI_Changed_Event;
/// <summary>
/// 自动读取DO委托
/// </summary>
/// <param name="box">AIOBOX</param>
/// <param name="sta">所有DO状态</param>
public delegate void DO_Changed(AIOBOX box, Box_Sta[] sta);
/// <summary>
/// 自动读取DO事件触发
/// </summary>
public event DO_Changed DO_Changed_Event;
/// <summary>
/// 日志输出
/// </summary>
/// <param name="box"></param>
/// <param name="s"></param>
public delegate void Log_Out(AIOBOX box, string[] s);
/// <summary>
/// 日志输出事件
/// </summary>
public event Log_Out Log_Out_Event;
/// <summary>
/// 日志输出
/// </summary>
/// <param name="box"></param>
/// <param name="s"></param>
public delegate void Log_RxTx(AIOBOX box, string[] s);
/// <summary>
/// 日志输出事件,发送命令的标识减去接收命令的标识
/// </summary>
public event Log_RxTx Log_RxTx_Event;
/// <summary>
/// 断开重连
/// </summary>
/// <param name="box"></param>
/// <param name="times">断开次数</param>
/// <param name="conn"></param>
public delegate void Reconnect(AIOBOX box, int times, ref bool conn, Dictionary<string, string> dict);
/// <summary>
/// 断开重连事件
/// </summary>
public event Reconnect Reconnect_Event;
/// <summary>
/// AIOBOX
/// </summary>
public AIOBOX()
{
_unrevd = 0;
_unrevdRemote = 0;
_readDI = false;
_readDO = false;
_readDISleep = 100;
_readDOSleep = 100;
_addr = new byte[32];
_sta = new Box_Sta[32];
_log = new List<string>();
_logRxTx = new List<string>();
_send = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
_receive = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
Type = Box_Type.DIO_32;
tReconn = new Thread(new ThreadStart(Reconn));
tReconn.Start();
}
/// <summary>
/// IP地址
/// </summary>
public string IP { set; get; } = "192.168.1.100";
/// <summary>
/// ModBus端口
/// </summary>
public int Port { set; get; } = 502;
/// <summary>
/// 是否连接
/// </summary>
public bool IsConn { get; private set; } = false;
/// <summary>
/// 错误信息
/// </summary>
public string ErrInfo { get; private set; } = "";
/// <summary>
/// 日志输出
/// </summary>
public bool LogOut { set; get; } = false;
/// <summary>
/// 模块的类型
/// </summary>
public Box_Type Type
{
set
{
_type = value;
if (value == Box_Type.DIO_16)
{
_addr[(int)Box_Addr.DI_1] = 0;
_addr[(int)Box_Addr.DI_2] = 1;
_addr[(int)Box_Addr.DI_3] = 2;
_addr[(int)Box_Addr.DI_4] = 3;
_addr[(int)Box_Addr.DI_5] = 4;
_addr[(int)Box_Addr.DI_6] = 5;
_addr[(int)Box_Addr.DI_7] = 6;
_addr[(int)Box_Addr.DI_8] = 7;
_addr[(int)Box_Addr.DI_9] = 255;
_addr[(int)Box_Addr.DI_10] = 255;
_addr[(int)Box_Addr.DI_11] = 255;
_addr[(int)Box_Addr.DI_12] = 255;
_addr[(int)Box_Addr.DI_13] = 255;
_addr[(int)Box_Addr.DI_14] = 255;
_addr[(int)Box_Addr.DI_15] = 255;
_addr[(int)Box_Addr.DI_16] = 255;
_addr[(int)Box_Addr.DO_1] = 8;
_addr[(int)Box_Addr.DO_2] = 9;
_addr[(int)Box_Addr.DO_3] = 10;
_addr[(int)Box_Addr.DO_4] = 11;
_addr[(int)Box_Addr.DO_5] = 12;
_addr[(int)Box_Addr.DO_6] = 13;
_addr[(int)Box_Addr.DO_7] = 14;
_addr[(int)Box_Addr.DO_8] = 15;
_addr[(int)Box_Addr.DO_9] = 255;
_addr[(int)Box_Addr.DO_10] = 255;
_addr[(int)Box_Addr.DO_11] = 255;
_addr[(int)Box_Addr.DO_12] = 255;
_addr[(int)Box_Addr.DO_13] = 255;
_addr[(int)Box_Addr.DO_14] = 255;
_addr[(int)Box_Addr.DO_15] = 255;
_addr[(int)Box_Addr.DO_16] = 255;
}
else if (value == Box_Type.DIO_32)
{
_addr[(int)Box_Addr.DI_1] = 0;
_addr[(int)Box_Addr.DI_2] = 1;
_addr[(int)Box_Addr.DI_3] = 2;
_addr[(int)Box_Addr.DI_4] = 3;
_addr[(int)Box_Addr.DI_5] = 4;
_addr[(int)Box_Addr.DI_6] = 5;
_addr[(int)Box_Addr.DI_7] = 6;
_addr[(int)Box_Addr.DI_8] = 7;
_addr[(int)Box_Addr.DI_9] = 8;
_addr[(int)Box_Addr.DI_10] = 9;
_addr[(int)Box_Addr.DI_11] = 10;
_addr[(int)Box_Addr.DI_12] = 11;
_addr[(int)Box_Addr.DI_13] = 12;
_addr[(int)Box_Addr.DI_14] = 13;
_addr[(int)Box_Addr.DI_15] = 14;
_addr[(int)Box_Addr.DI_16] = 15;
_addr[(int)Box_Addr.DO_1] = 16;
_addr[(int)Box_Addr.DO_2] = 17;
_addr[(int)Box_Addr.DO_3] = 18;
_addr[(int)Box_Addr.DO_4] = 19;
_addr[(int)Box_Addr.DO_5] = 20;
_addr[(int)Box_Addr.DO_6] = 21;
_addr[(int)Box_Addr.DO_7] = 22;
_addr[(int)Box_Addr.DO_8] = 23;
_addr[(int)Box_Addr.DO_9] = 24;
_addr[(int)Box_Addr.DO_10] = 25;
_addr[(int)Box_Addr.DO_11] = 26;
_addr[(int)Box_Addr.DO_12] = 27;
_addr[(int)Box_Addr.DO_13] = 28;
_addr[(int)Box_Addr.DO_14] = 29;
_addr[(int)Box_Addr.DO_15] = 30;
_addr[(int)Box_Addr.DO_16] = 31;
}
else if (value == Box_Type.DO_16)
{
_addr[(int)Box_Addr.DO_1] = 0;
_addr[(int)Box_Addr.DO_2] = 1;
_addr[(int)Box_Addr.DO_3] = 2;
_addr[(int)Box_Addr.DO_4] = 3;
_addr[(int)Box_Addr.DO_5] = 4;
_addr[(int)Box_Addr.DO_6] = 5;
_addr[(int)Box_Addr.DO_7] = 6;
_addr[(int)Box_Addr.DO_8] = 7;
_addr[(int)Box_Addr.DO_9] = 8;
_addr[(int)Box_Addr.DO_10] = 9;
_addr[(int)Box_Addr.DO_11] = 10;
_addr[(int)Box_Addr.DO_12] = 11;
_addr[(int)Box_Addr.DO_13] = 12;
_addr[(int)Box_Addr.DO_14] = 13;
_addr[(int)Box_Addr.DO_15] = 14;
_addr[(int)Box_Addr.DO_16] = 15;
}
}
get
{
return _type;
}
}
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public bool Connect()
{
IsConn = false;
try
{
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(IP, pattern);
if (!rtn)
{
ErrInfo = "非法的IP地址 " + IP;
return false;
}
//Ping服务端
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(IP, 5000);
if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
{
ErrInfo = "Ping " + IP + " 请求没有回应";
return false;
}
_unrevd = 0;
_unrevdRemote = 0;
_flag = 0;
_send = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
_receive = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
_log.Clear();
_logRxTx.Clear();
//建立连接
//_client.Blocking = true;
_client = new Socket(IPAddress.Parse(IP).AddressFamily, SocketType.Stream, ProtocolType.Tcp);
_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 500);
_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 500);
_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
_client.BeginConnect(IP, Port, new AsyncCallback(Target), null);
Thread.Sleep(100); //需要等待一会才能获取连接状态
tReadDI = new Thread(new ThreadStart(AutoReadDI));
tReadDO = new Thread(new ThreadStart(AutoReadDO));
tSend = new Thread(new ThreadStart(Send));
tReceive = new Thread(new ThreadStart(Receive));
tLogOut = new Thread(new ThreadStart(LogPrint));
tSend.Start();
tReceive.Start();
tLogOut.Start();
tReadDI.Start();
tReadDO.Start();
ErrInfo = "OK";
IsConn = true;
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
LogUtil.error(ex.ToString());
return false;
}
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
if (tReconn != null)
tReconn.Abort();
tReconn = null;
CloseConn();
}
/// <summary>
/// 自动读取DI状态,触发DI_Changed_Event
/// </summary>
/// <param name="read">是否自动读取</param>
/// <param name="sleep">间隔,必须大于10ms</param>
public void AutoReadDI(bool read, int sleep)
{
if (_type == Box_Type.DO_16)
{
_readDI = false;
_readDISleep = 10000;
}
else
{
if (sleep < 10)
{
_readDI = false;
_readDISleep = 100;
}
else
{
if (read)
{
_readDI = true;
_readDISleep = sleep;
}
else
{
_readDI = false;
_readDISleep = 100;
}
}
}
}
/// <summary>
/// 自动读取DO状态,触发DO_Changed_Event
/// </summary>
/// <param name="read">是否自动读取</param>
/// <param name="sleep">间隔,必须大于10ms</param>
public void AutoReadDO(bool read, int sleep)
{
if (sleep < 10)
{
_readDO = false;
_readDOSleep = 100;
}
else
{
if (read)
{
_readDO = true;
_readDOSleep = sleep;
}
else
{
_readDO = false;
_readDOSleep = 100;
}
}
}
/// <summary>
/// 相反状态
/// </summary>
/// <param name="sta"></param>
/// <returns></returns>
public Box_Sta ReverseStatus(Box_Sta sta)
{
return sta == Box_Sta.On ? Box_Sta.Off : Box_Sta.On;
}
/// <summary>
/// 读取单个DI
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public Box_Sta ReadDI(Box_Addr add)
{
return _sta[(int)add];
}
/// <summary>
/// 读取多个DI
/// </summary>
/// <param name="add"></param>
/// <param name="count"></param>
/// <returns></returns>
public Box_Sta[] ReadDI(Box_Addr add, int count)
{
Box_Sta[] sta = new Box_Sta[count];
Array.Copy(_sta, (int)add, sta, 0, count);
return sta;
}
/// <summary>
/// 读取单个DO
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public Box_Sta ReadDO(Box_Addr add)
{
return _sta[(int)add];
}
/// <summary>
/// 读取多个DO
/// </summary>
/// <param name="add"></param>
/// <param name="count"></param>
/// <returns></returns>
public Box_Sta[] ReadDO(Box_Addr add, int count)
{
Box_Sta[] sta = new Box_Sta[count];
Array.Copy(_sta, (int)add, sta, 0, count);
return sta;
}
/// <summary>
/// 写入单个DO
/// </summary>
/// <param name="add"></param>
/// <param name="sta"></param>
/// <returns></returns>
public bool WriteDO(Box_Addr add, Box_Sta sta)
{
try
{
byte[] data = Command();
byte[] buff = new byte[12];
Array.Copy(data, 0, buff, 0, data.Length);
buff[7] = 5; //功能码
buff[9] = _addr[(int)add]; //地址
buff[10] = (byte)sta; //写入值
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
string s = string.Format("{0:HH:mm:ss.fff} WriteDO id={1} ({2},{3})", DateTime.Now, flag, add.ToString(), sta.ToString());
_log.Add(s);
}
//_send.Add(buff);
_send.Enqueue(buff);
if (_unrevdRemote == 0) ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
LogUtil.error(ex.ToString());
return false;
}
}
/// <summary>
/// 获取本地IPv4地址
/// </summary>
/// <returns></returns>
public string[] GetLocalIP()
{
List<string> str = new List<string>();
IPAddress[] add = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
foreach (IPAddress ip in add)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
str.Add(ip.ToString());
}
return str.ToArray();
}
/// <summary>
/// 发送命令
/// </summary>
private void Send()
{
string s;
ushort flag;
while (true)
{
//if (_send.Count > 0)
//{
// if (_send[0] != null)
// {
// try
// {
// _client.Send(_send[0]);
// if (LogOut)
// {
// byte[] bb = new byte[2];
// bb[0] = _send[0][1];
// bb[1] = _send[0][0];
// flag = BitConverter.ToUInt16(bb, 0);
// s = string.Format("{0:HH:mm:ss:fff} Send {1}", DateTime.Now, flag);
// _log.Add(s);
// _logRxTx.Add(flag + "," + _send[0][7]);
// }
// _send.RemoveAt(0);
// }
// catch (Exception ex)
// {
// ErrInfo = ex.Message;
// LogUtil.error(ex.ToString());
// _unrevdRemote = 1;
// break;
// }
// }
//}
bool rtn = _send.TryDequeue(out byte[] result);
if (rtn)
{
try
{
_client.Send(result);
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = result[1];
bb[1] = result[0];
flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} Send id={1} fun={2} len={3}", DateTime.Now, flag, result[7], result.Length);
_log.Add(s);
_logRxTx.Add(flag + "," + result[7]);
}
}
catch (Exception ex)
{
ErrInfo = ex.Message;
LogUtil.error(ex.ToString());
_unrevdRemote = 1;
break;
}
}
Thread.Sleep(SEND_SLEEP);
}
}
/// <summary>
/// 接收命令
/// </summary>
private void Receive()
{
while (true)
{
if (_receive.TryDequeue(out byte[] buff))
{
if (buff[7] == 1)
ReadDO(buff);
else if (buff[7] == 2)
ReadDI(buff);
else if (buff[7] == 5)
ReadSingle(buff);
}
Thread.Sleep(10);
}
}
/// <summary>
/// 读取单个DO
/// </summary>
/// <param name="buff"></param>
private void ReadSingle(byte[] buff)
{
//try
//{
string s;
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} WriteDO Receive {1}", DateTime.Now, flag);
_log.Add(s);
}
// int n = 0;
// int move = 0;
// byte val = _receive[0][9];
// for (int i = 0; i < 8; i++)
// {
// n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
// _sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
// move++;
// }
// if (_receive[0][8] == 2)
// {
// move = 0;
// val = _receive[0][10];
// for (int i = 8; i < 16; i++)
// {
// n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
// _sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
// move++;
// }
// }
// ErrInfo = "OK";
//}
//catch (Exception ex)
//{
// ErrInfo = ex.Message;
//}
}
/// <summary>
/// 读取所有DO状态
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
private bool ReadDO(byte[] buff)
{
try
{
string s;
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} ReadDO id={1} fun={2} len={3}", DateTime.Now, flag, buff[7], buff.Length);
s += Convert.ToString(buff[9], 2);
if (buff[8] == 2)
s += "," + Convert.ToString(buff[10], 2);
_log.Add(s);
}
int n = 0;
int move = 0;
byte val = buff[9];
for (int i = 0; i < 8; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
if (buff[8] == 2)
{
move = 0;
val = buff[10];
for (int i = 8; i < 16; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
}
if (_unrevdRemote == 0) ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
LogUtil.error(ex.ToString());
return false;
}
}
/// <summary>
/// 读取所有DI状态
/// </summary>
/// <returns></returns>
private bool ReadDI(byte[] buff)
{
try
{
string s;
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} ReadDI id={1} fun={2} len={3}", DateTime.Now, flag, buff[7], buff.Length);
s += Convert.ToString(buff[9], 2);
if (buff[8] == 2)
s += "," + Convert.ToString(buff[10], 2);
_log.Add(s);
}
int n = 0;
int move = 0;
byte val = buff[9];
for (int i = 0; i < 8; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
if (buff[8] == 2)
{
move = 0;
val = buff[10];
for (int i = 8; i < 16; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
}
if (_unrevdRemote == 0) ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
LogUtil.error(ex.ToString());
return false;
}
}
/// <summary>
/// 命令,前7个字节
/// </summary>
/// <returns></returns>
private byte[] Command()
{
byte[] add = BitConverter.GetBytes(++_flag);
byte[] data = new byte[7];
data[0] = add[1];
data[1] = add[0];
data[2] = 0;
data[3] = 0;
data[4] = 0;
data[5] = 0;
data[6] = 1;
if (_flag == ushort.MaxValue) _flag = 0;
return data;
}
/// <summary>
/// 触发DIO改变事件
/// </summary>
private void TriggerDIO()
{
int n;
Box_Sta[] sta = null;
while (true)
{
n = 0;
if (_readDI && DI_Changed_Event != null)
{
if (_type == Box_Type.DIO_16) sta = new Box_Sta[8];
else if (_type == Box_Type.DIO_32) sta = new Box_Sta[16];
Array.Copy(_sta, 0, sta, 0, sta.Length);
DI_Changed_Event.Invoke(this, sta);
Thread.Sleep(TRIG_SLEEP);
n++;
}
if (_readDO && DO_Changed_Event != null)
{
if (_type == Box_Type.DIO_16) sta = new Box_Sta[8];
else if (_type == Box_Type.DIO_32) sta = new Box_Sta[16];
else if (_type == Box_Type.DO_16) sta = new Box_Sta[16];
Array.Copy(_sta, 16, sta, 0, sta.Length);
DO_Changed_Event.Invoke(this, sta);
Thread.Sleep(TRIG_SLEEP);
n++;
}
if (n == 0)
Thread.Sleep(TRIG_SLEEP);
}
}
/// <summary>
/// 日志输出线程
/// </summary>
private void LogPrint()
{
int len = 0;
while (true)
{
if (LogOut && _log.Count > len)
{
len = _log.Count;
string[] ss = new string[len + 1];
_log.CopyTo(0, ss, 0, len);
Log_Out_Event?.Invoke(this, ss);
_log.RemoveRange(0, len);
len = 0;
}
if (LogOut)
{
Log_RxTx_Event?.Invoke(this, _logRxTx.ToArray());
_logRxTx.Clear();
}
Thread.Sleep(1000);
}
}
/// <summary>
/// 自动读取DI线程
/// </summary>
private void AutoReadDI()
{
while (true)
{
if (IsConn && _readDI)
{
byte[] data = Command();
byte[] buff = new byte[12];
Array.Copy(data, 0, buff, 0, data.Length);
buff[7] = 2; //功能码
buff[9] = _addr[(int)Box_Addr.DI_1]; //地址
if (_type == Box_Type.DIO_16)
buff[11] = 8; //个数
else if (_type == Box_Type.DIO_32)
buff[11] = 16; //个数
//_send.Add(buff);
_send.Enqueue(buff);
}
Thread.Sleep(_readDISleep);
}
}
/// <summary>
/// 自动读取DO线程
/// </summary>
private void AutoReadDO()
{
while (true)
{
if (IsConn && _readDO)
{
byte[] data = Command();
byte[] buff = new byte[12];
Array.Copy(data, 0, buff, 0, data.Length);
buff[7] = 1; //功能码
buff[9] = _addr[(int)Box_Addr.DO_1]; //地址
if (_type == Box_Type.DIO_16)
buff[11] = 8; //个数
else if (_type == Box_Type.DIO_32)
buff[11] = 16; //个数
else if (_type == Box_Type.DO_16)
buff[11] = 16; //个数
//_send.Add(buff);
_send.Enqueue(buff);
}
Thread.Sleep(_readDOSleep);
}
}
/// <summary>
/// 监听结果线程
/// </summary>
private void Listen()
{
byte[] bb = new byte[100];
while (true)
{
if (_client.Available > 0)
{
Thread.Sleep(2);
int len = _client.Receive(bb);
byte[] buff = new byte[len];
Array.Copy(bb, buff, len);
_receive.Enqueue(buff);
_unrevd = 0;
}
_unrevd += NET_SLEEP;
Thread.Sleep(NET_SLEEP);
}
}
/// <summary>
/// 回调函数,开启监听线程
/// </summary>
/// <param name="args"></param>
private void Target(IAsyncResult args)
{
if (!args.IsCompleted) return;
if (_client == null || !_client.Connected) return;
tListen = new Thread(new ThreadStart(Listen)) { IsBackground = true };
tTrigger = new Thread(new ThreadStart(TriggerDIO)) { IsBackground = true };
tListen.Start();
tTrigger.Start();
}
/// <summary>
/// 断开重连
/// </summary>
private void Reconn()
{
bool rtn = false;
bool loop = false;
bool conn = false;
int times = 0;
while (true)
{
try
{
if (IsConn)
{
if (_unrevd > 10000 || _unrevdRemote > 0) //断开10s后重连
{
//临时
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("send.Count = ", _send.Count.ToString());
foreach (System.Reflection.PropertyInfo info in _client.GetType().GetProperties())
{
try
{
dic.Add(info.Name, info.GetValue(_client) == null ? "null" : info.GetValue(_client).ToString());
}
catch (Exception ex)
{
dic.Add("错误" + info.Name, ex.Message);
}
}
CloseConn();
Thread.Sleep(100);
if (Reconnect_Event != null)
{
loop = false;
conn = false;
do
{
if (!loop)
{
if (_unrevdRemote == 0)
ErrInfo = "本地缓存连续10s未收到数据";
}
times++;
Reconnect_Event.Invoke(this, times, ref conn, dic);
if (conn)
{
rtn = Connect();
if (rtn)
{
loop = false;
times = 0;
}
else
{
conn = false;
loop = true;
}
}
else
{
loop = false;
}
} while (loop);
}
}
}
}
catch (Exception ex)
{
LogUtil.error("Reconn出错:" + ex.ToString());
}
Thread.Sleep(1000);
}
}
/// <summary>
/// 关闭连接
/// </summary>
private void CloseConn()
{
IsConn = false;
if (tListen != null) tListen.Abort();
tListen = null;
if (tReadDI != null) tReadDI.Abort();
tReadDI = null;
if (tReadDO != null) tReadDO.Abort();
tReadDO = null;
if (tTrigger != null) tTrigger.Abort();
tTrigger = null;
if (tLogOut != null) tLogOut.Abort();
tLogOut = null;
if (tSend != null) tSend.Abort();
tSend = null;
if (tReceive != null) tReceive.Abort();
tReceive = null;
if (_client != null)
{
_client.Shutdown(SocketShutdown.Both);
_client.Close();
}
_client = null;
}
}
}
\ No newline at end of file
/*
* @Description: 用于AIOBOX-32系列一体化IO模块
* @CreateDate: 2019-02-28
* @UpdateDate: 2019-05-22
* @Author: Asa
* @Version: 1.9
*
*/
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections.Generic;
using OnlineStore.Common;
namespace Asa.IOModule
{
/// <summary>
/// AIOBOX操作类,socket同步
/// </summary>
public class AIOBOX2
{
/// <summary>
/// 暂停次数
/// WriteDO 命令非常多时,暂停一次发送 ReadDI 或 ReadDO
/// </summary>
private int suspend;
private bool suspendDI; //暂停一次DI
private bool suspendDO; //暂停一次DI
private Socket _client; //客户端
private Box_Type _type; //类型
private byte[] _addr; //地址
private Box_Sta[] _sta; //状态
private int _unrevdRemote; //本地还是远程没有收到数据
private bool _readDI; //自动读取DI
private bool _readDO; //自动读取DO
private int _readDISleep; //自动读取DI间隔
private int _readDOSleep; //自动读取DO间隔
private List<string> _log; //日志
private System.Collections.Concurrent.ConcurrentQueue<ushort> _flag;
private System.Collections.Concurrent.ConcurrentQueue<byte[]> _send;
private System.Collections.Concurrent.ConcurrentQueue<byte[]> _receive;
//private bool suspendWrite;
//private AutoResetEvent aWrite;
//private AutoResetEvent aReadDI;
//private AutoResetEvent aReadDO;
private Thread tSend; //发送命令处理
private Thread tReceive; //接收信息处理
private Thread tListen; //监听网络
private Thread tTrigger; //触发DI、DO改变事件
private Thread tReadDI; //自动读取DI线程
private Thread tReadDO; //自动读取DO线程
private Thread tLogOut; //日志输出
private Thread tFlag; //ModBusTCP标识
/// <summary>
/// 每条命令发送的间隔
/// 不能小于15,会出现IO接收不到的情况
/// 小于30时,会出现接收数据连包的情况
/// </summary>
private const int SEND_SLEEP = 15;
/// <summary>
/// 监听网络接收数据的间隔
/// 必须小于SEND_SLEEP
/// </summary>
private const int NET_SLEEP = 10;
/// <summary>
/// 触发DIO状态事件的间隔
/// </summary>
private const int TRIG_SLEEP = 20;
/// <summary>
/// 自动读取DI委托
/// </summary>
/// <param name="box">AIOBOX</param>
/// <param name="sta">所有DI状态</param>
public delegate void DI_Changed(AIOBOX2 box, Box_Sta[] sta);
/// <summary>
/// 自动读取DI事件触发
/// </summary>
public event DI_Changed DI_Changed_Event;
/// <summary>
/// 自动读取DO委托
/// </summary>
/// <param name="box">AIOBOX</param>
/// <param name="sta">所有DO状态</param>
public delegate void DO_Changed(AIOBOX2 box, Box_Sta[] sta);
/// <summary>
/// 自动读取DO事件触发
/// </summary>
public event DO_Changed DO_Changed_Event;
/// <summary>
/// 日志输出
/// </summary>
/// <param name="box"></param>
/// <param name="s"></param>
public delegate void Log_Out(AIOBOX2 box, string[] s);
/// <summary>
/// 日志输出事件
/// </summary>
public event Log_Out Log_Out_Event;
/// <summary>
/// AIOBOX
/// </summary>
public AIOBOX2()
{
_readDI = false;
_readDO = false;
_readDISleep = 100;
_readDOSleep = 100;
_addr = new byte[32];
_sta = new Box_Sta[32];
_log = new List<string>();
Type = Box_Type.DIO_32;
//aWrite = new AutoResetEvent(false);
//aReadDI = new AutoResetEvent(false);
//aReadDO = new AutoResetEvent(false);
}
/// <summary>
/// IP地址
/// </summary>
public string IP { set; get; } = "192.168.1.100";
/// <summary>
/// ModBus端口
/// </summary>
public int Port { set; get; } = 502;
/// <summary>
/// 是否连接
/// </summary>
public bool IsConn { get; private set; } = false;
/// <summary>
/// 错误信息
/// </summary>
public string ErrInfo { get; private set; } = "";
/// <summary>
/// 日志输出
/// </summary>
public bool LogOut { set; get; } = false;
/// <summary>
/// 模块的类型
/// </summary>
public Box_Type Type
{
set
{
_type = value;
if (value == Box_Type.DIO_16)
{
_addr[(int)Box_Addr.DI_1] = 0;
_addr[(int)Box_Addr.DI_2] = 1;
_addr[(int)Box_Addr.DI_3] = 2;
_addr[(int)Box_Addr.DI_4] = 3;
_addr[(int)Box_Addr.DI_5] = 4;
_addr[(int)Box_Addr.DI_6] = 5;
_addr[(int)Box_Addr.DI_7] = 6;
_addr[(int)Box_Addr.DI_8] = 7;
_addr[(int)Box_Addr.DI_9] = 255;
_addr[(int)Box_Addr.DI_10] = 255;
_addr[(int)Box_Addr.DI_11] = 255;
_addr[(int)Box_Addr.DI_12] = 255;
_addr[(int)Box_Addr.DI_13] = 255;
_addr[(int)Box_Addr.DI_14] = 255;
_addr[(int)Box_Addr.DI_15] = 255;
_addr[(int)Box_Addr.DI_16] = 255;
_addr[(int)Box_Addr.DO_1] = 8;
_addr[(int)Box_Addr.DO_2] = 9;
_addr[(int)Box_Addr.DO_3] = 10;
_addr[(int)Box_Addr.DO_4] = 11;
_addr[(int)Box_Addr.DO_5] = 12;
_addr[(int)Box_Addr.DO_6] = 13;
_addr[(int)Box_Addr.DO_7] = 14;
_addr[(int)Box_Addr.DO_8] = 15;
_addr[(int)Box_Addr.DO_9] = 255;
_addr[(int)Box_Addr.DO_10] = 255;
_addr[(int)Box_Addr.DO_11] = 255;
_addr[(int)Box_Addr.DO_12] = 255;
_addr[(int)Box_Addr.DO_13] = 255;
_addr[(int)Box_Addr.DO_14] = 255;
_addr[(int)Box_Addr.DO_15] = 255;
_addr[(int)Box_Addr.DO_16] = 255;
}
else if (value == Box_Type.DIO_32)
{
_addr[(int)Box_Addr.DI_1] = 0;
_addr[(int)Box_Addr.DI_2] = 1;
_addr[(int)Box_Addr.DI_3] = 2;
_addr[(int)Box_Addr.DI_4] = 3;
_addr[(int)Box_Addr.DI_5] = 4;
_addr[(int)Box_Addr.DI_6] = 5;
_addr[(int)Box_Addr.DI_7] = 6;
_addr[(int)Box_Addr.DI_8] = 7;
_addr[(int)Box_Addr.DI_9] = 8;
_addr[(int)Box_Addr.DI_10] = 9;
_addr[(int)Box_Addr.DI_11] = 10;
_addr[(int)Box_Addr.DI_12] = 11;
_addr[(int)Box_Addr.DI_13] = 12;
_addr[(int)Box_Addr.DI_14] = 13;
_addr[(int)Box_Addr.DI_15] = 14;
_addr[(int)Box_Addr.DI_16] = 15;
_addr[(int)Box_Addr.DO_1] = 16;
_addr[(int)Box_Addr.DO_2] = 17;
_addr[(int)Box_Addr.DO_3] = 18;
_addr[(int)Box_Addr.DO_4] = 19;
_addr[(int)Box_Addr.DO_5] = 20;
_addr[(int)Box_Addr.DO_6] = 21;
_addr[(int)Box_Addr.DO_7] = 22;
_addr[(int)Box_Addr.DO_8] = 23;
_addr[(int)Box_Addr.DO_9] = 24;
_addr[(int)Box_Addr.DO_10] = 25;
_addr[(int)Box_Addr.DO_11] = 26;
_addr[(int)Box_Addr.DO_12] = 27;
_addr[(int)Box_Addr.DO_13] = 28;
_addr[(int)Box_Addr.DO_14] = 29;
_addr[(int)Box_Addr.DO_15] = 30;
_addr[(int)Box_Addr.DO_16] = 31;
}
else if (value == Box_Type.DO_16)
{
_addr[(int)Box_Addr.DO_1] = 0;
_addr[(int)Box_Addr.DO_2] = 1;
_addr[(int)Box_Addr.DO_3] = 2;
_addr[(int)Box_Addr.DO_4] = 3;
_addr[(int)Box_Addr.DO_5] = 4;
_addr[(int)Box_Addr.DO_6] = 5;
_addr[(int)Box_Addr.DO_7] = 6;
_addr[(int)Box_Addr.DO_8] = 7;
_addr[(int)Box_Addr.DO_9] = 8;
_addr[(int)Box_Addr.DO_10] = 9;
_addr[(int)Box_Addr.DO_11] = 10;
_addr[(int)Box_Addr.DO_12] = 11;
_addr[(int)Box_Addr.DO_13] = 12;
_addr[(int)Box_Addr.DO_14] = 13;
_addr[(int)Box_Addr.DO_15] = 14;
_addr[(int)Box_Addr.DO_16] = 15;
}
}
get
{
return _type;
}
}
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public bool Connect()
{
IsConn = false;
try
{
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(IP, pattern);
if (!rtn)
{
ErrInfo = "非法的IP地址 " + IP;
return false;
}
//Ping服务端
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(IP, 5000);
if (result.Status != System.Net.NetworkInformation.IPStatus.Success)
{
ErrInfo = "Ping " + IP + " 请求没有回应";
return false;
}
_unrevdRemote = 0;
//suspendWrite = false;
suspend = 0;
suspendDI = false;
suspendDO = false;
_send = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
_receive = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();
_flag = new System.Collections.Concurrent.ConcurrentQueue<ushort>();
_log.Clear();
//建立连接
_client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 500);
_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 500);
_client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
//_client.BeginConnect(IP, Port, new AsyncCallback(ConnectCallback), null);
_client.Connect(IPAddress.Parse(IP), Port);
Thread.Sleep(100); //需要等待一会才能获取连接状态
tFlag = new Thread(new ThreadStart(Flag));
tFlag.Start();
Thread.Sleep(10);
tReadDI = new Thread(new ThreadStart(AutoReadDI));
tReadDO = new Thread(new ThreadStart(AutoReadDO));
tSend = new Thread(new ThreadStart(Send));
tReceive = new Thread(new ThreadStart(Receive));
tLogOut = new Thread(new ThreadStart(LogPrint));
tListen = new Thread(new ThreadStart(Listen));
tTrigger = new Thread(new ThreadStart(TriggerDIO));
tListen.Start();
tTrigger.Start();
tSend.Start();
tReceive.Start();
tLogOut.Start();
tReadDI.Start();
tReadDO.Start();
ErrInfo = "OK";
IsConn = true;
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
/// <summary>
/// 关闭连接
/// </summary>
public void Close()
{
IsConn = false;
if (tListen != null) tListen.Abort();
tListen = null;
if (tReadDI != null) tReadDI.Abort();
tReadDI = null;
if (tReadDO != null) tReadDO.Abort();
tReadDO = null;
if (tTrigger != null) tTrigger.Abort();
tTrigger = null;
if (tLogOut != null) tLogOut.Abort();
tLogOut = null;
if (tSend != null) tSend.Abort();
tSend = null;
if (tReceive != null) tReceive.Abort();
tReceive = null;
if (tFlag != null) tFlag.Abort();
tFlag = null;
if (_client != null)
{
_client.Shutdown(SocketShutdown.Both);
_client.Close();
}
_client = null;
}
/// <summary>
/// 自动读取DI状态,触发DI_Changed_Event
/// </summary>
/// <param name="read">是否自动读取</param>
/// <param name="sleep">间隔,必须大于10ms</param>
public void AutoReadDI(bool read, int sleep)
{
if (_type == Box_Type.DO_16)
{
_readDI = false;
_readDISleep = 10000;
}
else
{
if (sleep < 10)
{
_readDI = false;
_readDISleep = 100;
}
else
{
if (read)
{
_readDI = true;
_readDISleep = sleep;
}
else
{
_readDI = false;
_readDISleep = 100;
}
}
}
}
/// <summary>
/// 自动读取DO状态,触发DO_Changed_Event
/// </summary>
/// <param name="read">是否自动读取</param>
/// <param name="sleep">间隔,必须大于10ms</param>
public void AutoReadDO(bool read, int sleep)
{
if (sleep < 10)
{
_readDO = false;
_readDOSleep = 100;
}
else
{
if (read)
{
_readDO = true;
_readDOSleep = sleep;
}
else
{
_readDO = false;
_readDOSleep = 100;
}
}
}
/// <summary>
/// 相反状态
/// </summary>
/// <param name="sta"></param>
/// <returns></returns>
public Box_Sta ReverseStatus(Box_Sta sta)
{
return sta == Box_Sta.On ? Box_Sta.Off : Box_Sta.On;
}
/// <summary>
/// 读取单个DI
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public Box_Sta ReadDI(Box_Addr add)
{
return _sta[(int)add];
}
/// <summary>
/// 读取多个DI
/// </summary>
/// <param name="add"></param>
/// <param name="count"></param>
/// <returns></returns>
public Box_Sta[] ReadDI(Box_Addr add, int count)
{
Box_Sta[] sta = new Box_Sta[count];
Array.Copy(_sta, (int)add, sta, 0, count);
return sta;
}
/// <summary>
/// 读取单个DO
/// </summary>
/// <param name="add"></param>
/// <returns></returns>
public Box_Sta ReadDO(Box_Addr add)
{
return _sta[(int)add];
}
/// <summary>
/// 读取多个DO
/// </summary>
/// <param name="add"></param>
/// <param name="count"></param>
/// <returns></returns>
public Box_Sta[] ReadDO(Box_Addr add, int count)
{
Box_Sta[] sta = new Box_Sta[count];
Array.Copy(_sta, (int)add, sta, 0, count);
return sta;
}
/// <summary>
/// 写入单个DO
/// </summary>
/// <param name="add"></param>
/// <param name="sta"></param>
/// <returns></returns>
public bool WriteDO(Box_Addr add, Box_Sta sta)
{
try
{
//suspendWrite = true;
//if (_readDI) aWrite.WaitOne();
//if (_readDO) aWrite.WaitOne();
//if (_send.Count > 10)
//{
// while (_send.TryDequeue(out byte[] result))
// { }
//}
byte[] data = Command();
byte[] buff = new byte[12];
Array.Copy(data, 0, buff, 0, data.Length);
buff[7] = 5; //功能码
buff[9] = _addr[(int)add]; //地址
buff[10] = (byte)sta; //写入值
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
string s = string.Format("{0:HH:mm:ss.fff} WriteDO {1} ({2},{3})", DateTime.Now, flag, add.ToString(), sta.ToString());
_log.Add(s);
}
suspend++;
_send.Enqueue(buff);
//suspendWrite = false;
//aReadDI.Set();
//aReadDO.Set();
if (_unrevdRemote == 0) ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
/// <summary>
/// 获取本地IPv4地址
/// </summary>
/// <returns></returns>
public string[] GetLocalIP()
{
List<string> str = new List<string>();
IPAddress[] add = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
foreach (IPAddress ip in add)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
str.Add(ip.ToString());
}
return str.ToArray();
}
/// <summary>
/// 发送命令
/// </summary>
private void Send()
{
string s;
ushort flag;
while (true)
{
bool rtn = _send.TryDequeue(out byte[] result);
if (rtn)
{
try
{
_client.Send(result);
//_client.BeginSend(result, 0, result.Length, SocketFlags.None, new AsyncCallback(SendCallback), _client);
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = result[1];
bb[1] = result[0];
flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} Send {1} fun={2} len={3}", DateTime.Now, flag, result[7], result.Length);
_log.Add(s);
}
}
catch (Exception ex)
{
ErrInfo = ex.Message;
_unrevdRemote = 1;
break;
}
}
Thread.Sleep(SEND_SLEEP);
}
}
/// <summary>
/// 接收命令
/// </summary>
private void Receive()
{
while (true)
{
try
{
if (_receive.TryDequeue(out byte[] buff))
{
if (buff.Length >= 8)
{
if (buff[7] == 1)
ReadDO(buff);
else if (buff[7] == 2)
ReadDI(buff);
else if (buff[7] == 5)
ReadSingle(buff);
}
else
{
string s = "";
foreach(byte b in buff)
{
s += b + " ";
}
LogUtil.error("AIOBOX2 Receive长度不足:" + s);
}
}
}catch(Exception ex)
{
LogUtil.error("AIOBOX2 Receive出错:" + ex.ToString());
}
Thread.Sleep(10);
}
}
/// <summary>
/// 读取单个DO
/// </summary>
/// <param name="buff"></param>
private void ReadSingle(byte[] buff)
{
//try
//{
string s;
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} WriteDO {1} fun={2} len={3}", DateTime.Now, flag, buff[7], buff.Length);
_log.Add(s);
}
// int n = 0;
// int move = 0;
// byte val = _receive[0][9];
// for (int i = 0; i < 8; i++)
// {
// n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
// _sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
// move++;
// }
// if (_receive[0][8] == 2)
// {
// move = 0;
// val = _receive[0][10];
// for (int i = 8; i < 16; i++)
// {
// n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
// _sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
// move++;
// }
// }
// ErrInfo = "OK";
//}
//catch (Exception ex)
//{
// ErrInfo = ex.Message;
//}
}
/// <summary>
/// 读取所有DO状态
/// </summary>
/// <param name="buff"></param>
/// <returns></returns>
private bool ReadDO(byte[] buff)
{
try
{
string s;
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} ReadDO {1} fun={2} len={3} (", DateTime.Now, flag, buff[7], buff.Length);
s += Convert.ToString(buff[9], 2);
if (buff[8] == 2)
s += "," + Convert.ToString(buff[10], 2);
s += ")";
_log.Add(s);
}
int n = 0;
int move = 0;
byte val = buff[9];
for (int i = 0; i < 8; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
if (buff[8] == 2)
{
move = 0;
val = buff[10];
for (int i = 8; i < 16; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i + 16] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
}
if (_unrevdRemote == 0) ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
/// <summary>
/// 读取所有DI状态
/// </summary>
/// <returns></returns>
private bool ReadDI(byte[] buff)
{
try
{
string s;
if (LogOut)
{
byte[] bb = new byte[2];
bb[0] = buff[1];
bb[1] = buff[0];
ushort flag = BitConverter.ToUInt16(bb, 0);
s = string.Format("{0:HH:mm:ss:fff} ReadDI {1} fun={2} len={3} (", DateTime.Now, flag, buff[7], buff.Length);
s += Convert.ToString(buff[9], 2);
if (buff[8] == 2)
s += "," + Convert.ToString(buff[10], 2);
s += ")";
_log.Add(s);
}
int n = 0;
int move = 0;
byte val = buff[9];
for (int i = 0; i < 8; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
if (buff[8] == 2)
{
move = 0;
val = buff[10];
for (int i = 8; i < 16; i++)
{
n = (val & Convert.ToInt32(Math.Pow(2, move))) >> move;
_sta[i] = n == 1 ? Box_Sta.On : Box_Sta.Off;
move++;
}
}
if (_unrevdRemote == 0) ErrInfo = "OK";
return true;
}
catch (Exception ex)
{
ErrInfo = ex.Message;
return false;
}
}
/// <summary>
/// 命令,前7个字节
/// </summary>
/// <returns></returns>
private byte[] Command()
{
_flag.TryDequeue(out ushort result);
byte[] add = BitConverter.GetBytes(result);
byte[] data = new byte[7];
data[0] = add[1];
data[1] = add[0];
data[2] = 0;
data[3] = 0;
data[4] = 0;
data[5] = 0;
data[6] = 1;
return data;
}
/// <summary>
/// 触发DIO改变事件
/// </summary>
private void TriggerDIO()
{
int n;
Box_Sta[] sta = null;
while (true)
{
n = 0;
if (_readDI && DI_Changed_Event != null)
{
if (_type == Box_Type.DIO_16) sta = new Box_Sta[8];
else if (_type == Box_Type.DIO_32) sta = new Box_Sta[16];
Array.Copy(_sta, 0, sta, 0, sta.Length);
DI_Changed_Event.Invoke(this, sta);
Thread.Sleep(TRIG_SLEEP);
n++;
}
if (_readDO && DO_Changed_Event != null)
{
if (_type == Box_Type.DIO_16) sta = new Box_Sta[8];
else if (_type == Box_Type.DIO_32) sta = new Box_Sta[16];
else if (_type == Box_Type.DO_16) sta = new Box_Sta[16];
Array.Copy(_sta, 16, sta, 0, sta.Length);
DO_Changed_Event.Invoke(this, sta);
Thread.Sleep(TRIG_SLEEP);
n++;
}
if (n == 0)
Thread.Sleep(TRIG_SLEEP);
}
}
/// <summary>
/// 日志输出线程
/// </summary>
private void LogPrint()
{
int len = 0;
while (true)
{
if (LogOut && _log.Count > len)
{
len = _log.Count;
string[] ss = new string[len + 1];
_log.CopyTo(0, ss, 0, len);
Log_Out_Event?.Invoke(this, ss);
_log.RemoveRange(0, len);
len = 0;
}
Thread.Sleep(1000);
}
}
/// <summary>
/// 自动读取DI线程
/// </summary>
private void AutoReadDI()
{
while (true)
{
if (IsConn && _readDI)
{
//if (suspendWrite)
//{
// aWrite.Set();
// aReadDI.WaitOne();
//}
if (suspendDI && suspend > 0)
{
suspendDI = false;
suspend--;
}
else
{
byte[] data = Command();
byte[] buff = new byte[12];
Array.Copy(data, 0, buff, 0, data.Length);
buff[7] = 2; //功能码
buff[9] = _addr[(int)Box_Addr.DI_1]; //地址
if (_type == Box_Type.DIO_16)
buff[11] = 8; //个数
else if (_type == Box_Type.DIO_32)
buff[11] = 16; //个数
_send.Enqueue(buff);
suspendDI = true;
}
}
Thread.Sleep(_readDISleep);
}
}
/// <summary>
/// 自动读取DO线程
/// </summary>
private void AutoReadDO()
{
while (true)
{
if (IsConn && _readDO)
{
//if (suspendWrite)
//{
// aWrite.Set();
// aReadDO.WaitOne();
//}
if (suspendDO && suspend > 0)
{
suspendDO = false;
suspend--;
}
else
{
byte[] data = Command();
byte[] buff = new byte[12];
Array.Copy(data, 0, buff, 0, data.Length);
buff[7] = 1; //功能码
buff[9] = _addr[(int)Box_Addr.DO_1]; //地址
if (_type == Box_Type.DIO_16)
buff[11] = 8; //个数
else if (_type == Box_Type.DIO_32)
buff[11] = 16; //个数
else if (_type == Box_Type.DO_16)
buff[11] = 16; //个数
_send.Enqueue(buff);
suspendDO = true;
}
}
Thread.Sleep(_readDOSleep);
}
}
/// <summary>
/// 监听结果线程
/// </summary>
private void Listen()
{
byte[] temp = new byte[50];
while (true)
{
if (_client.Available > 0)
{
//_client.BeginReceive(temp, 0, temp.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), _client);
int len = _client.Receive(temp);
if (len > 15) //连包
{
byte[] aa = new byte[len / 2];
byte[] bb = new byte[len - aa.Length];
Array.Copy(temp, 0, aa, 0, aa.Length);
Array.Copy(temp, aa.Length, bb, 0, bb.Length);
_receive.Enqueue(aa);
_receive.Enqueue(bb);
}
else
{
byte[] cc = new byte[len];
Array.Copy(temp, cc, len);
_receive.Enqueue(cc);
}
}
Thread.Sleep(NET_SLEEP);
}
}
private void SendCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
int bytesSent = client.EndSend(ar);
}
catch (Exception ex)
{
ErrInfo = ex.Message;
}
}
private void ReceiveCallback(IAsyncResult ar)
{
try
{
int len = _client.EndReceive(ar);
//if (len > 0)
//{
// if (len > 15) //连包
// {
// byte[] aa = new byte[len / 2];
// byte[] bb = new byte[len - aa.Length];
// Array.Copy(temp, 0, aa, 0, aa.Length);
// Array.Copy(temp, aa.Length, bb, 0, bb.Length);
// _receive.Enqueue(aa);
// _receive.Enqueue(bb);
// }
// else
// {
// byte[] cc = new byte[len];
// Array.Copy(temp, cc, len);
// _receive.Enqueue(cc);
// }
//}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private void ConnectCallback(IAsyncResult ar)
{
if (!ar.IsCompleted) return;
if (_client == null || !_client.Connected) return;
_client.EndConnect(ar);
}
private void Flag()
{
ushort n = 0;
while (true)
{
if (_flag.Count < 10)
{
_flag.Enqueue(++n);
if (n == ushort.MaxValue) n = 0;
}
Thread.Sleep(5);
}
}
}
}
\ No newline at end of file

namespace Asa.IOModule
{
/// <summary>
/// IO模块寄存器
/// </summary>
public class Reg
{
/// <summary>
/// 标识
/// </summary>
public int ID;
/// <summary>
/// 文本,仅用于显示
/// </summary>
public string Text;
/// <summary>
/// 寄存器地址
/// </summary>
public Box_Addr Address;
/// <summary>
/// 当前的状态
/// </summary>
public Box_Sta Status;
/// <summary>
/// 寄存器,文本空,状态OFF
/// </summary>
/// <param name="address">地址</param>
public Reg(Box_Addr address) : this(0, "", address, Box_Sta.Off)
{
}
/// <summary>
/// 寄存器,状态OFF
/// </summary>
/// <param name="text">文本</param>
/// <param name="addr">地址</param>
public Reg(string text, Box_Addr addr) : this(0, text, addr, Box_Sta.Off)
{
}
/// <summary>
/// 寄存器,状态OFF
/// </summary>
/// <param name="id">标志</param>
/// <param name="text">文本</param>
/// <param name="addr">地址</param>
public Reg(int id, string text, Box_Addr addr) : this(id, text, addr, Box_Sta.Off)
{
}
/// <summary>
/// 寄存器
/// </summary>
/// <param name="text">文本</param>
/// <param name="addr">地址</param>
/// <param name="status">状态</param>
public Reg(string text, Box_Addr addr, Box_Sta status) : this(0, text, addr, status)
{
}
/// <summary>
/// 寄存器
/// </summary>
/// <param name="id">标志</param>
/// <param name="text">文本</param>
/// <param name="address">地址</param>
/// <param name="status">状态</param>
public Reg(int id, string text, Box_Addr address, Box_Sta status)
{
ID = id;
Text = text;
Address = address;
Status = status;
}
}
/// <summary>
/// IO模块寄存器地址
/// </summary>
public enum Box_Addr : int
{
/// <summary>
/// 输入点,DI01
/// </summary>
DI_1,
/// <summary>
/// 输入点,DI02
/// </summary>
DI_2,
/// <summary>
/// 输入点,DI03
/// </summary>
DI_3,
/// <summary>
/// 输入点,DI04
/// </summary>
DI_4,
/// <summary>
/// 输入点,DI05
/// </summary>
DI_5,
/// <summary>
/// 输入点,DI06
/// </summary>
DI_6,
/// <summary>
/// 输入点,DI07
/// </summary>
DI_7,
/// <summary>
/// 输入点,DI08
/// </summary>
DI_8,
/// <summary>
/// 输入点,DI09
/// </summary>
DI_9,
/// <summary>
/// 输入点,DI10
/// </summary>
DI_10,
/// <summary>
/// 输入点,DI11
/// </summary>
DI_11,
/// <summary>
/// 输入点,DI12
/// </summary>
DI_12,
/// <summary>
/// 输入点,DI13
/// </summary>
DI_13,
/// <summary>
/// 输入点,DI14
/// </summary>
DI_14,
/// <summary>
/// 输入点,DI15
/// </summary>
DI_15,
/// <summary>
/// 输入点,DI16
/// </summary>
DI_16,
/// <summary>
/// 输出点,DO01
/// </summary>
DO_1,
/// <summary>
/// 输出点,DO02
/// </summary>
DO_2,
/// <summary>
/// 输出点,DO03
/// </summary>
DO_3,
/// <summary>
/// 输出点,DO04
/// </summary>
DO_4,
/// <summary>
/// 输出点,DO05
/// </summary>
DO_5,
/// <summary>
/// 输出点,DO06
/// </summary>
DO_6,
/// <summary>
/// 输出点,DO07
/// </summary>
DO_7,
/// <summary>
/// 输出点,DO08
/// </summary>
DO_8,
/// <summary>
/// 输出点,DO09
/// </summary>
DO_9,
/// <summary>
/// 输出点,DO10
/// </summary>
DO_10,
/// <summary>
/// 输出点,DO11
/// </summary>
DO_11,
/// <summary>
/// 输出点,DO12
/// </summary>
DO_12,
/// <summary>
/// 输出点,DO13
/// </summary>
DO_13,
/// <summary>
/// 输出点,DO14
/// </summary>
DO_14,
/// <summary>
/// 输出点,DO15
/// </summary>
DO_15,
/// <summary>
/// 输出点,DO16
/// </summary>
DO_16,
/// <summary>
/// 无
/// </summary>
NONE = 255
}
/// <summary>
/// IO模块类型
/// </summary>
public enum Box_Type : int
{
/// <summary>
/// 16位,8DI + 8DO
/// </summary>
DIO_16,
/// <summary>
/// 32位,16DI + 16DO
/// </summary>
DIO_32,
/// <summary>
/// 16位DO
/// </summary>
DO_16
}
/// <summary>
/// IO模块寄存器状态
/// </summary>
public enum Box_Sta : int
{
/// <summary>
/// 断开,关闭,低电平
/// </summary>
Off = 0,
/// <summary>
/// 闭合,打开,高电平
/// </summary>
On = 255
}
}
\ No newline at end of file
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace OnlineStore.DeviceLibrary
{
public abstract class IOManager
{
public static IOManager instance = null;
#region KNDIO
public static void IOMove(string ioType, IO_VALUE ioValue)
{
if (StoreManager.Config.DOList.ContainsKey(ioType))
{
ConfigIO configIo = StoreManager.Config.DOList[ioType];
//DateTime time = DateTime.Now;
instance.WriteSingleDO(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr(), ioValue);
//TimeSpan span = DateTime.Now - time;
//if (span.TotalMilliseconds > 100)
//{
// LogUtil.error("IOMove [" + ioType + "] ["+ioValue+"]耗时" + span.TotalMilliseconds);
//}
Thread.Sleep(60);
}
else
{
LogUtil.error("没有DO=" + ioType);
}
}
public static IO_VALUE IOValue(string ioType)
{
IO_VALUE ioValue = IO_VALUE.LOW;
if (StoreManager.Config.DILIst.ContainsKey(ioType))
{
ConfigIO configIo = StoreManager.Config.DILIst[ioType];
ioValue = instance.GetDIValue(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr());
//UpdateDoValue(ioType, ioValue);
}
else if (StoreManager.Config.DOList.ContainsKey(ioType))
{
ConfigIO configIo = StoreManager.Config.DOList[ioType];
ioValue = instance.GetDOValue(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr());
}
else
{
LogUtil.error("没有DO=" + ioType);
}
return ioValue;
}
public static IO_VALUE DOValue(string ioType)
{
IO_VALUE ioValue = IO_VALUE.LOW;
if (StoreManager.Config.DOList.ContainsKey(ioType))
{
ConfigIO configIo = StoreManager.Config.DOList[ioType];
ioValue = instance.GetDOValue(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr());
}
else
{
LogUtil.error("没有DO=" + ioType);
}
return ioValue;
}
#endregion
public static void Init()
{
bool isAIOBox = ConfigAppSettings.GetIntValue(Setting_Init.UseAIOBOX).Equals(1);
if (isAIOBox)
{
instance = new AIOBOXManager();
}
else
{
instance = new KNDManager();
}
}
protected static ushort GetDILength(string ioIp)
{
try
{
return StoreManager.Config.GetDILength(ioIp);
}
catch (Exception ex)
{
}
return 16;
}
protected static ushort GetDOLength(string ioIp)
{
try
{
return StoreManager.Config.GetDOLength(ioIp);
}
catch (Exception ex)
{
}
return 16;
}
public abstract void ConnectionIOList(List<string> dIODeviceNameList);
public abstract void ReadAllDI(string deviceName, byte slaveId);
public abstract void ReadAllDO(string deviceName, byte slaveId);
public abstract void WriteSingleDO(string deviceName, byte slaveId, ushort index, IO_VALUE value, int time);
public abstract void WriteSingleDO(string deviceName, byte slaveId, ushort index, IO_VALUE value);
public abstract IO_VALUE GetDIValue(string deviceName, byte slaveID, ushort v);
public abstract IO_VALUE GetDOValue(string deviceName, byte slaveID, ushort v);
public abstract IO_VALUE GetIOValue(ConfigIO configIO);
public abstract void CloseAllDO();
public abstract void CloseAllConnection();
}
}
using log4net;
using OnlineStore.Common;
//using OnlineStore.DeviceLibrary.doubleStore;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
namespace OnlineStore.DeviceLibrary
{
//public class KND
//{
// #region KNDIO
// public static void IOMove(string ioType, IO_VALUE ioValue)
// {
// if (StoreManager.Config.StoreDOList.ContainsKey(ioType))
// {
// ConfigIO configIo = StoreManager.Config.StoreDOList[ioType];
// KNDManager.WriteSingleDO(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr(), ioValue);
// Thread.Sleep(60);
// }
// else
// {
// LogUtil.error( "没有DO=" + ioType);
// }
// }
// public static IO_VALUE IOValue(string ioType)
// {
// IO_VALUE ioValue = IO_VALUE.LOW;
// if (StoreManager.Config.StoreDIList.ContainsKey(ioType))
// {
// ConfigIO configIo = StoreManager.Config.StoreDIList[ioType];
// ioValue = KNDManager.GetDIValue(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr());
// //UpdateDoValue(ioType, ioValue);
// }
// else if (StoreManager.Config.StoreDOList.ContainsKey(ioType))
// {
// ConfigIO configIo = StoreManager.Config.StoreDOList[ioType];
// ioValue = KNDManager.GetDOValue(configIo.DeviceName, configIo.SlaveID, configIo.GetIOAddr());
// }
// else
// {
// LogUtil.error( "没有DO=" + ioType);
// }
// return ioValue;
// }
// #endregion
//}
/// <summary>
/// 康奈德IO控制模块
/// </summary>
public class KNDManager : IOManager
{
public static ushort DIStartAddress = 200;
public static ushort DoStartAddress = 100;
//public static ushort DefaultDILength = 16;
//public static ushort DefaultDOLength = 16;
private static byte DefualtSlaveID = 255;
public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static Dictionary<string, MasterTcpClient> mastMap = new Dictionary<string, MasterTcpClient>();
public static Dictionary<string, List<KNDIO>> DIValueMap = new Dictionary<string, List<KNDIO>>();
public static Dictionary<string, List<KNDIO>> DOValueMap = new Dictionary<string, List<KNDIO>>();
private static object DIMapLock = "";
private static object DOMapLock = "";
public static System.Timers.Timer timer = null;
private static ushort port = 502;
public static void ConnectionIP(string ioIp)
{
if (timer == null)
{
timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.AutoReset = true;
timer.Elapsed += timer_Elapsed;
timer.Enabled = true;
}
MasterTcpClient MBmaster = null;
if (mastMap.ContainsKey(ioIp))
{
MBmaster = mastMap[ioIp];
if (null != MBmaster)
{
MBmaster.disconnect();
MBmaster.Dispose();
MBmaster = null;
lock (DIMapLock)
{
if (DIValueMap.ContainsKey(ioIp))
{
DIValueMap.Remove(ioIp);
}
}
lock (DOMapLock)
{
if (DOValueMap.ContainsKey(ioIp))
{
DOValueMap.Remove(ioIp);
}
}
}
mastMap.Remove(ioIp);
}
try
{
// Create new modbus master and add event functions
MBmaster = new MasterTcpClient(ioIp, port);
MBmaster.OnResponseData += new MasterTcpClient.ResponseData(MBmaster_OnResponseData);
MBmaster.OnException += new MasterTcpClient.ExceptionData(MBmaster_OnException);
MBmaster.autoConnectOfBreak = false;
mastMap.Add(ioIp, MBmaster);
Thread.Sleep(10);
//读取所有的DO
ReadMultipleDO(ioIp, DefualtSlaveID, DoStartAddress, GetDOLength(ioIp));
}
catch (Exception error)
{
LogUtil.error(LOGGER, "连接IO模块[" + ioIp + "]出错:" + error.ToString());
}
}
public override void ReadAllDI(string ioIp, byte slaveId)
{
ReadMultipleDI(ioIp, (byte)slaveId, (ushort)DIStartAddress, 16);
}
public override void ReadAllDO(string ioIp, byte slaveId)
{
ReadMultipleDO(ioIp, (byte)slaveId, (ushort)DoStartAddress, 16);
}
/// <summary>
/// 判断Io模块是否连接
/// </summary>
public static bool IsConnection(string ip)
{
try
{
List<string> list = new List<string>(mastMap.Keys);
foreach (string io in list)
{
if (io.Equals(ip))
{
//判断是否连接,如果没有连接自动重连
MasterTcpClient clinet = mastMap[io];
if (clinet.ISConnection())
{
return true;
}
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦:" + ex.ToString());
}
return false;
}
private static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
List<string> list = new List<string>(mastMap.Keys);
foreach (string io in list)
{
//判断是否连接,如果没有连接自动重连
MasterTcpClient clinet = mastMap[io];
if (clinet.ISConnection())
{
//ReadMultipleDI(io, DefualtSlaveID, DIStartAddress, DefualtLength);
}
else
{
ConnectionIP(io);
LogUtil.error(LOGGER, io + "当前没有连上,重连" + io);
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "出错啦:" + ex.ToString());
}
Thread.Sleep(1);
}
public override void ConnectionIOList(List<string> DIONameList)
{
foreach (string ip in DIONameList)
{
//if (ip.Equals("192.168.10.10"))
//{
// continue;
//}
ConnectionIP(ip);
}
}
public static void ReadMultipleDI(string ioIp, byte slaveId, ushort StartAddress, ushort length)
{
ushort ID = 2;
MasterTcpClient MBmaster = null;
if (mastMap.ContainsKey(ioIp))
{
MBmaster = mastMap[ioIp];
MBmaster.ReadDiscreteInputs(ID, StartAddress, length, slaveId);
}
else
{
LogUtil.error(LOGGER, "ReadSingleDO出错没有连接IO模块:" + ioIp);
}
}
public static void ReadMultipleDO(string ioIp, byte slaveId, ushort StartAddress, ushort length)
{
ushort ID = 1;
MasterTcpClient MBmaster = null;
if (mastMap.ContainsKey(ioIp))
{
MBmaster = mastMap[ioIp];
MBmaster.ReadCoils(ID, StartAddress, length, slaveId);
}
else
{
LogUtil.error(LOGGER, "ReadSingleDO出错没有连接IO模块:" + ioIp);
}
}
//关闭所有的DO
public override void CloseAllDO()
{
foreach (string key in mastMap.Keys)
{
byte[] data = new byte[] { 0, 0 };
WriteMultipleDO(key, DefualtSlaveID, DoStartAddress, GetDOLength(key), data);
}
}
public override void CloseAllConnection()
{
foreach (MasterTcpClient tcp in mastMap.Values)
{
tcp.disconnect();
}
mastMap.Clear();
timer.Stop();
timer = null;
}
public static void CloseAllDO(string ioIp, byte slaveId)
{
ushort length = GetDOLength(ioIp);
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++)
{
bytes[i] = 0;
}
WriteMultipleDO(ioIp, slaveId, DoStartAddress, length, bytes);
}
public static void WriteMultipleDO(string ioIp, byte slaveId, ushort StartAddress, ushort length, byte[] bytes)
{
ushort ID = 6;
MasterTcpClient MBmaster = null;
if (mastMap.ContainsKey(ioIp))
{
MBmaster = mastMap[ioIp];
MBmaster.WriteMultipleCoils(ID, StartAddress, length, bytes, slaveId);
}
else
{
LogUtil.error(LOGGER, "ReadSingleDO出错没有连接IO模块:" + ioIp);
}
}
public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff)
{
ushort ID = 5;
MasterTcpClient MBmaster = null;
if (mastMap.ContainsKey(ioIp))
{
MBmaster = mastMap[ioIp];
MBmaster.WriteSingleCoils(ID, StartAddress, onOff, slaveId);
KNDIO io = new KNDIO(ioIp, slaveId, StartAddress, onOff);
SaveDOValue(io, ioIp);
}
else
{
LogUtil.error(LOGGER, "ReadSingleDO出错没有连接IO模块:" + ioIp);
}
}
public override void WriteSingleDO(string ioIp, byte slaveId, ushort StartAddress, IO_VALUE onOff, int mSeconds)
{
ushort ID = 5;
MasterTcpClient MBmaster = null;
if (mastMap.ContainsKey(ioIp))
{
MBmaster = mastMap[ioIp];
MBmaster.WriteSingleCoils(ID, StartAddress, onOff, slaveId);
KNDIO io = new KNDIO(ioIp, slaveId, StartAddress, onOff);
SaveDOValue(io, ioIp);
//写入之后,等待指定间隔后回写
System.Timers.Timer mytimer = new System.Timers.Timer(mSeconds);
mytimer.Elapsed += (o1, e1) =>
{
try
{
IO_VALUE newValue = IO_VALUE.LOW;
if (onOff.Equals(IO_VALUE.LOW))
{
newValue = IO_VALUE.HIGH;
}
MBmaster.WriteSingleCoils(ID, StartAddress, newValue, slaveId);
KNDIO newIo = new KNDIO(ioIp, slaveId, StartAddress, newValue);
SaveDOValue(newIo, ioIp);
LogUtil.info(LOGGER, "**********定时回写入 IO【" + ioIp + "," + StartAddress + ",值" + onOff + "】:");
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "**********定时回写入 出错:" + ex.StackTrace);
}
};
mytimer.AutoReset = false;//设置是否自动重启,即自动执行多次;
mytimer.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件mytask;
}
else
{
LogUtil.error(LOGGER, "ReadSingleDO出错没有连接IO模块:" + ioIp);
}
}
private static void SaveDOValue(KNDIO io, string ioIp)
{
try
{
lock (DOMapLock)
{
if (!DOValueMap.ContainsKey(ioIp))
{
DOValueMap.Add(ioIp, new List<KNDIO>());
DOValueMap[ioIp].Add(io);
}
else
{
List<KNDIO> ios = DOValueMap[ioIp];
List<KNDIO> list = (from m in ios where m.SlaveId.Equals(io.SlaveId) && m.IOAddress.Equals(io.IOAddress) select m).ToList<KNDIO>();
if (list.Count > 0)
{
DOValueMap[ioIp].Remove(list[0]);
DOValueMap[ioIp].Add(io);
}
else
{
DOValueMap[ioIp].Add(io);
}
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "SaveDOValue出错:" + ex.ToString());
}
}
public override IO_VALUE GetDOValue(string ioIP, byte slaveId, ushort StartAddress)
{
IO_VALUE value = IO_VALUE.LOW;
if (DOValueMap.ContainsKey(ioIP))
{
List<KNDIO> allIo = new List<KNDIO>(DOValueMap[ioIP]);
List<KNDIO> list = (from m in allIo where m.SlaveId.Equals(slaveId) && m.IOAddress.Equals(StartAddress) select m).ToList<KNDIO>();
if (list.Count > 0)
{
value = list[0].IoValue;
}
}
return value;
}
public override IO_VALUE GetDIValue(string ioIP, byte slaveId, ushort StartAddress)
{
IO_VALUE value = IO_VALUE.LOW;
if (DIValueMap.ContainsKey(ioIP))
{
List<KNDIO> allIo = new List<KNDIO>(DIValueMap[ioIP]);
List<KNDIO> list = (from m in allIo where m.SlaveId.Equals(slaveId) && m.IOAddress.Equals(StartAddress) select m).ToList<KNDIO>();
if (list.Count > 0)
{
value = list[0].IoValue;
}
}
return value;
}
public override IO_VALUE GetIOValue(ConfigIO configIO)
{
IO_VALUE value = IO_VALUE.LOW;
try
{
if (configIO.ProType.Equals(ConfigItemType.DI))
{
if (DIValueMap.ContainsKey(configIO.DeviceName))
{
List<KNDIO> allIo = new List<KNDIO>(DIValueMap[configIO.DeviceName]);
List<KNDIO> list = (from m in allIo where m.IOAddress.Equals(configIO.GetIOAddr()) select m).ToList<KNDIO>();
if (list.Count > 0)
{
value = list[0].IoValue;
}
}
}
else if (configIO.ProType.Equals(ConfigItemType.DO))
{
if (DOValueMap.ContainsKey(configIO.DeviceName))
{
List<KNDIO> allIo = new List<KNDIO>(DOValueMap[configIO.DeviceName]);
List<KNDIO> list = (from m in allIo where m.SlaveId.Equals(configIO.SlaveID) && m.IOAddress.Equals(configIO.GetIOAddr()) select m).ToList<KNDIO>();
if (list.Count > 0)
{
value = list[0].IoValue;
}
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "获取数据出错:" + ex.ToString());
}
return value;
}
private static void SaveDIData(string ioIp, ushort ID, byte[] values)
{
try
{
ushort DILength =GetDILength(ioIp);
string finalData = "";
if (values.Length == 2 && DILength.Equals(16))
{
string finalData0 = Convert.ToString(values[0], 2).PadLeft(8, '0');
string finalData1 = Convert.ToString(values[1], 2).PadLeft(8, '0');
finalData = finalData1 + finalData0;
}
else if (values.Length == 1 && DILength <= 8)
{
finalData = Convert.ToString(values[0], 2).PadLeft(DILength, '0');
}
LOGGER.Debug("IO模块【" + ioIp + "】收到DI上传:【" + finalData + "】");
if (finalData.Length >= DILength)
{
List<KNDIO> kndList = new List<KNDIO>();
ushort index = (ushort)(DIStartAddress + DILength - 1);
foreach (char str in finalData)
{
KNDIO io = null;
if (str.Equals('1'))
{
io = new KNDIO(ioIp, (byte)ID, index, IO_VALUE.HIGH);
}
else
{
io = new KNDIO(ioIp, (byte)ID, index, IO_VALUE.LOW);
}
kndList.Add(io);
index--;
}
lock (DIMapLock)
{
if (DIValueMap.ContainsKey(ioIp))
{
DIValueMap.Remove(ioIp);
}
DIValueMap.Add(ioIp, kndList);
}
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "SaveDIData出错:" + ex.ToString());
//LOGGER.Error("处理接受数据出错:", ex);
}
}
private static void SaveDOData(string ioIp, ushort ID, byte[] values)
{
ushort DoLength = GetDOLength(ioIp);
string finalData = "";
if (values.Length == 2 && DoLength.Equals(16))
{
string finalData0 = Convert.ToString(values[0], 2).PadLeft(8, '0');
string finalData1 = Convert.ToString(values[1], 2).PadLeft(8, '0');
finalData = finalData1 + finalData0;
}
else if (values.Length == 1 && DoLength <= 8)
{
finalData = Convert.ToString(values[0], 2).PadLeft(DoLength, '0');
}
if (finalData.Length >= DoLength)
{
List<KNDIO> kndList = new List<KNDIO>();
ushort index = (ushort)(DoStartAddress + DoLength - 1);
foreach (char str in finalData)
{
KNDIO io = null;
if (str.Equals('1'))
{
io = new KNDIO(ioIp, (byte)ID, index, IO_VALUE.HIGH);
}
else
{
io = new KNDIO(ioIp, (byte)ID, index, IO_VALUE.LOW);
}
kndList.Add(io);
index--;
}
lock (DOMapLock)
{
if (DOValueMap.ContainsKey(ioIp))
{
DOValueMap.Remove(ioIp);
}
DOValueMap.Add(ioIp, kndList);
}
}
}
// ------------------------------------------------------------------------
// Event for response data
// ------------------------------------------------------------------------
private static void MBmaster_OnResponseData(string ioIp, ushort ID, byte function, byte[] values, byte[] reviceData)
{
try
{
if (ioIp.IndexOf(":") > 0)
{
ioIp = ioIp.Substring(0, ioIp.IndexOf(":"));
}
string reviceMsg = "";
foreach (byte data in reviceData)
{
reviceMsg = reviceMsg + " " + data;
if (reviceMsg.Length > 200)
{
break;
}
}
if (ID == 0xFF)
{
return;
}
// ------------------------------------------------------------------------
// Identify requested data
ushort Func = ID;
if (Func == 0 && reviceData.Length > 8)
{
Func = reviceData[7];
}
byte SlaveId = 0xFF;
if (reviceData.Length >= 7)
{
SlaveId = reviceData[6];
}
switch (Func)
{
case 1:
LOGGER.Info("Read coils end:【" + reviceMsg + "】 ");
SaveDOData(ioIp, SlaveId, values);
break;
case 2:
//LOGGER.Info("Read discrete inputs end:【" + reviceMsg + "】 ");
SaveDIData(ioIp, SlaveId, values);
break;
case 3:
//LOGGER.Info("读入(多个)寄存器完成 end:【" + reviceMsg + "】 ");
break;
case 4:
//LOGGER.Info("读入(多个)寄存器完成 end:【" + reviceMsg + "】 ");
break;
case 5:
//LOGGER.Info("Write single coil:【" + reviceMsg + "】 ");
break;
}
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "处理接受数据出错:" + ex.ToString());
//LOGGER.Error("处理接受数据出错:", ex);
}
}
// ------------------------------------------------------------------------
// Modbus TCP slave exception
// ------------------------------------------------------------------------
private static void MBmaster_OnException(string ioIp, ushort id, byte function, byte exception, byte[] reviceData)
{
string exc = "Modbus says error: ";
switch (exception)
{
case MasterTcpClient.excIllegalFunction: exc += "Illegal function!"; break;
case MasterTcpClient.excIllegalDataAdr: exc += "Illegal data adress!"; break;
case MasterTcpClient.excIllegalDataVal: exc += "Illegal data value!"; break;
case MasterTcpClient.excSlaveDeviceFailure: exc += "Slave device failure!"; break;
case MasterTcpClient.excAck: exc += "Acknoledge!"; break;
case MasterTcpClient.excSlaveIsBusy: exc += "Slave is busy!"; break;
case MasterTcpClient.excGatePathUnavailable: exc += "Gateway path unavailbale!"; break;
case MasterTcpClient.excExceptionTimeout: exc += "Slave timed out!"; break;
case MasterTcpClient.excExceptionConnectionLost: exc += "Connection is lost!"; break;
case MasterTcpClient.excExceptionNotConnected: exc += "Not connected!"; break;
default:
break;
}
LOGGER.Error("接收数据出错:" + exc);
//MessageBox.Show(exc, "Modbus slave exception");
}
}
public class KNDIO
{
public KNDIO(string ioIp, byte ID, ushort index, IO_VALUE o_VALUE)
{
this.IoIP = ioIp;
this.SlaveId = ID;
this.IOAddress = index;
this.IoValue = o_VALUE;
UpdateTime = DateTime.Now;
}
/// <summary>
/// IO模块IP
/// </summary>
public string IoIP { get; set; }
/// <summary>
/// 地址
/// </summary>
public byte SlaveId { get; set; }
/// <summary>
/// 寄存器地址
/// </summary>
public ushort IOAddress { get; set; }
/// <summary>
/// 值
/// </summary>
public IO_VALUE IoValue { get; set; }
/// <summary>
/// 更新时间
/// </summary>
public DateTime UpdateTime { get; set; }
}
}
using System;
using System.Collections;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
using log4net;
using System.Reflection;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
namespace OnlineStore.DeviceLibrary
{
public class MasterTcpClient
{
public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
// ------------------------------------------------------------------------
// Constants for access
private const byte fctReadCoil = 1;
private const byte fctReadDiscreteInputs = 2;
private const byte fctReadHoldingRegister = 3;
private const byte fctReadInputRegister = 4;
private const byte fctWriteSingleCoil = 5;
private const byte fctWriteSingleRegister = 6;
private const byte fctWriteMultipleCoils = 15;
private const byte fctWriteMultipleRegister = 16;
private const byte fctReadWriteMultipleRegister = 23;
/// <summary>Constant for exception illegal function.</summary>
public const byte excIllegalFunction = 1;
/// <summary>Constant for exception illegal data address.</summary>
public const byte excIllegalDataAdr = 2;
/// <summary>Constant for exception illegal data value.</summary>
public const byte excIllegalDataVal = 3;
/// <summary>Constant for exception slave device failure.</summary>
public const byte excSlaveDeviceFailure = 4;
/// <summary>Constant for exception acknowledge.</summary>
public const byte excAck = 5;
/// <summary>Constant for exception slave is busy/booting up.</summary>
public const byte excSlaveIsBusy = 6;
/// <summary>Constant for exception gate path unavailable.</summary>
public const byte excGatePathUnavailable = 10;
/// <summary>Constant for exception not connected.</summary>
public const byte excExceptionNotConnected = 253;
/// <summary>Constant for exception connection lost.</summary>
public const byte excExceptionConnectionLost = 254;
/// <summary>Constant for exception response timeout.</summary>
public const byte excExceptionTimeout = 255;
/// <summary>Constant for exception wrong offset.</summary>
private const byte excExceptionOffset = 128;
/// <summary>Constant for exception send failt.</summary>
private const byte excSendFailt = 100;
// ------------------------------------------------------------------------
// Private declarations
private static ushort _timeout = 500;
private static ushort _refresh = 10;
private static bool _connected = false;
private static bool _autoConnectOfBreak = false;
private Socket socketClient;
private int M281_A_Len = 0;
private byte[] tcpSocketReviceBuffer = new byte[2048];
//private Socket tcpSynCl;
//private byte[] tcpSynClBuffer = new byte[2048];
// ------------------------------------------------------------------------
/// <summary>Response data event. This event is called when new data arrives</summary>
public delegate void ResponseData(string ip, ushort id, byte function, byte[] data, byte[] reviceData);
/// <summary>Response data event. This event is called when new data arrives</summary>
public event ResponseData OnResponseData;
/// <summary>Exception data event. This event is called when the data is incorrect</summary>
public delegate void ExceptionData(string ip, ushort id, byte function, byte exception, byte[] reviceData);
/// <summary>Exception data event. This event is called when the data is incorrect</summary>
public event ExceptionData OnException;
/// <summary>
/// autoConnectOfBreak
/// </summary>
public bool autoConnectOfBreak
{
get { return _autoConnectOfBreak; }
set { _autoConnectOfBreak = value; }
}
public static ushort timeout
{
get { return _timeout; }
set { _timeout = value; }
}
public ushort refresh
{
get { return _refresh; }
set { _refresh = value; }
}
public bool connected
{
get { return _connected; }
}
public MasterTcpClient()
{
}
private string IP = "";
public int TimeOutTime = 0;
public MasterTcpClient(string ip, ushort port)
{
this.IP = ip;
TimeOutTime = 2000;
connect(ip, port);
}
private System.Timers.Timer reviceTimer = new System.Timers.Timer();
public void connect(string ip, ushort port)
{
try
{
// Connect asynchronous client
socketClient = new Socket(IPAddress.Parse(ip).AddressFamily, SocketType.Stream, ProtocolType.Tcp);
if (TimeOutTime <= 0)
{
socketClient.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _timeout);
socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout);
socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
}
else
{
socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _timeout);
socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _timeout);
socketClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.NoDelay, 1);
IAsyncResult connResult = socketClient.BeginConnect(ip, port, null, null);
connResult.AsyncWaitHandle.WaitOne(this.TimeOutTime, true); //等待2秒
if (!connResult.IsCompleted)
{
LogUtil.info(LOGGER, "Connect to " + ip + ":" + port + " fail!");
return;
}
else
{
_connected = true;
}
}
//Thread threadReceive = new Thread(new ThreadStart(ReceiveHandle));
//threadReceive.Start();
reviceTimer.AutoReset = true;
reviceTimer.Elapsed += reviceTimer_Elapsed;
reviceTimer.Interval = 130;
reviceTimer.Enabled = true;
_connected = true;
}
catch (System.IO.IOException error)
{
LogUtil.info(LOGGER, "Connect to " + ip + ":" + port + " fail!");
_connected = false;
throw (error);
}
}
void reviceTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{
//socketClient.BeginReceive(tcpSocketReviceBuffer, 0, tcpSocketReviceBuffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), socketClient);
ReviceDataProcess();
Thread.Sleep(10);
}
catch (Exception ex)
{
LOGGER.Error("IO模块[" + IP + "]出错:" + ex.ToString());
}
}
private void ReviceDataProcess()
{
int lengthIndex = 5;
byte[] rdata = Receive();
if (rdata != null)
{
string str = "";
foreach (byte by in rdata)
{
str = str + " " + by;
if (str.Length > 200)
{
break;
}
}
//这里rdata就是接收到的数据,
IPEndPoint clientipe = (IPEndPoint)socketClient.RemoteEndPoint;
//ushort id = BitConverter.ToUInt16(rdata, 0);
//byte function = rdata[7];
byte dataLength = rdata[lengthIndex];
int allLength = lengthIndex + 1 + dataLength;
if (rdata.Length > allLength)
{
//LogUtil.info(clientipe.ToString() + "收到数据(需要分包):" + str);
int currStartIndex = 0;
for (int i = 0; i < 100; i++)
{
try
{
if (rdata.Length < currStartIndex + lengthIndex)
{
LogUtil.error(clientipe.ToString() + "收到数据:" + str + "分包出错 [" + currStartIndex + "]");
}
dataLength = rdata[currStartIndex + lengthIndex];
allLength = lengthIndex + 1 + dataLength;
byte[] thisData = new byte[allLength];
Array.Copy(rdata, currStartIndex, thisData, 0, allLength);
ushort id = BitConverter.ToUInt16(thisData, 0);
byte function = thisData[7];
DataProcess(clientipe.ToString(), id, function, thisData);
//剩余的数据处理
if (rdata.Length <= currStartIndex + allLength)
{
break;
}
currStartIndex = currStartIndex + allLength;
}
catch (Exception ex)
{
LogUtil.error(clientipe.ToString() + "收到数据:" + str + "分包出错 [" + currStartIndex + "]:" + ex.ToString());
}
}
}
else
{
LogUtil.debug(LOGGER, clientipe.ToString() + "收到数据(无需分包):" + str);
ushort id = BitConverter.ToUInt16(rdata, 0);
byte function = rdata[7];
DataProcess(clientipe.ToString(), id, function, rdata);
}
}
}
private void DataProcess(string clientIp, ushort id, byte function, byte[] rdata)
{
byte[] data;
if ((function >= fctWriteSingleCoil) && (function != fctReadWriteMultipleRegister))
{
data = new byte[2];
Array.Copy(rdata, 10, data, 0, 2);
}
// ------------------------------------------------------------
// Read response data
else
{
data = new byte[rdata[8]];
Array.Copy(rdata, 9, data, 0, rdata[8]);
}
// ------------------------------------------------------------
// Response data is slave exception
if (function > excExceptionOffset)
{
function -= excExceptionOffset;
CallException(id, function, rdata[8], rdata);
}
// ------------------------------------------------------------
// Response data is regular data
else if (OnResponseData != null)
{ //收到的数据打印出来
OnResponseData(clientIp, id, function, data, rdata);
}
}
private byte[] Receive()
{
try
{
if (socketClient == null || !socketClient.Connected || socketClient.Available < 1)
{
return null;
}
int size = socketClient.Available;
byte[] rData = new byte[size];
socketClient.Receive(rData, size, SocketFlags.None);
return rData;
}
catch (SocketException e)
{
LogUtil.error(LOGGER, "IO模块[" + IP + "]接收数据出现错误:" + e.ToString());
if (socketClient != null)
{
socketClient.Close();
socketClient = null;
}
return null;
}
}
// ------------------------------------------------------------------------
/// <summary>Stop connection to slave.</summary>
public void disconnect()
{
Dispose();
}
// ------------------------------------------------------------------------
/// <summary>Destroy master instance.</summary>
~MasterTcpClient()
{
Dispose();
}
// ------------------------------------------------------------------------
/// <summary>Destroy master instance</summary>
public void Dispose()
{
reviceTimer.Enabled = false;
if (socketClient != null)
{
if (socketClient.Connected)
{
try
{
socketClient.Shutdown(SocketShutdown.Both);
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "IO模块[" + IP + "]dispose出错:" + ex.ToString());
}
socketClient.Close();
}
socketClient = null;
}
}
internal void CallException(ushort id, byte function, byte exception, byte[] rdata)
{
reviceTimer.Enabled = false;
if ((socketClient == null))
{
return;
}
if (exception == excExceptionConnectionLost)
{
//tcpSynCl = null;
socketClient = null;
return;
}
if (OnException != null)
{
OnException(socketClient.RemoteEndPoint.AddressFamily.ToString(), id, function, exception, rdata);
}
}
public void ReadCoils(ushort id, ushort startAddress, ushort numInputs, byte SlaveID)
{
WriteAsyncData(CreateReadHeader(id, startAddress, numInputs, fctReadCoil, SlaveID), id);
}
public void ReadDiscreteInputs(ushort id, ushort startAddress, ushort numInputs, byte SlaveID)
{
WriteAsyncData(CreateReadHeader(id, startAddress, numInputs, fctReadDiscreteInputs, SlaveID), id);
}
public void ReadHoldingRegister(ushort id, ushort startAddress, ushort numInputs, byte SlaveID)
{
WriteAsyncData(CreateReadHeader(id, startAddress, numInputs, fctReadHoldingRegister, SlaveID), id);
}
public void ReadInputRegister(ushort id, ushort startAddress, ushort numInputs, byte SlaveID)
{
WriteAsyncData(CreateReadHeader(id, startAddress, numInputs, fctReadInputRegister, SlaveID), id);
}
public void WriteSingleCoils(ushort id, ushort startAddress, IO_VALUE ioValue, byte SlaveID)
{
byte[] data;
bool OnOff = ioValue.Equals(IO_VALUE.HIGH);
data = CreateWriteHeader(id, startAddress, 1, 1, fctWriteSingleCoil, SlaveID);
if (OnOff == true) data[10] = 255;
else data[10] = 0;
WriteAsyncData(data, id);
}
public void WriteMultipleCoils(ushort id, ushort startAddress, ushort numBits, byte[] values, byte SlaveID)
{
byte numBytes = Convert.ToByte(values.Length);
byte[] data;
data = CreateWriteHeader(id, startAddress, numBits, (byte)(numBytes + 2), fctWriteMultipleCoils, SlaveID);
Array.Copy(values, 0, data, 13, numBytes);
WriteAsyncData(data, id);
}
public void WriteSingleRegister(ushort id, ushort startAddress, byte[] values, byte SlaveID)
{
byte[] data;
data = CreateWriteHeader(id, startAddress, 1, 1, fctWriteSingleRegister, SlaveID);
data[10] = values[0];
data[11] = values[1];
WriteAsyncData(data, id);
}
public void WriteMultipleRegister(ushort id, ushort startAddress, byte[] values, byte SlaveID)
{
ushort numBytes = Convert.ToUInt16(values.Length);
if (numBytes % 2 > 0) numBytes++;
byte[] data;
data = CreateWriteHeader(id, startAddress, Convert.ToUInt16(numBytes / 2), Convert.ToUInt16(numBytes + 2), fctWriteMultipleRegister, SlaveID);
Array.Copy(values, 0, data, 13, values.Length);
WriteAsyncData(data, id);
}
public void ReadWriteMultipleRegister(ushort id, ushort startReadAddress, ushort numInputs, ushort startWriteAddress, byte SlaveID, byte[] values)
{
ushort numBytes = Convert.ToUInt16(values.Length);
if (numBytes % 2 > 0) numBytes++;
byte[] data;
data = CreateReadWriteHeader(id, startReadAddress, numInputs, startWriteAddress, Convert.ToUInt16(numBytes / 2), SlaveID);
Array.Copy(values, 0, data, 17, values.Length);
WriteAsyncData(data, id);
}
// ------------------------------------------------------------------------
// Create modbus header for read action
private byte[] CreateReadHeader(ushort id, ushort startAddress, ushort length, byte function, byte SlaveID)
{
byte[] data = new byte[12];
byte[] _id = BitConverter.GetBytes((short)id);
data[0] = _id[0]; // Slave id high byte
data[1] = _id[1]; // Slave id low byte
data[5] = 6; // Message size
data[6] = SlaveID; // Slave address //必须设置为"1": 2012.04-24 覃发光;
data[7] = function; // Function code
byte[] _adr = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)startAddress));
data[8] = _adr[0]; // Start address
data[9] = _adr[1]; // Start address
byte[] _length = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)length));
data[10] = _length[0]; // Number of data to read
data[11] = _length[1]; // Number of data to read
return data;
}
// ------------------------------------------------------------------------
// Create modbus header for write action
private byte[] CreateWriteHeader(ushort id, ushort startAddress, ushort numData, ushort numBytes, byte function, byte SlaveID)
{
byte[] data = new byte[numBytes + 11];
byte[] _id = BitConverter.GetBytes((short)id);
data[0] = _id[0]; // Slave id high byte
data[1] = _id[1]; // Slave id low byte+
byte[] _size = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)(5 + numBytes)));
data[4] = _size[0]; // Complete message size in bytes
data[5] = _size[1]; // Complete message size in bytes
data[6] = SlaveID; // Slave address //必须设置为"1": 2012.04-24 覃发光;
data[7] = function; // Function code
byte[] _adr = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)startAddress));
data[8] = _adr[0]; // Start address
data[9] = _adr[1]; // Start address
if (function >= fctWriteMultipleCoils)
{
byte[] _cnt = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)numData));
data[10] = _cnt[0]; // Number of bytes
data[11] = _cnt[1]; // Number of bytes
data[12] = (byte)(numBytes - 2);
}
return data;
}
// ------------------------------------------------------------------------
// Create modbus header for write action
private byte[] CreateReadWriteHeader(ushort id, ushort startReadAddress, ushort numRead, ushort startWriteAddress, ushort numWrite, byte SlaveID)
{
byte[] data = new byte[numWrite * 2 + 17];
byte[] _id = BitConverter.GetBytes((short)id);
data[0] = _id[0]; // Slave id high byte
data[1] = _id[1]; // Slave id low byte+
byte[] _size = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)(11 + numWrite * 2)));
data[4] = _size[0]; // Complete message size in bytes
data[5] = _size[1]; // Complete message size in bytes
data[6] = SlaveID; // Slave address //必须设置为"1": 2012.04-24 覃发光;
data[7] = fctReadWriteMultipleRegister; // Function code
byte[] _adr_read = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)startReadAddress));
data[8] = _adr_read[0]; // Start read address
data[9] = _adr_read[1]; // Start read address
byte[] _cnt_read = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)numRead));
data[10] = _cnt_read[0]; // Number of bytes to read
data[11] = _cnt_read[1]; // Number of bytes to read
byte[] _adr_write = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)startWriteAddress));
data[12] = _adr_write[0]; // Start write address
data[13] = _adr_write[1]; // Start write address
byte[] _cnt_write = BitConverter.GetBytes((short)IPAddress.HostToNetworkOrder((short)numWrite));
data[14] = _cnt_write[0]; // Number of bytes to write
data[15] = _cnt_write[1]; // Number of bytes to write
data[16] = (byte)(numWrite * 2);
return data;
}
// ------------------------------------------------------------------------
// Write asynchronous data
private void WriteAsyncData(byte[] write_data, ushort id)
{
if (socketClient == null)
{
LOGGER.Error("发送数据时发现socketClient=null");
return;
}
IPEndPoint clientipe = (IPEndPoint)socketClient.RemoteEndPoint;
if ((socketClient != null) && (socketClient.Connected))
{
try
{
//发送的数据打印出来
string str = "";
foreach (byte by in write_data)
{
str = str + " " + by;
}
//LogUtil.info( clientipe.ToString()+"发送数据:" + str);
socketClient.BeginSend(write_data, 0, write_data.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
//socketClient.BeginReceive(tcpSocketReviceBuffer, 0, tcpSocketReviceBuffer.Length, SocketFlags.None, new AsyncCallback(OnReceive), socketClient);
ReviceDataProcess();
}
catch (SystemException error)
{
LogUtil.error(LOGGER, "IO模块 WriteAsyncData出错:" + error.ToString());
CallException(id, write_data[7], excExceptionConnectionLost, tcpSocketReviceBuffer);
}
}
else CallException(id, write_data[7], excExceptionConnectionLost, tcpSocketReviceBuffer);
}
// ------------------------------------------------------------------------
// Write asynchronous data acknowledge
private void OnSend(System.IAsyncResult result)
{
if (result.IsCompleted == false) CallException(0xFFFF, 0xFF, excSendFailt, tcpSocketReviceBuffer);
}
// ------------------------------------------------------------------------
// Write asynchronous data response
private void OnReceive(System.IAsyncResult result)
{
if (socketClient == null)
{
return;
}
IPEndPoint clientipe = (IPEndPoint)socketClient.RemoteEndPoint;
if (result.IsCompleted == false)
{
CallException(0xFF, 0xFF, excExceptionConnectionLost, tcpSocketReviceBuffer);
}
ushort id = BitConverter.ToUInt16(tcpSocketReviceBuffer, 0); ;
byte function = tcpSocketReviceBuffer[7];
byte[] data;
// ------------------------------------------------------------
// Write response data
if ((function >= fctWriteSingleCoil) && (function != fctReadWriteMultipleRegister))
{
data = new byte[2];
Array.Copy(tcpSocketReviceBuffer, 10, data, 0, 2);
}
// ------------------------------------------------------------
// Read response data
else
{
data = new byte[tcpSocketReviceBuffer[8]];
Array.Copy(tcpSocketReviceBuffer, 9, data, 0, tcpSocketReviceBuffer[8]);
}
// ------------------------------------------------------------
// Response data is slave exception
if (function > excExceptionOffset)
{
function -= excExceptionOffset;
CallException(id, function, tcpSocketReviceBuffer[8], tcpSocketReviceBuffer);
}
// ------------------------------------------------------------
// Response data is regular data
else if (OnResponseData != null)
{ //收到的数据打印出来
string str = "";
foreach (byte by in tcpSocketReviceBuffer)
{
str = str + " " + by;
if (str.Length > 50)
{
break;
}
}
// LogUtil.info( clientipe.ToString()+"收到数据:" + str);
OnResponseData(clientipe.ToString(), id, function, data, tcpSocketReviceBuffer);
}
}
internal bool ISConnection()
{
if (socketClient == null)
{
return false;
}
if (socketClient.Connected == false)
{
return false;
}
return true;
}
}
}
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OnlineStore.DeviceLibrary
{
public class ACCMDManager
{
/// <summary>
/// 0132h 原点接近传感器输入 (HOME) 状态 ReadOnly 0: 输入 OFF 1: 输入 ON
/// </summary>
public static string Home_Single = "0132";
//
//
/// <summary>
/// 0022h 负方向驱动禁止输入(NOT) ReadOnly 0: 输入 OFF、 1: 输入 ON
/// </summary>
public static string Limit_Negative_Single = "0022";
/// <summary>
/// 0023h 正方向驱动禁止输入(POT) ReadOnly 0: 输入 OFF、 1: 输入 ON
/// </summary>
public static string Limit_Positive_Single = "0023";
/// <summary>
///速度 4601
/// </summary>
public static string Speed_Addr = "4601";
/// <summary>
/// 0060h 伺服 On 输入 (SRV-ON) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string ServerOn_Addr = "0060";
/// <summary>
/// 0120h 选通输入 (STB) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string STB_Addr = "0120";
/// <summary>
/// 0124h 减速停止输入 (S-STOP) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string Stop_Addr = "0124";
/// <summary>
/// 0123h 紧急停止输入 (H-STOP) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string SDStop_Addr = "0123";
/// <summary>
/// 0061h 报警清除输入 (A-CLR) 操作 R/W 0000h:输入 OFF、FF00h:输入 ON
/// </summary>
public static string Clear_Alarm_Addr = "0061";
/// <summary>
/// 1020h Save all parameters - 0 - 4294967295 2 R/W 参数写入 EEPROM 写入“6173h”后,实行 EEPROM 写入
/// </summary>
public static string EEPROM_Param_Addr = "1020";
/// <summary>
/// 目标位置=600B
/// </summary>
public static string TargetPostion = "600B";
/// <summary>
/// 实际位置=600F
/// </summary>
public static string ActualPosition = "600F";
/// <summary>
/// BUSY状态=0140,0140h 动作执行状态 (BUSY) ReadOnly 0:未执行 1:执行中
public static string BUSYStatus = "0140";
/// <summary>
/// HOME-CMP=0141,0141h 回原点完成状态 (HOME-CMP) ReadOnly 0:未完成 1:完成
/// </summary>
public static string HOME_CMP_Status = "0141";
/// <summary>
/// 私服Ready状态,00A0h 伺服 Ready 状态 (S-RDY) ReadOnly 0:Ready OFF 1:Ready ON
/// </summary>
public static string ServerOn_Status = "00A0";
/// <summary>
/// 报警状态=00A1,00A1h 报警状态 (ALM) ReadOnly 0:报警未发生 1:报警发生
/// </summary>
public static string Alarm_Status = "00A1";
/// <summary>
/// 指定BlockNo=4414
/// </summary>
public static string BlockNo = "4414";
/// <summary>
/// 读线圈01
/// </summary>
public static byte CMD_ReadCoil= 0x01;
/// <summary>
/// 写线圈 05
/// </summary>
public static byte CMD_WriteCoil = 0x05;
/// <summary>
/// 读寄存器03
/// </summary>
public static byte CMD_ReadRegisters = 0x03;
/// <summary>
/// 写寄存器06
/// </summary>
public static byte CMD_WriteRegisters = 0x06;
/// <summary>
/// 写多个线圈 0f
/// </summary>
public static byte CMD_WriteMCoil = 0x0F;
/// <summary>
/// 写多个寄存器10
/// </summary>
public static byte CMD_WriteMRegisters = 0x10;
/// <summary>
/// block0=原点返回,正方向
/// </summary>
public static string Block_HomeMove0 = "0";
/// <summary>
/// block1=原点返回,反方向
/// </summary>
public static string Block_HomeMove1 = "1";
/// <summary>
/// block2=绝对位置运动
/// </summary>
public static string Block_AbsMove = "2";
/// <summary>
/// block3=相对位置运动
/// </summary>
public static string Block_RelMove = "3";
/// <summary>
/// block4=正方向匀速运动
/// </summary>
public static string Block_VolMove0 = "4";
/// <summary>
/// block5=反方向匀速运动
/// </summary>
public static string Block_VolMove1 = "5";
/// <summary>
/// block6=减速停止
/// </summary>
public static string Block_DelSpeedStop = "6";
/// <summary>
/// block7=紧急停止
/// </summary>
public static string Block_SuddenStop = "7";
public static byte[] buildCheckData(byte[] sendData, int length)
{
ushort pChecksum = 0;
AcSerialBean.CalculateCRC(sendData, length, out pChecksum);
string checkStr = Convert.ToString(pChecksum, 16);
byte[] checkByte = AcSerialBean.StringToByte(checkStr);
if (checkByte.Length == 1)
{
sendData[length] = checkByte[0];
sendData[length + 1] = 0x00;
}
else
{
sendData[length + 1] = checkByte[0];
sendData[length] = checkByte[1];
}
return sendData;
}
public static byte[] GetWriteData(int slvAddr, byte cmd, string addr, string dataValue, int length)
{
// ( 2) 读取寄存器( 03h)
if (cmd.Equals(CMD_ReadRegisters))
{
return Get03WriteData(slvAddr, addr,length);
}
else if (cmd.Equals(CMD_WriteRegisters))
{
// ( 4) 写入寄存器( 06h)
return Get06WriteData(slvAddr, addr, dataValue);
}
else if (cmd.Equals(CMD_WriteCoil))
{
return Get05WriteData(slvAddr, addr, dataValue);
}
else if (cmd.Equals(CMD_ReadCoil))
{
return Get01WriteData(slvAddr, addr, length);
}
return null;
}
private static byte[] Get05WriteData(int slvAddr, string addr, string dataValue)
{
byte[] sendData = new byte[8];
sendData[0] = (byte)slvAddr;
sendData[1] = CMD_WriteCoil;
byte[] addrByte = AcSerialBean.StringToByte(addr.ToString());
if (addrByte.Length == 1)
{
sendData[2] = 0x00;
sendData[3] = addrByte[0];
}
else if (addrByte.Length == 2)
{
sendData[3] = addrByte[1];
sendData[2] = addrByte[0];
}
byte[] dataByte = AcSerialBean.StringToByte(dataValue);
if (dataByte.Length == 1)
{
sendData[4] = 0x00;
sendData[5] = dataByte[0];
}
else if (dataByte.Length == 2)
{
sendData[5] = dataByte[1];
sendData[4] = dataByte[0];
}
sendData = buildCheckData(sendData, sendData.Length - 2);
return sendData;
}
private static byte[] Get01WriteData(int slvAddr, string addr, int length)
{
byte[] sendData = new byte[8];
sendData[0] = (byte)slvAddr;
sendData[1] = CMD_ReadCoil;
byte[] addrByte = AcSerialBean.StringToByte(addr.ToString());
if (addrByte.Length == 1)
{
sendData[2] = 0x00;
sendData[3] = addrByte[0];
}
else if (addrByte.Length == 2)
{
sendData[3] = addrByte[1];
sendData[2] = addrByte[0];
}
byte[] dataByte = AcSerialBean.StringToByte(length.ToString());
if (dataByte.Length == 1)
{
sendData[4] = 0x00;
sendData[5] = dataByte[0];
}
else if (dataByte.Length == 2)
{
sendData[4] = dataByte[1];
sendData[5] = dataByte[0];
}
sendData = buildCheckData(sendData, sendData.Length - 2);
return sendData;
}
private static byte[] Get03WriteData(int slvAddr, string addr, int length)
{
// ( 2) 读取寄存器( 03h)
//发送
//从站地址
//03h
//寄存器起始地址 高位
//低位
//寄存器数(N) 高位
//低位
//CRC 低位
//高位
byte[] sendData = new byte[8 ];
sendData[0] = (byte)slvAddr;
sendData[1] = CMD_ReadRegisters;
byte[] addrByte = AcSerialBean.StringToByte(addr.ToString());
if (addrByte.Length == 1)
{
sendData[2] = 0x00;
sendData[3] = addrByte[0];
}
else if (addrByte.Length == 2)
{
sendData[3] = addrByte[1];
sendData[2] = addrByte[0];
}
byte[] dataByte = AcSerialBean.StringToByte(length.ToString());
if (dataByte.Length == 1)
{
sendData[4] = 0x00;
sendData[5] = dataByte[0];
}
else if (dataByte.Length == 2)
{
sendData[4] = dataByte[1];
sendData[5] = dataByte[0];
}
sendData = buildCheckData(sendData, sendData.Length-2);
return sendData;
}
private static byte[] Get06WriteData(int slvAddr, string addr, string dataValue)
{
// ( 4) 写入寄存器( 06h)
//从站地址
//06h
//地址 高位
//低位
//変更数据 高位
//低位
//CRC 低位
//高位
byte[] sendData = new byte[8];
sendData[0] = (byte)slvAddr;
sendData[1] = CMD_WriteRegisters;
byte[] addrByte = AcSerialBean.StringToByte(addr.ToString());
if (addrByte.Length == 1)
{
sendData[2] = 0x00;
sendData[3] = addrByte[0];
}
else if (addrByte.Length == 2)
{
sendData[3] = addrByte[1];
sendData[2] = addrByte[0];
}
byte[] dataByte = AcSerialBean.StringToByte(dataValue);
if (dataByte.Length == 1)
{
sendData[4] = 0x00;
sendData[5] = dataByte[0];
}
else if (dataByte.Length == 2)
{
sendData[4] = dataByte[0];
sendData[5] = dataByte[1];
//sendData[4] = dataByte[1];
//sendData[5] = dataByte[0];
}
sendData = buildCheckData(sendData, sendData.Length - 2);
return sendData;
}
}
}
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 外部调用的接口放在此文件中
/// </summary>
public partial class ACServerManager
{
public static bool IsShowMsg = false ;
private static int SleepMSendons = 20;
private static int ReviceOutTimeMS = 100;
private static Dictionary<string, Dictionary<string, int>> ComAddrValue = new Dictionary<string, Dictionary<string, int>>();
private static string mapObj = "";
private static int GetAddrValue(string portName, int slvAddr, string addr)
{
int value = -1;
try
{
string name = portName + "_" + slvAddr;
Dictionary<string, int> map = null;
ComAddrValue.TryGetValue(name, out map);
if (map == null)
{
return -1;
}
if (map.ContainsKey(addr))
{
return map[addr];
}
}
catch (Exception ex)
{
LogUtil.error("UpdateAddrValue出错:" + ex.ToString());
}
return value;
}
private static void UpdateAddrValue(string portName, int slvAddr, string addr, int value)
{
try
{
lock (mapObj)
{
string name = portName + "_" + slvAddr;
Dictionary<string, int> map = null;
ComAddrValue.TryGetValue(name, out map);
if (map == null)
{
map = new Dictionary<string, int>();
ComAddrValue.Add(name, map);
}
if (map.ContainsKey(addr))
{
ComAddrValue[name][addr] = value;
}
else
{
ComAddrValue[name].Add(addr, value);
}
}
}
catch (Exception ex)
{
LogUtil.error("UpdateAddrValue出错:" + ex.ToString());
}
}
public static bool OpenPort(string portName)
{
if (serialBeanMap.ContainsKey(portName))
{
return true;
}
int ACBaudRate = ConfigAppSettings.GetIntValue(Setting_Init.ACBaudRate);
if (ACBaudRate <= 0)
{
ACBaudRate = 115200;
}
AcSerialBean bean = new AcSerialBean(portName, ACBaudRate, Parity.Even, 8, StopBits.One);
bool result = bean.openPort();
if (!result)
{
LogUtil.info("打开串口【" + portName + "】【" + ACBaudRate + "】失败");
return false;
}
LogUtil.info("打开串口【" + portName + "】【" + ACBaudRate + "】成功");
//bean.DataReceived += DataReceived;
if (serialBeanMap.ContainsKey(portName))
{
serialBeanMap.Remove(portName);
}
serialBeanMap.Add(portName, bean);
return true;
}
public static void ColsePort(string portName)
{
AcSerialBean bean = GetSerialBean(portName);
if (bean == null)
{
LogUtil.info("串口【" + portName + "】未打开,不需要关闭");
return;
}
//清理缓存
bean.clearInBuffer();
bean.clearOutBuffer();
bean.closePort();
if (serialBeanMap.ContainsKey(portName))
{
serialBeanMap.Remove(portName);
}
LogUtil.info("ACServerManager 关闭串口【" + portName + "】 ");
}
public static void CloseAllPort()
{
List<string> kes = new List<string>(serialBeanMap.Keys);
foreach (string key in kes)
{
ColsePort(key);
}
}
/// <summary>
/// 是否成功打开伺服
/// </summary>
public static bool ServerOnStatus(string portName, int slvAddr)
{
if (!serialBeanMap.ContainsKey(portName))
{
return false ;
}
PreReadCoilAddr = ACCMDManager.ServerOn_Addr;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_ReadCoil, PreReadCoilAddr, "0000", 1);
//SendData(portName,dataArray);
byte[] reviceData = SendCommand(portName, dataArray, ReviceOutTimeMS, 6);
int result= GetCoilData(portName, reviceData, PreReadCoilAddr);
//TODO
return result>=1;
}
/// <summary>
/// 是否原点返回完成
/// </summary>
public static bool IsHomeMoveEnd(string portName, int slvAddr)
{
int status = GetHomeEndStatus(portName, slvAddr);
if (status >= 1)
{
return true;
} return false;
}
public static void InitSlvAddr(string portName, int slvAddr)
{
InitSlvAddr(portName, slvAddr, 100, 30, 30 );
//InitSlvAddr(portName, slvAddr, 100, 30, 30, 100, 50, 30);
}
//把数字转换为四位的字符串
public static string SpeedToStr(int speed,int length)
{
string str = String.Format("{0:X}", speed);
return str.PadLeft(length, '0');
}
public static void InitSlvAddr(string portName, int slvAddr,int targetSpeed,int addSpeed,int delSpeed )
//public static void InitSlvAddr(string portName, int slvAddr,int targetSpeed,int addSpeed,int delSpeed,int homeHighSpeed,int homeLowSpeed,int homeAddSpeed)
{
LogUtil.info("开始初始化伺服【" + portName + "】【" + slvAddr + "】");
//写入block
int sleep = 20;
string slvAddrStr = string.Format(strFromat, slvAddr);
//block0=原点返回,正方向
string str0 = slvAddrStr + "104800 000408 00000420 00000000 ffff";
SendStrAndSleep(portName, str0, sleep);
//block1=原点返回,反方向
string str1 = slvAddrStr + "104804 000408 04000420 00000000 ffff";
SendStrAndSleep(portName, str1, sleep);
//block2=绝对位置运动
string str2 = slvAddrStr + "104808 000408 10000211EC78FFFF ffff";
SendStrAndSleep(portName, str2, sleep);
//block3=相对位置运动
string str3 = slvAddrStr + "10480C 000408 10000111EC78FFFF ffff";
SendStrAndSleep(portName, str3, sleep);
//block4=正方向匀速运动
string str4 = slvAddrStr + "104810 000408 10000311 00000000 ffff";
SendStrAndSleep(portName, str4, sleep);
//block5=反方向匀速运动
string str5 = slvAddrStr + "104814 000408 14000311 00000000 ffff";
SendStrAndSleep(portName, str5, sleep);
//block6=减速停止
string str6 = slvAddrStr + "104818 000408 00000500 00000000 ffff";
SendStrAndSleep(portName, str6, sleep);
//block7=紧急停止
string str7 = slvAddrStr + "10481c 000408 00000510 00000000 ffff";
SendStrAndSleep(portName, str7, sleep);
//0106460001009D12
//速度 V1 =100
string v1 = slvAddrStr + "064601 " + SpeedToStr(targetSpeed, 4) + " ffff";
SendStrAndSleep(portName, v1, sleep);
//加速度 A1 =30
string a1 = slvAddrStr + "064611 " + SpeedToStr(addSpeed, 4) + " ffff";
SendStrAndSleep(portName, a1, sleep);
//减速度 D1=30
string d1 = slvAddrStr + "064621 " + SpeedToStr(delSpeed, 4) + " ffff";
SendStrAndSleep(portName, d1, sleep);
////原点返回高速 homeHighSpeed=100
//SendStrAndSleep(portName, slvAddrStr + "06 4637 " + SpeedToStr(homeHighSpeed, 4) + " ffff", sleep);
////原点返回抵速 homeLowSpeed=50
//SendStrAndSleep(portName, slvAddrStr + "06 4638 " + SpeedToStr(homeLowSpeed, 4) + " ffff", sleep);
////原点返回加速度=30
//SendStrAndSleep(portName, slvAddrStr + "06 4639 " + SpeedToStr(homeAddSpeed, 4) + " ffff", sleep);
//UpdateEEPROM(portName, slvAddr);
}
public static void UpdateEEPROM(string portName, int slvAddr)
{
string addr = ACCMDManager.EEPROM_Param_Addr;
string data = "6173";
int length = 2;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteRegisters, addr, data, length);
SendData(portName, dataArray, 5);
}
public static void ServoOn(string portName, int slvAddr)
{
string addr = ACCMDManager.ServerOn_Addr;
string data = "FF00";
//byte cmd = 0x05;
int length = 2;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
}
public static void ServoOff(string portName, int slvAddr)
{
string addr = ACCMDManager.ServerOn_Addr;
string data = "0000";
//byte cmd = 0x05;
int length = 2;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
}
public static void RelMove(string portName, int slvAddr, int position,int targetSpeed)
{
//先更新速度
string slvAddrStr = string.Format(strFromat, slvAddr);
string v1 = slvAddrStr + "064601 " + SpeedToStr(targetSpeed, 4) + " ffff";
SendStrAndSleep(portName, v1,SleepMSendons);
RelMove(portName, slvAddr, position);
}
public static void RelMove(string portName, int slvAddr, int position)
{
//int position = Convert.ToInt32(txtPosition.Text);
byte[] positionData = AcSerialBean.StringToByte(position.ToString("X8"));
byte[] data = AcSerialBean.StringToByte("0110480C 000408 10000111EC78FFFF ffff");
data[0] = (byte)slvAddr;
data[data.Length - 1] = 0x00;
data[data.Length - 2] = 0x00;
if (positionData.Length == 4)
{
data[11] = positionData[2];
data[12] = positionData[3];
data[13] = positionData[0];
data[14] = positionData[1];
}
else if (positionData.Length == 3)
{
data[11] = positionData[1];
data[12] = positionData[2];
data[13] = 0x00;
data[14] = positionData[0];
}
else if (positionData.Length == 2)
{
data[11] = positionData[0];
data[12] = positionData[1];
data[13] = 0x00;
data[14] = 0x00;
}
else if (positionData.Length == 1)
{
data[11] = 0x00;
data[12] = positionData[0];
data[13] = 0x00;
data[14] = 0x00;
}
data = ACCMDManager.buildCheckData(data, data.Length - 2);
SendData(portName, data);
System.Threading.Thread.Sleep(SleepMSendons);
UpdateBlock(portName, slvAddr, ACCMDManager.Block_RelMove);
OpenAndCloseSTB(portName, slvAddr);
}
public static void HomeMove(string portName, int slvAddr, int speed)
{
try
{
//先判断是否在原点,如果已经在原点,先向正方向走2000
int homeSingle = GetHomeSingle(portName, slvAddr);
if (homeSingle.Equals(1))
{
int isHomeEnd = GetHomeEndStatus(portName, slvAddr);
if (isHomeEnd.Equals(1))
{
int value = 2000;
if ( StoreManager.Config.Batch_Axis.IsSameAxis(portName,slvAddr))
{
value = 40000;
}
LogUtil.info("轴【" + portName +"_"+slvAddr +"】原点返回时发现原点已亮且回过原点,需要先相对走" + value);
RelMove(portName, slvAddr, value);
bool isStop = false;
for (int i = 0; i <= 10; i++)
{
Thread.Sleep(200);
if (GetBusyStatus(portName, slvAddr).Equals(0))
{
isStop = true;
LogUtil.info("轴【" + portName + "_" + slvAddr + "】 相对走" + value + "已结束");
break;
}
}
if (!isStop)
{
LogUtil.info("轴【" + portName + "_" + slvAddr + "】 相对走" + value + "已等待2分钟,直接停止");
SuddenStop(portName, slvAddr);
}
}
else
{
int volSpeed =Math.Abs( speed / 5);
int time = 1500;
LogUtil.info("轴【" + portName + "_" + slvAddr + "】原点返回时发现原点已亮但未回过原点,先匀速向上走"+time+",速度["+ volSpeed + "]" );
//需要匀速向上走
ACServerManager.SpeedMove(portName, slvAddr, volSpeed);
Thread.Sleep(time);
LogUtil.info("轴【" + portName + "_" + slvAddr + "】匀速 已等待" + time + ",直接停止");
SuddenStop(portName, slvAddr);
Thread.Sleep(SleepMSendons);
}
}
}catch(Exception ex)
{
LogUtil.error("轴【" + portName + "_" + slvAddr + "】原点返回前验证是否在原点出错:" + ex.StackTrace);
}
//默认负方向原点返回
UpdateBlock(portName, slvAddr, ACCMDManager.Block_HomeMove1);
Thread.Sleep(SleepMSendons);
OpenAndCloseSTB(portName, slvAddr);
}
public static void SetSpeed(string portName, int slvAddr, int speed)
{
int preSpeed = GetAddrValue(portName,slvAddr, ACCMDManager.Speed_Addr);
if ((preSpeed.Equals(-1)) || (!preSpeed.Equals(Math.Abs(speed))))
{
string v1 = slvAddr + "064601 " + ACServerManager.SpeedToStr(speed, 4) + " ffff";
LogUtil.debug("轴【" + portName + "_" + slvAddr + "】更新速度为【" + speed + "】,发送数据【" + v1 + "】");
SendStrAndSleep(portName, v1, SleepMSendons);
UpdateAddrValue(portName,slvAddr, ACCMDManager.Speed_Addr, Math.Abs(speed));
Thread.Sleep(SleepMSendons);
}
}
public static void SpeedMove(string portName, int slvAddr, int speed)
{
SetSpeed(portName, slvAddr, speed);
if (speed > 0)
{
UpdateBlock(portName, slvAddr, ACCMDManager.Block_VolMove0);
}
else
{
UpdateBlock(portName, slvAddr, ACCMDManager.Block_VolMove1);
}
//Thread.Sleep(SleepMSendons);
OpenAndCloseSTB(portName, slvAddr);
}
public static bool isInPosition(ConfigMoveAxis Axis, int PPosition)
{
int outCount = ACServerManager.GetActualtPosition(Axis.DeviceName, Axis.GetAxisValue());
int errorCount = Math.Abs(outCount - PPosition);
if (errorCount <= Axis.CanErrorCountMin)
{
return true;
}
else
{
return false;
}
}
public static void AbsMove(string portName, int slvAddr, int targetPosition, int targetSpeed)
{
//先更新速度
string slvAddrStr = string.Format(strFromat, slvAddr);
int preSpeed = GetAddrValue(portName,slvAddr, ACCMDManager.Speed_Addr);
if ((preSpeed.Equals(-1)) || (!preSpeed.Equals(targetSpeed)))
{
//速度 V1 =100
string v1 = slvAddrStr + "064601 " + SpeedToStr(targetSpeed, 4) + " ffff";
SendStrAndSleep(portName, v1, SleepMSendons);
UpdateAddrValue(portName,slvAddr, ACCMDManager.Speed_Addr, targetSpeed);
}
//绝对运动
AbsMove(portName, slvAddr, targetPosition);
}
public static void AbsMove(string portName, int slvAddr, int position)
{
//int position = Convert.ToInt32(txtPosition.Text, 10);
byte[] positionData = AcSerialBean.StringToByte(position.ToString("X8"));
byte[] data = AcSerialBean.StringToByte("01104808 000408 10000211 EC78FFFF ffff");
data[0] = (byte)slvAddr;
data[data.Length - 1] = 0x00;
data[data.Length - 2] = 0x00;
if (positionData.Length == 4)
{
data[11] = positionData[2];
data[12] = positionData[3];
data[13] = positionData[0];
data[14] = positionData[1];
}
else if (positionData.Length == 3)
{
data[11] = positionData[1];
data[12] = positionData[2];
data[13] = 0x00;
data[14] = positionData[0];
}
else if (positionData.Length == 2)
{
data[11] = positionData[0];
data[12] = positionData[1];
data[13] = 0x00;
data[14] = 0x00;
}
else if (positionData.Length == 1)
{
data[11] = 0x00;
data[12] = positionData[0];
data[13] = 0x00;
data[14] = 0x00;
}
data = ACCMDManager.buildCheckData(data, data.Length - 2);
SendData(portName, data);
Thread.Sleep(SleepMSendons);
UpdateBlock(portName, slvAddr, ACCMDManager.Block_AbsMove);
//data = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteRegisters, ACCMDManager.BlockNo, ACCMDManager.Block_AbsMove, 2);
//SendData(portName, data);
//Thread.Sleep(SleepMSendons);
OpenAndCloseSTB(portName, slvAddr);
}
}
}
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
namespace OnlineStore.DeviceLibrary
{
partial class ACServerManager
{
private static Dictionary<int, Dictionary<string, RegisterInfo>> LastDataMap = new Dictionary<int, Dictionary<string, RegisterInfo>>();
//private static SerialBean bean = null;
private static string strFromat = "{0:X2}";
private static Dictionary<string, AcSerialBean> serialBeanMap = new Dictionary<string, AcSerialBean>();
private static AcSerialBean GetSerialBean(string portName)
{
if (serialBeanMap.ContainsKey(portName))
{
return serialBeanMap[portName];
}
return null;
}
public static void SendStrAndSleep(string portName, string str, int sleepS)
{
byte[] data = AcSerialBean.StringToByte(str);
data[data.Length - 1] = 0x00;
data[data.Length - 2] = 0x00;
data = ACCMDManager.buildCheckData(data, data.Length - 2);
SendData(portName, data);
System.Threading.Thread.Sleep(sleepS);
}
public static void SaveData(string portName, byte slvAddr, string regAddr, int value)
{
if (LastDataMap.ContainsKey(slvAddr))
{
if (LastDataMap[slvAddr].ContainsKey(regAddr))
{
LastDataMap[slvAddr][regAddr] = new RegisterInfo(slvAddr, regAddr, value);
}
else
{
LastDataMap[slvAddr].Add(regAddr, new RegisterInfo(slvAddr, regAddr, value));
}
}
else
{
Dictionary<string, RegisterInfo> map = new Dictionary<string, RegisterInfo>();
map.Add(regAddr, new RegisterInfo(slvAddr, regAddr, value));
LastDataMap.Add(slvAddr, map);
}
}
public static RegisterInfo GetData(string portName, byte slvAddr, string regAddr)
{
if (LastDataMap.ContainsKey(slvAddr))
{
if (LastDataMap[slvAddr].ContainsKey(regAddr))
{
return LastDataMap[slvAddr][regAddr];
}
}
return null;
}
public static void SendData(string portName, byte[] data, int reviceLength)
{
if (data == null)
{
return;
}
byte[] returnData = SendCommand(portName, data, ReviceOutTimeMS, reviceLength);
string strSend = "";
for (int i = 0; i < returnData.Length; i++)
{
strSend += string.Format("{0:X2} ", returnData[i]);
}
if (IsShowMsg)
{
LogUtil.info( "串口" + portName + " 收到数据:" + strSend + "");
}
}
public static void SendData(string portName, byte[] data)
{
if (data == null)
{
return;
}
byte[] returnData = SendCommand(portName, data, ReviceOutTimeMS, 8);
if (returnData != null)
{
string strSend = "";
for (int i = 0; i < returnData.Length; i++)
{
strSend += string.Format("{0:X2} ", returnData[i]);
}
if (IsShowMsg)
{
LogUtil.info("串口" + portName + " 收到数据:" + strSend + "");
}
}
}
/// <summary>
/// 发送数据并获取返回值
/// </summary>
/// <param name="data">发送的数据</param>
/// <param name="outTime">超时时间</param>
/// <returns>返回值的长度</returns>
public static byte[] SendCommand(string portName, byte[] data, int outTime, int reviceLength)
{
if (outTime <100)
{
outTime = 100;
}
byte[] returnData = null;
try
{
if (data == null)
{
return returnData;
}
string strSend = "";
for (int i = 0; i < data.Length; i++)
{
strSend += string.Format("{0:X2} ", data[i]);
}
if (strSend.Equals(""))
{
return returnData;
}
if (IsShowMsg)
{
LogUtil.info( "串口" + portName + " 写入数据:" + strSend + "");
}
AcSerialBean bean = GetSerialBean(portName);
if (bean == null)
{
LogUtil.debug("ACServerManager SendCommand 试图向未打开串口【" + portName + "】写入数据:" + strSend + "。");
}
else
{
int ret= bean.SendCommand(data, ref returnData, outTime, reviceLength);
if (!ret.Equals(reviceLength))
{
LogUtil.error("串口" + portName + " 写入数据:" + strSend + ",预计返回字节数【"+reviceLength+"】实际返回【"+ret+"】");
}
System.Threading.Thread.Sleep(2);
}
}
catch (Exception ex)
{
LogUtil.info(ex.ToString());
}
return returnData;
}
public static void WriteData(string portName, int slvAddr, string addr, string data, byte cmd, int length)
{
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, cmd, addr, data, length);
SendData(portName, dataArray);
}
public static void SendStr(string portName, int slvAddr, string str)
{
//string str = txtSendStr.Text;
byte[] data = AcSerialBean.StringToByte(str);
data[data.Length - 1] = 0x00;
data[data.Length - 2] = 0x00;
data = ACCMDManager.buildCheckData(data, data.Length - 2);
SendData(portName, data);
}
public static void Stop(string portName, int slvAddr)
{
//RunBlock(6);
string addr = ACCMDManager.Stop_Addr;
string data = "FF00";
//byte cmd = 0x05;
int length = 2;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
System.Threading.Thread.Sleep(1000);
data = "0000";
dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
}
public static void SuddenStop(ConfigMoveAxis axis)
{
if (GetBusyStatus(axis.DeviceName, axis.GetAxisValue()).Equals(1))
{
SuddenStop(axis.DeviceName, axis.GetAxisValue());
}
}
public static void SuddenStop(string portName, int slvAddr)
{
// RunBlock(7);
string addr = ACCMDManager.SDStop_Addr;
string data = "FF00";
//byte cmd = 0x05;
int length = 2;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
System.Threading.Thread.Sleep(500);
data = "0000";
dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
}
public static void OpenAndCloseSTB(string portName, int slvAddr)
{
OnlyOpenSTB(portName, slvAddr);
System.Threading.Thread.Sleep(200);
CloseSTB(portName, slvAddr);
System.Threading.Thread.Sleep(SleepMSendons);
}
public static void OnlyOpenSTB(string portName, int slvAddr)
{
string addr = ACCMDManager.STB_Addr;
string data = "FF00";
int length = 2;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
}
public static void CloseSTB(string portName, int slvAddr)
{
string addr = ACCMDManager.STB_Addr;
string data = "0000";
int length = 2;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
}
public static void UpdateBlock(string portName, int slvAddr, string blockNum)
{
int preNum = GetAddrValue(portName,slvAddr, ACCMDManager.BlockNo);
if (preNum.Equals(-1) || (!preNum.ToString().Equals(blockNum)))
{
byte[] data = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteRegisters, ACCMDManager.BlockNo, blockNum , 2);
SendData(portName, data);
int value = Convert.ToInt16(blockNum);
UpdateAddrValue(portName,slvAddr, ACCMDManager.BlockNo, value);
Thread.Sleep(SleepMSendons);
}
}
public static void AlarmClear(string portName, int slvAddr)
{
string addr = ACCMDManager.Clear_Alarm_Addr;
string data = "FF00";
//byte cmd = 0x05;
int length = 2;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
System.Threading.Thread.Sleep(200);
data = "0000";
dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
}
public static void CloseAlarmClear(string portName, int slvAddr)
{
string addr = ACCMDManager.Clear_Alarm_Addr;
string data = "0000";
//byte cmd = 0x05;
int length = 2;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_WriteCoil, addr, data, length);
SendData(portName, dataArray);
}
private static string PreReadAddr = "";
private static string PreReadCoilAddr = "";
public static int GetRegisterData(string portName, byte[] reviceData, string Address)
{
if (reviceData != null && reviceData.Length > 0)
{
string strSend = "";
for (int i = 0; i < reviceData.Length; i++)
{
strSend += string.Format("{0:X2} ", reviceData[i]);
}
if (IsShowMsg)
{
LogUtil.info( "串口" + portName + " 收到数据:" + strSend + "");
}
string strFromat = "{0:X2}";
byte slvAddrR = reviceData[0];
int lenth = reviceData[2];
byte cmd = reviceData[1];
if (cmd.Equals(ACCMDManager.CMD_ReadRegisters) && reviceData.Length > 6 && lenth == 4)
{
string str = string.Format(strFromat, reviceData[5]) + string.Format(strFromat, reviceData[6]) + string.Format(strFromat, reviceData[3]) + string.Format(strFromat, reviceData[4]);
int value = Convert.ToInt32(str.Trim().Replace(" ", ""), 16);
SaveData(portName, slvAddrR, Address, value);
return value;
}
}
return -1;
}
public static int GetTargetPosition(string portName, int slvAddr)
{
PreReadAddr = ACCMDManager.TargetPostion;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_ReadRegisters, ACCMDManager.TargetPostion, "0000", 2);
// SendData(portName,dataArray);
byte[] reviceData = SendCommand(portName, dataArray, ReviceOutTimeMS, 9);
return GetRegisterData(portName, reviceData, ACCMDManager.TargetPostion);
}
public static int GetActualtPosition(ConfigMoveAxis axis)
{
return GetActualtPosition(axis.DeviceName, axis.GetAxisValue());
}
public static int GetActualtPosition(string portName, int slvAddr)
{
PreReadAddr = ACCMDManager.ActualPosition;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_ReadRegisters, ACCMDManager.ActualPosition, "0000", 2);
// SendData(portName,dataArray);
byte[] reviceData = SendCommand(portName, dataArray, ReviceOutTimeMS, 9);
int result = -1;
for (int i = 1; i <= 3; i++)
{
result = GetRegisterData(portName, reviceData, ACCMDManager.ActualPosition);
if (!result.Equals(-1))
{
break;
}
else
{
LogUtil.error("第" + i + "次获取" + portName + "_" + slvAddr + "的实时位置" + result);
}
}
return result;
}
public static int GetCoilData(string portName, byte[] reviceData, string Address)
{
if (reviceData != null && reviceData.Length > 0)
{
string strSend = "";
for (int i = 0; i < reviceData.Length; i++)
{
strSend += string.Format("{0:X2} ", reviceData[i]);
}
if (IsShowMsg)
{
LogUtil.info( "串口" + portName + " 收到数据:" + strSend + "");
}
string strFromat = "{0:X2}";
byte slvAddrR = reviceData[0];
int lenth = reviceData[2];
byte cmd = reviceData[1];
if (cmd.Equals(ACCMDManager.CMD_ReadCoil) && reviceData.Length >= 6 && lenth == 1)
{
string str = string.Format(strFromat, reviceData[3]);
int value = Convert.ToInt32(str.Trim().Replace(" ", ""), 16);
SaveData(portName, slvAddrR, Address, value);
return value;
}
}
return -1;
}
public static int GetAlarmStatus(string portName, int slvAddr)
{
PreReadCoilAddr = ACCMDManager.Alarm_Status;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_ReadCoil, PreReadCoilAddr, "0000", 1);
//SendData(portName,dataArray);
byte[] reviceData = SendCommand(portName, dataArray, ReviceOutTimeMS, 6);
return GetCoilData(portName, reviceData, PreReadCoilAddr);
}
public static int GetBusyStatus(string portName, int slvAddr)
{
PreReadCoilAddr = ACCMDManager.BUSYStatus;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_ReadCoil, ACCMDManager.BUSYStatus, "0000", 1);
//SendData(portName,dataArray);
byte[] reviceData = SendCommand(portName, dataArray, ReviceOutTimeMS, 6);
return GetCoilData(portName, reviceData, ACCMDManager.BUSYStatus);
}
public static int GetHomeEndStatus(string portName, int slvAddr)
{
PreReadCoilAddr = ACCMDManager.HOME_CMP_Status;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_ReadCoil, ACCMDManager.HOME_CMP_Status, "0000", 1);
//SendData(portName,dataArray);
byte[] reviceData = SendCommand(portName, dataArray, ReviceOutTimeMS, 6);
return GetCoilData(portName, reviceData, ACCMDManager.HOME_CMP_Status);
}
public static int GetHomeSingle(string portName, int slvAddr)
{
PreReadCoilAddr = ACCMDManager.Home_Single;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_ReadCoil, ACCMDManager.Home_Single, "0000", 1);
byte[] reviceData = SendCommand(portName, dataArray, ReviceOutTimeMS, 6);
return GetCoilData(portName, reviceData, ACCMDManager.Home_Single);
}
/// <summary>
/// 负极限
/// </summary>
public static int GetLimitNegativeSingle(string portName, int slvAddr)
{
PreReadCoilAddr = ACCMDManager.Home_Single;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_ReadCoil, ACCMDManager.Limit_Negative_Single, "0000", 1);
byte[] reviceData = SendCommand(portName, dataArray, ReviceOutTimeMS, 6);
return GetCoilData(portName, reviceData, ACCMDManager.Home_Single);
}
/// <summary>
/// 负极限
/// </summary>
public static int GetLimitNegativeSingle(ConfigMoveAxis axis)
{
return GetLimitNegativeSingle(axis.DeviceName, axis.GetAxisValue());
}
/// <summary>
/// 正极限
/// </summary>
public static int GetLimitPositiveSingle(ConfigMoveAxis axis)
{
return GetLimitPositiveSingle(axis.DeviceName, axis.GetAxisValue());
}
/// <summary>
/// 正极限
/// </summary>
public static int GetLimitPositiveSingle(string portName, int slvAddr)
{
PreReadCoilAddr = ACCMDManager.Home_Single;
byte[] dataArray = ACCMDManager.GetWriteData(slvAddr, ACCMDManager.CMD_ReadCoil, ACCMDManager.Limit_Positive_Single, "0000", 1);
byte[] reviceData = SendCommand(portName, dataArray, ReviceOutTimeMS, 6);
return GetCoilData(portName, reviceData, ACCMDManager.Home_Single);
}
}
/// <summary>
/// 记录最后一次获得的寄存器的值
/// </summary>
public class RegisterInfo
{
public RegisterInfo(int slv, string addr, int data)
{
this.SlvAddr = slv;
this.RegisterAddr = addr;
this.LastData = data;
this.LastTime = new DateTime();
}
public int SlvAddr { get; set; }
public string RegisterAddr { get; set; }
public int LastData { get; set; }
public DateTime LastTime { get; set; }
}
}
using DeviceLib;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 批量上下料 轴,检测料盘检测信号处理
/// </summary>
public class BatchAxisController
{
private static bool IsStop = false;
private static System.Timers.Timer checkTimer = null;
private static string TargetIoType = IO_Type.TrayCheck_LoadMaterial;
private static IO_VALUE TargetIoValue = IO_VALUE.HIGH;
public static bool StartCheck(string targetIo, IO_VALUE value = IO_VALUE.HIGH)
{
if (checkTimer == null)
{
checkTimer = new System.Timers.Timer();
checkTimer.AutoReset = true;
checkTimer.Interval += 30;
checkTimer.Elapsed += CheckTimer_Elapsed;
checkTimer.Enabled = false;
}
TargetIoValue = value;
TargetIoType = targetIo;
checkTimer.Start();
return true;
}
public static bool StopCheck()
{
if (!(checkTimer == null))
{
checkTimer.Stop();
}
return true;
}
private static bool IsInProcess = false;
private static DateTime preTime = DateTime.Now;
private static void CheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (IsInProcess) { return; }
IsInProcess = true;
bool result = false;
if (IOManager.IOValue(TargetIoType).Equals(TargetIoValue))
{
LogUtil.info("批量上料轴,检测到 " + TargetIoType + "="+TargetIoValue+",可以停止运动");
result = true;
}
//else if (IOManager.IOValue(IO_Type.BatchAxis_Limit).Equals(IO_VALUE.HIGH))
//{
// LogUtil.info("批量上料轴,检测到正极限信号,可以停止运动");
// result = true;
//}
if (result)
{
//AutoAxisIsMove = 0;
LogUtil.debug("批量上料轴, 停止运动");
ACServerManager.SuddenStop(StoreManager.Config.Batch_Axis.DeviceName, StoreManager.Config.Batch_Axis.GetAxisValue());
StopCheck();
}
preTime = DateTime.Now;
IsInProcess = false;
}
}
}
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 压紧机构计量检测信号处理
/// </summary>
public class MeteringSignal
{
public static int ComAxisChangeValue = ConfigAppSettings.GetIntValue(Setting_Init.ComAxisChangeValue);
/// <summary>
/// 信号变化次数
/// </summary>
public static int SignalChangeCount=0;
public static int TargetChangeCount = 0;
public static IO_VALUE PreSignalValue = IO_VALUE.LOW;
private static System.Timers.Timer timer = null;
public static void StartCheck(int targetPositon, int currPosition)
{
int chazhi = Math.Abs(targetPositon - currPosition);
int count = chazhi / ComAxisChangeValue;
if (count > 0 && chazhi > ComAxisChangeValue)
{
StartCheck(count);
}
}
private static void StartCheck(int targetCount)
{
TargetChangeCount = targetCount;
if (timer == null)
{
timer = new System.Timers.Timer();
timer.Interval = 20;
timer.AutoReset = true;
timer.Elapsed += Timer_Elapsed;
timer.Enabled = false;
}
LogUtil.debug("开始压紧轴计量检测,预计变化" + TargetChangeCount + "次");
IsInProcess = false;
PreSignalValue = IO_VALUE.LOW;
SignalChangeCount = 0;
timer.Start();
}
public static void StopCheck()
{
if (timer==null)
{ return; }
timer.Stop();
TargetChangeCount = 0;
SignalChangeCount = 0;
}
private static bool IsInProcess = false;
protected static void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (IsInProcess) { return; }
IsInProcess = true;
IO_VALUE value = IOManager.IOValue(IO_Type.CompressAxis_Check);
if (value.Equals(IO_VALUE.HIGH) && PreSignalValue.Equals(IO_VALUE.LOW))
{
SignalChangeCount++;
LogUtil.info("MeteringSignal检测到信号变化,已经变化" + SignalChangeCount + "次(目标次数"+ TargetChangeCount + ")");
}
PreSignalValue = value;
if (TargetChangeCount <= SignalChangeCount)
{
timer.Stop();
}
IsInProcess = false;
}
}
}
using log4net;
using OnlineStore.Common;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
namespace OnlineStore.DeviceLibrary
{
/// <summary>
/// 硕科控制模块代码
/// </summary>
public class ShuoKeControls
{
public static readonly ILog LOGGER = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static AcSerialBean bean = null;
public static bool isRun = false;
private static Dictionary<int, ShuoKeInfo> shuokeMap = new Dictionary<int, ShuoKeInfo>();
public static string PortName = "";
private static int SleepMS = 50;
private static int DReviceLength = 5;
#region 串口操作
public static bool ResertInitPort(string comPortName, int baudRate, int tparity, int dataBits, StopBits stopBits)
{
PortName = comPortName;
if (isRun)
{
ClosePort();
}
return InitPort(comPortName, baudRate, tparity, dataBits, stopBits);
}
public static bool InitPort(string comPortName, int baudRate, int tparity, int dataBits, StopBits stopBits)
{
if (isRun)
{
return true;
}
try
{
ShuoKeControls.CheckErrorCount = 0;
PortName = comPortName;
Parity parity = (Parity)tparity;
bean = new AcSerialBean(comPortName, baudRate, parity, 8, stopBits);
bool result = bean.openPort();
if (!result)
{
return false;
}
//bean.DataReceived += DataReceived;
isRun = true;
}
catch (Exception ex)
{
isRun = false;
LogUtil.error(LOGGER, "打开步进控制器串口失败:" + ex.ToString());
return false;
}
return true;
}
public static void ClosePort()
{
ShuoKeControls.CheckErrorCount = 0;
isRun = false;
if (bean == null)
{
return;
}
bean.closePort();
LogUtil.info(LOGGER, "关闭步进控制器串口完成");
}
/// <summary>
/// 发送数据并获取返回值
/// </summary>
/// <param name="data">发送的数据</param>
/// <param name="outTime">超时时间</param>
/// <returns>返回值的长度</returns>
public static byte[] SendCommand( byte[] data, int outTime, int reviceLength)
{
if (outTime <= 0)
{
outTime = 30;
}
if (outTime > 80)
{
outTime = 80;
}
byte[] returnData = null;
try
{
if (data == null)
{
return returnData;
}
string strSend = "";
for (int i = 0; i < data.Length; i++)
{
strSend += string.Format("{0:X2} ", data[i]);
}
if (strSend.Equals(""))
{
return returnData;
}
LogUtil.debug("串口" + PortName + " 写入数据:" + strSend + "");
if (bean == null)
{
LogUtil.debug(" 试图向未打开串口【" + PortName + "】写入数据:" + strSend + "。");
}
else
{
bean.SendCommand(data, ref returnData, outTime, reviceLength);
Thread.Sleep(2);
}
}
catch (Exception ex)
{
LogUtil.info(ex.ToString());
}
return returnData;
}
public static int CheckErrorCount = 0;
private static bool CheckByte(byte[] bits)
{
if (bits == null)
{
return false;
}
string strSend = "";
for (int i = 0; i < bits.Length; i++)
{
strSend += string.Format("{0:X2} ", bits[i]);
}
//校验
ushort pChecksum = 0;
AcSerialBean.CalculateCRC(bits, bits.Length - 2, out pChecksum);
string checkStr = Convert.ToString(pChecksum, 16);
byte[] checkByte = AcSerialBean.StringToByte(checkStr);
bool isError = false;
if ((checkByte.Length == 1) && (!bits[bits.Length - 2].Equals(checkByte[0])))
{
isError = true;
}
else if (!bits[bits.Length - 1].Equals(checkByte[0]) || (!bits[bits.Length - 2].Equals(checkByte[1])))
{
isError = true;
}
if (isError)
{
bean.clearInBuffer();
CheckErrorCount++;
if (CheckErrorCount > 10)
{
LogUtil.error( "【" + PortName + "】收到数据后已累计【"+CheckErrorCount+"】次校验错误,通信异常",201);
}
else
{
LogUtil.error( "【" + PortName + "】收到数据【" + strSend + "】校验错误");
}
return false;
}
CheckErrorCount = 0;
return true;
}
private static int DataToPosition(byte[] bits)
{
int absValue = -1;
if (!CheckByte(bits))
{
return absValue;
}
if (bits.Length >= 7)
{
int slvAddr = (int)bits[0];
byte[] positionData = new byte[4];
Array.Copy(bits, 3, positionData, 0, 4);
string a = AcSerialBean.byteToHexStr(positionData);
absValue = Convert.ToInt32(a, 16);
LogUtil.debug(LOGGER, "收到驱动器【" + slvAddr + "】绝对位置【" + absValue + "】");
if (shuokeMap.ContainsKey(slvAddr))
{
shuokeMap[slvAddr].LastPosition = absValue;
}
else
{
ShuoKeInfo info = new ShuoKeInfo(slvAddr, absValue);
shuokeMap.Add(slvAddr, info);
}
}
return absValue;
}
private static ShuoKeInfo DataToStatus(byte[] bits)
{
ShuoKeInfo info = new ShuoKeInfo(-1, -1);
if (!CheckByte(bits))
{
return info;
}
if (bits.Length >= 8)
{
//[数据1]:1表示运动中,0表示停止
//[数据2]:LL-左限位IO状态 1表示有效 0表示无效
//[数据3]:O-原点IO状态 1表示有效 0表示无效,,=1表示原点灭,=0表示当前原点亮
// [数据4]:RL-右限位IO状态 1表示有效 0表示无效
//[数据5]:EN-使能IO状态 1表示有效 0表示无效
int slvAddr = (int)bits[0];
byte cmd = bits[1];
int isInMove = bits[3];
int isLLimit = bits[4];
int isOrg = bits[5];
int isRLimit = bits[6];
int isEn = bits[7];
info = new ShuoKeInfo(isInMove, slvAddr, isLLimit, isOrg, isRLimit, isEn);
try
{
if (shuokeMap.ContainsKey(slvAddr))
{
info.LastPosition = shuokeMap[slvAddr].LastPosition;
shuokeMap.Remove(slvAddr);
}
shuokeMap.Add(slvAddr, info);
}
catch (Exception ex)
{
LogUtil.error(LOGGER, "记录状态出错:" + ex.ToString());
}
LogUtil.debug(LOGGER, "收到驱动器【" + slvAddr + "】运动状态:运动【" + isInMove + "】左限位【" + isLLimit + "】右限位【" + isRLimit + "】原点【" + isOrg + "】使能【" + isEn + "】");
}
return info;
}
public static int GetLastPosition(int slvAddr)
{
if (shuokeMap.ContainsKey(slvAddr))
{
ShuoKeInfo info = shuokeMap[slvAddr];
return info.LastPosition;
}
return -1;
}
#endregion
#region 逻辑判断
public static bool IsMoveEnd(int slvAddr, int targetPosition, DateTime time, out string msg)
{
ShuoKeInfo shuokeInfo = GetStatus(slvAddr);
msg = " IsInMove【" + shuokeInfo.IsInMove + "】 ";
if (shuokeInfo.IsInMove == 0 && shuokeInfo.UpdateTime > time)
{
int position = GetABSPosition(slvAddr);
msg += "位置【" + position + "】";
if (Math.Abs(position - targetPosition) < 10 || targetPosition.Equals(-2000))
{
return true;
}
else
{
LogUtil.info("【" + PortName + "】 目标位置【" + targetPosition + "】当前位置【" + position + "】,已经不再运动中");
}
}
return false;
}
public static bool IsHomeMoveEnd(int slvAddr, DateTime time, out string msg)
{
ShuoKeInfo shuokeInfo = GetStatus(slvAddr);
msg = " IsInMove【" + shuokeInfo.IsInMove + "】Org【" + shuokeInfo.Org + "】";
if (shuokeInfo.IsInMove .Equals(0) && shuokeInfo.Org.Equals(0)&&shuokeInfo.UpdateTime>time)
{
return true;
}
return false;
}
#endregion
#region 驱动器操作方法
public static ShuoKeInfo GetStatus(int slvAddr)
{
Thread.Sleep(SleepMS);
byte[] reviceData = SendData(slvAddr, ShuoKeCMD.SearchMoveStatus, 0x00, 0,10);
return DataToStatus(reviceData);
}
public static void DStoop(int slvAddr)
{
byte[] reviceData = SendData(slvAddr, ShuoKeCMD.DStopMove, 0x00, 0,DReviceLength);
}
public static void SuddownStop(int slvAddr)
{
byte[] reviceData = SendData(slvAddr, ShuoKeCMD.SuddownStopMove, 0x00, 0, DReviceLength);
}
public static int GetABSPosition(int slvAddr)
{
byte[] reviceData = SendData(slvAddr, ShuoKeCMD.GetAbsPosition, 0x00, 0, 9);
return DataToPosition(reviceData);
}
public static bool IsInPosition(int slvAddr,int position)
{
int outCount = GetABSPosition(slvAddr);
int errorCount = Math.Abs(outCount - position);
if (errorCount <= 100)
{
return true;
}
else
{
return false;
}
}
// <param name="homeType">0=反方向,1=正方向</param>
public static void HomeMove(int slvAddr, byte homeType)
{
string fangx = homeType.Equals(0) ? "反方向" : "正方向";
LogUtil.debug("压紧轴原点返回:" + fangx);
byte[] reviceData = SendData(slvAddr, ShuoKeCMD.HomeMove, 0x01, homeType, DReviceLength);
}
public static void SetConfigSpeed(AUTO_SA_Config Config)
{
//ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetAddSpeed, Config.CompressAxis_AddSpeed);
//ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetDelSpeed, Config.CompressAxis_DelSpeed);
//ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetEndSpeed, Config.CompressAxis_EndSpeed);
//ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetHomeSpeed, Config.CompressAxis_HomeSpeed);
//ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetMaxSpeed, Config.CompressAxis_MaxSpeed);
//ShuoKeControls.SetSpeed(Config.CompressAxis_Slv, ShuoKeCMD.SetStartSpeed, Config.CompressAxis_StartSpeed);
}
public static void VolMove(int slvAddr, int speed)
{
byte[] reviceData = SendData(slvAddr, ShuoKeCMD.VolMove, 0x04, speed, DReviceLength);
}
public static void AbsMove(int slvAddr, int TargetPosition)
{
// 47、设置绝对位置并开始运动:不支持广播(开环的32位脉冲统计长度)
//正负数表示绝对位置,如果已经到位置,则不做运动。(M4505S特有命令)
//发送: 应答:
//[设备地址] 0x01 [设备地址] 0x01
//[命令] 0x50 [命令] 0x50
//[数据长度] 4 [数据长度] 0
//[数据] xxxxxxxx [数据] 无
//[CRC校验1] 0xnn [CRC校验1] 0xnn
//[CRC校验2] 0xnn [CRC校验2] 0xnn
//数据:4BYTE,(支持32位有符号脉冲位置)
byte[] reviceData = SendData(slvAddr, ShuoKeCMD.AbsMove, 0x04, TargetPosition, DReviceLength);
}
public static void RelativeMove(int slvAddr, int position)
{
//1是正方向
byte fangxiang = 0x01;
if (position < 0)
{
fangxiang = 0x00;
position = Math.Abs(position);
}
byte dataLength = 0x04;
byte[] sendData = new byte[5 + dataLength];
sendData[0] = (byte)slvAddr;
sendData[1] = ShuoKeCMD.RelativeMove;
sendData[2] = dataLength;
sendData[3] = 0x00;
sendData[4] = 0x00;
sendData[5] = 0x00;
sendData[6] = 0x00;
string speed = Convert.ToString(position, 16);
byte[] speedByte = AcSerialBean.StringToByte(speed);
for (int i = 0; i < speedByte.Length; i++)
{
if (i >= 4)
{
break;
}
sendData[6 - (speedByte.Length - 1 - i)] = speedByte[i];
}
sendData[3] = fangxiang;
sendData = buildCheckData(sendData, 3 + dataLength);
byte[] reviceData = SendCommand(sendData,SleepMS,DReviceLength);
Thread.Sleep(SleepMS);
}
public static void PositionClear(int slvAddr, byte setCMD)
{
byte[] reviceData = SendData(slvAddr, setCMD, 0x00, 0, DReviceLength);
}
/// <summary>
/// 设置电机的速度
/// </summary>
/// <param name="slvAddr">电机地址</param>
/// <param name="setCMD">设置速度的命令号</param>
/// <param name="speedValue">速度值</param>
public static void SetSpeed(int slvAddr, byte setCMD, int speedValue)
{
byte[] reviceData = SendData(slvAddr, setCMD, 0x04, speedValue, DReviceLength);
Thread.Sleep(10);
}
private static byte[] SendData(int slvAddr, byte cmd, byte dataLength, int dataValue, int reviceDataLength)
{
byte[] sendData = new byte[5 + dataLength];
sendData[0] = (byte)slvAddr;
sendData[1] = cmd;
sendData[2] = dataLength;
if (dataLength.Equals(0x00))
{
}
else if (dataLength.Equals(0x01))
{
sendData[3] = (byte)dataValue;
}
else if (dataLength.Equals(0x04))
{
sendData[3] = 0x00;
sendData[4] = 0x00;
sendData[5] = 0x00;
sendData[6] = 0x00;
string speed = Convert.ToString(dataValue, 16);
byte[] speedByte = AcSerialBean.StringToByte(speed);
for (int i = 0; i < speedByte.Length; i++)
{
if (i >= 4)
{
break;
}
sendData[6 - (speedByte.Length - 1 - i)] = speedByte[i];
}
}
sendData = buildCheckData(sendData, 3 + dataLength);
byte[] reviceData = SendCommand(sendData, SleepMS, reviceDataLength);
Thread.Sleep(SleepMS);
return reviceData;
}
private static byte[] buildCheckData(byte[] sendData, int length)
{
ushort pChecksum = 0;
AcSerialBean.CalculateCRC(sendData, length, out pChecksum);
string checkStr = Convert.ToString(pChecksum, 16);
byte[] checkByte = AcSerialBean.StringToByte(checkStr);
if (checkByte.Length == 1)
{
sendData[length] = checkByte[0];
sendData[length + 1] = 0x00;
}
else
{
sendData[length + 1] = checkByte[0];
sendData[length] = checkByte[1];
}
return sendData;
}
#endregion
}
public class ShuoKeCMD
{
/// <summary>
/// 设置运动初速度://SndHex:01 25 04 00 00 00 64 FC DE
/// </summary>
public static byte SetStartSpeed = 0x25;
/// <summary>
/// 设置运动最大速度://SndHex:01 27 04 00 00 27 10 E6 EB
/// </summary>
public static byte SetMaxSpeed = 0x27;
/// <summary>
/// 设置运动末速度://SndHex:01 29 04 00 00 03 E8 FD 47
/// </summary>
public static byte SetEndSpeed = 0x29;
/// <summary>
/// 设置运动加速度://SndHex:01 2B 04 00 00 13 88 F1 4D
/// </summary>
public static byte SetAddSpeed = 0x2B;
/// <summary>
/// 设置运动减速度://SndHex:01 2D 04 00 00 03 E8 FC C3
/// </summary>
public static byte SetDelSpeed = 0x2D;
/// <summary>
/// 设置归零运动速度://SndHex:01 2F 04 00 00 27 10 E7 A3
/// </summary>
public static byte SetHomeSpeed = 0x2F;
/// <summary>
/// //查找原点命令:
//SndHex:01 3E 01 00 61 84
/// </summary>
public static byte HomeMove = 0x3E;
/// <summary>
/// 绝对位置运动//SndHex:01 50 04 FF FF D8 F0 AC 30
/// </summary>
public static byte AbsMove = 0x50;
/// <summary>
/// 急停命令://SndHex:01 3C 00 31 00
/// </summary>
public static byte SuddownStopMove = 0x3C;
/// <summary>
/// 减速停止命令://SndHex:01 3D 00 30 90
/// </summary>
public static byte DStopMove = 0x3D;
/// <summary>
/// 绝对位置清零: //SndHex:01 38 00 33 C0
/// </summary>
public static byte AbsPositionClear = 0x38;
/// <summary>
/// 相对位置清零: //SndHex:01 3A 00 32 A0
/// </summary>
public static byte RelPositionClear = 0x3A;
/// <summary>
/// 查询运动状态://SndHex:01 3B 00 33 30
/// </summary>
public static byte SearchMoveStatus = 0x3B;
/// <summary>
//查询设备地址://SndHex:00 21 00 69 90
/// </summary>
public static byte SearchSlvAddr = 0x21;
/// <summary>
/// 设置32位运动速度,并恒速运动:SndHex:01 33 04 00 00 27 10 E5 FF
/// </summary>
public static byte VolMove = 0x33;
/// <summary>
/// 查询绝对位置
/// </summary>
public static byte GetAbsPosition = 0x37;
/// <summary>
/// 相对位置运动
/// </summary>
public static byte RelativeMove = 0x32;
}
public class ShuoKeInfo
{
public ShuoKeInfo(int slv, int position)
{
this.IsInMove = -1;
this.SlvAddr = slv;
this.LLimit = -1;
this.RLimit = -1;
this.Org = -1;
this.En = -1;
UpdateTime = DateTime.Now;
LastPosition = position;
}
public ShuoKeInfo(int ismove, int slv, int ll, int org, int rl, int en)
{
this.IsInMove = ismove;
this.SlvAddr = slv;
this.LLimit = ll;
this.RLimit = rl;
this.Org = org;
this.En = en;
UpdateTime = DateTime.Now;
LastPosition = 0;
}
public int LastPosition { get; set; }
/// <summary>
/// 驱动器地址
/// </summary>
public int SlvAddr { get; set; }
/// <summary>
/// 是否在运动中
/// </summary>
public int IsInMove { get; set; }
/// <summary>
/// 左极限
/// </summary>
public int LLimit { get; set; }
/// <summary>
/// 右极限
/// </summary>
public int RLimit { get; set; }
/// <summary>
/// 原点信号是否有效
/// </summary>
public int Org { get; set; }
/// <summary>
/// 使能是否有效
/// </summary>
public int En { get; set; }
public DateTime UpdateTime { get; set; }
public string ToShowStr()
{
return "运动[" + IsInMove + "]左限位[" + LLimit + "]右限位[" + RLimit + "]原点[" + Org + "]使能[" + En + "][" + UpdateTime.ToShortTimeString() + "]";
}
}
}

using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Windows.Forms;
using CodeLibrary;
using System.Drawing.Imaging;
namespace OnlineStore.DeviceLibrary
{
public class CodeManager
{
public static int DeCodeType = ConfigAppSettings.GetIntValue(Setting_Init.DeCodeType);
private static int QRCodeCount = ConfigAppSettings.GetIntValue(Setting_Init.QRCodeCount);
public static List<string> cameraNameList = new List<string>();
public static List<string> codeTypeList = new List<string>();
//public static List<string> balserNameList = new List<string>();
public static List<string> hikNameList = new List<string>();
private static char spiltChar = '#';
/// <summary>
/// 初始化摄像机名称和二维码类型
/// </summary>
public static void LoadConfig()
{
if (QRCodeCount < 1)
{
QRCodeCount = 1;
}
string nameStr = ConfigAppSettings.GetValue(Setting_Init.CameraName);
string codeStr = ConfigAppSettings.GetValue(Setting_Init.CodeType);
codeTypeList = new List<string>();
HDLogUtil.LogName = "RollingLogFileAppender";
try
{
string[] nameArray = nameStr.Split(spiltChar);
foreach (string str in nameArray)
{
if (str.Trim().Equals(""))
{
continue;
}
//LogUtil.info("加载到配置摄像机名称:" + str.Trim());
cameraNameList.Add(str.Trim());
}
string[] codeArray = codeStr.Split(spiltChar);
foreach (string str in codeArray)
{
if (str.Trim().Equals(""))
{
continue;
}
string file = GetCodeParamFilePath(str.Trim());
LogUtil.info("加载到配置二维码类型:" + str.Trim() + ",配置文件:" + file);
codeTypeList.Add(str.Trim());
}
LoadCamera(false);
CodeLibrary.HDCodeLearnHelper.LoadConfig(nameStr, codeStr);
//如果未加载到配置相机,自动配置
if (cameraNameList.Count <= 0 && hikNameList.Count > 0)
{
cameraNameList = new List<string>(hikNameList);
string cameraStr = "";
foreach (string name in hikNameList)
{
cameraStr += name + spiltChar;
}
cameraStr = cameraStr.Substring(0, cameraStr.Length - 1);
ConfigAppSettings.SaveValue(Setting_Init.CameraName, cameraStr);
LogUtil.info("未配置扫码相机,默认【" + Setting_Init.CameraName + "】=【" + cameraStr + "】");
}
}
catch (Exception ex)
{
LogUtil.error("解析摄像机配置出错:" + ex.StackTrace);
}
}
private static void LoadCamera(bool isReLoad)
{
if (isReLoad || Camera._cam == null)
{
try
{
if (Camera._cam != null)
{
Camera._cam.CloseAll();
}
Camera.Type = CameraType.HIK;
Camera._cam.Load();
}
catch (Exception ex)
{
LogUtil.error("加载HIK相机出错:" + ex.ToString());
}
}
string[] names = Camera._cam.Name;
if (names != null)
{
foreach (string n in names)
{
if (!hikNameList.Contains(n))
{
hikNameList.Add(n);
}
}
// hikNameList.AddRange(names);
foreach (string name in hikNameList)
{
LogUtil.info("加载到HIK相机:" + name);
}
}
}
public static void CloseCamera(string cameraName)
{
Camera._cam.Close(cameraName);
}
public static void CloseAllCamera()
{
Camera._cam.CloseAll();
}
[HandleProcessCorruptedStateExceptions]
public static List<string> CameraScan( )
{
int codeCount = 1;
List<string> codeList = new List<string>();
if (cameraNameList == null || cameraNameList.Count <= 0)
{
return codeList;
}
try
{
foreach (string cameraName in cameraNameList)
{
if (cameraName.Trim().Equals(""))
{
continue;
}
DateTime startTime = DateTime.Now;
LogUtil.info( " 【" + cameraName + "】开始取图片");
HalconDotNet.HObject ho_Image = null;
try
{
ho_Image = Camera._cam.CaptureOnImage(cameraName);
if (ho_Image == null)
{
LogUtil.error( " 【" + cameraName + "】取图片失败[" + Camera._cam.ErrInfo + "],关闭相机");
CloseCamera(cameraName);
continue;
}
LogUtil.info( " 【" + cameraName + "】取图片完成,开始扫码");
List<CodeInfo> cc = new List<CodeInfo>();
string r = "";
foreach (string codeType in codeTypeList)
{
//判断是否是一维码
if (codeType.ToLower().Equals("barcode"))
{
cc = HDCodeHelper.DecodeBarCode(ho_Image);
}
else
{
cc = HDCodeHelper.DecodeCode(ho_Image, codeCount, GetCodeParamFilePath(codeType), codeType);
}
foreach (CodeInfo c in cc)
{
string str = CodeManager.ReplaceCode(c.CodeStr);
if (!codeList.Contains(str))
{
codeList.Add(str);
r = r + "##" + str;
}
}
}
if (String.IsNullOrEmpty(r))
{
// SaveImageToFile(deviceName, cameraName, bit);
}
LogUtil.info( " 【" + cameraName + "】扫码完成【" + FormUtil.GetSpanStr(DateTime.Now - startTime) + "】 :" + r);
}
catch (AccessViolationException e)
{
LogUtil.error( " 扫码出现AccessViolationException异常,关闭相机【" + cameraName + "】:" + e.ToString());
Camera._cam.Close(cameraName);
// GC.Collect();
}
catch (Exception ex)
{
LogUtil.error( " 扫码出错:" + ex.ToString());
}
finally
{
if (ho_Image != null)
{
ho_Image.Dispose();
}
}
}
}
catch (AccessViolationException e)
{
LogUtil.error( " 扫码出现AccessViolationException异常:" + e.ToString());
// GC.Collect();
}
catch (Exception ex)
{
LogUtil.error( " 扫码出错:" + ex.ToString());
}
return codeList;
}
private static void SaveImageToFile(string deviceName, string cameraName, Bitmap bitmap)
{
string date = DateTime.Now.ToString("HH-mm-ss-") + DateTime.Now.Millisecond;
string dire = @"D:\image\" + deviceName.Trim().Replace('_', '-') + @"\" + cameraName.Trim().Replace('_', '-').Replace(':', '-') + @"\";
string iamgeName = date + ".bmp";
try
{
Bitmap bit = (Bitmap)bitmap.Clone();
if (Directory.Exists(dire).Equals(false))
{
Directory.CreateDirectory(dire);
}
bit.Save(dire + iamgeName, ImageFormat.Bmp);
LogUtil.info(deviceName + " 【" + cameraName + "】扫码失败,保存图片到【" + dire + iamgeName + "】成功");
}
catch (Exception ex)
{
LogUtil.error("保存" + deviceName + " 【" + cameraName + "】的图片到【" + dire + iamgeName + "】出错" + ex.ToString());
}
}
public static string GetCodeParamFilePath(string codePath)
{
string appPath = Application.StartupPath;
string path = appPath + ConfigAppSettings.GetValue(Setting_Init.CodeParamPath);
string filePath = path + codePath + ".dcm";
if (File.Exists(filePath))
{
return filePath;
}
else
{
return "";
}
}
/// <summary>
/// 处理接收后的二维码
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public static string ReplaceCode(string message)
{
message = message.Trim();
message = message.Replace("\r", "");
message = message.Replace("\n", "");
char a = (char)02;
message = message.Replace(a.ToString(), "");
message = message.Trim();
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] bytes = asciiEncoding.GetBytes(message);
List<byte> newBytes = new List<byte>();
foreach (byte by in bytes)
{
int value = (int)by;
if (value.Equals(24) || value.Equals(30) || value.Equals(29) || value.Equals(4))
{
continue;
}
if (!value.Equals(24))
{
newBytes.Add(by);
}
}
message = asciiEncoding.GetString(newBytes.ToArray());
return message;
}
public static string ProcessCode(List<string> codeList)
{
string code = "";
foreach (string cc in codeList)
{
if (string.IsNullOrEmpty(cc))
{
continue;
}
code += cc + "##";
}
return ReplaceCode(code);
}
}
}
using log4net;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Asa.RFID
{
public class RFIDAuto
{
public static readonly ILog LOGGER = LogManager.GetLogger("TheRFID");
private string IP = "";
private byte addr;
private int portIndex;
private bool IsConnect = false;
private byte[] lastData = new byte[8];
/// <summary>
/// 接收事件
/// </summary>
/// <param name="ip"></param>
/// <param name="buff"></param>
public delegate void Received_Event(string ip, byte[] buff);
/// <summary>
/// 接收数据
/// </summary>
public event Received_Event Received;
public RFIDAuto(string ip)
{
this.IP = ip;
}
public int StopAutoScan()
{
try
{
IsConnect = false;
byte scanMode = 0;
int ErrCode = SetAutoScanMode(addr, scanMode);
LOGGER.Info("Close RFID [" + IP + "] ScanMode:" + ErrCode);
ErrCode = ReaderA.StaticClassReaderA.CloseNetPort(portIndex);
LOGGER.Info("Close RFID [" + IP + "]:" + ErrCode);
return ErrCode;
}
catch (Exception e)
{
LOGGER.Error("Close RFID[" + IP + "] has error", e);
}
return -1;
}
public int StartAutoScan(Received_Event OnReceive)
{
if (IsConnect)
{
LOGGER.Warn(" RFID [" + IP + "] is already connected, no need to start again");
return 0;
}
this.Received = OnReceive;
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(IP, pattern);
if (!rtn)
{
LOGGER.Error(IP + " is error");
return -1;
}
string[] arr = IP.Split('.');
if (arr.Length != 4)
{
LOGGER.Error(IP + " length is not 4");
return -1;
}
addr = Convert.ToByte(arr[3]);
int port = 6000 + Convert.ToInt32(arr[3]);
portIndex = 0;
int ErrCode = 0;
for (int i = 1; i <= 3; i++)
{
ErrCode = ReaderA.StaticClassReaderA.OpenNetPort(port, IP, ref addr, ref portIndex);
LOGGER.Info("Connect [" + IP + "] [" + i + "] :" + ErrCode);
if (ErrCode == 0)
{
ErrCode = OpenAutoScanMode(addr);
LOGGER.Info(" [" + IP + "] Open Auto Scan Mode:" + ErrCode);
Task.Factory.StartNew(AutoScan);
break;
}
else if (i < 3)
{
LOGGER.Error("Connect [" + IP + "] " + ErrCode + ", StopAutoScan");
StopAutoScan();
}
Thread.Sleep(10);
}
return ErrCode;
}
private int SetAutoScanMode(byte addr, byte scanMode)
{
byte[] scanModeData = new byte[11];
//bit0: 0停止扫描,1开始扫描
//bit1: 0正常数据扫描,1EAS扫描
//bit2: 0正常灵敏度,1高灵敏度
//bit3: 0禁止输入同步,1允许输入同步
//bit4: 0禁止指令同步,1允许指令同步
//bit5: 0禁止扫描提示,1允许扫描提示
//bit6: 0禁止EAS探测提示,1允许EAS探测提示
//bit7: 0禁止EAS消息输出提示,1允许EAS消息输出提示
scanModeData[0] = scanMode;
//bit0: 0不发送comAddr,1发送comAddr
//bit1: 0不带UID,1带UID
//bit2: 0不包含指定块,1包含指定块
//bit3: 0不带安全状态字,1带安全状态字
//bit4: 0 ASCII,1 HEX
//bit5: 0不进行开关射频场,1进行开关射频场
//bit6: 0无操作,1停止扫描关闭射频场
//bit7: 0无,1鸣叫闪烁
scanModeData[1] = 255;
scanModeData[2] = 0; //起始块号
scanModeData[3] = 2; //块总数 之前是28
scanModeData[4] = 1; //块的起始字节
scanModeData[5] = 4; //块的字节数
//1 CR+LF 0x0D 0x0A
//2 CR 0x0D
//4 LF 0x0A
//8 BEL 0x07
//16 :(分号) 0x3B
//32 ,(逗号) 0x2C
//64 " "(空格) 0x20
//128 自定义
scanModeData[6] = 8; //间隔符
scanModeData[7] = 0; //自定义间隔符,[6] = 128
//1 CR+LF 0x0D 0x0A
//2 CR 0x0D
//4 LF 0x0A
//8 BEL 0x07
//16 :(分号) 0x3B
//32 ,(逗号) 0x2C
//64 " "(空格) 0x20
//128 自定义
scanModeData[8] = 8; //结束符
scanModeData[9] = 0; //自定义间隔符,[8] = 128
scanModeData[10] = 0x00;
int ErrCode = ReaderA.StaticClassReaderA.SetScanMode(ref addr, scanModeData, portIndex);
return ErrCode;
}
private DateTime LastOkTime = DateTime.Now;
private void AutoScan()
{
IsConnect = true;
byte[] dataBuffer = new byte[0];
while (IsConnect)
{
try
{
byte[] data = new byte[100];
int len = 0;
int ErrCode = ReaderA.StaticClassReaderA.ReadScanModeData(data, ref len, portIndex);
if (ErrCode == 0)
{
LastOkTime = DateTime.Now;
string dataStr = byteToStr(data, len);
LOGGER.Debug(IP + " Receive Code: " + ErrCode + " Len: " + len + " Data:" + dataStr);
byte[] temp = new byte[dataBuffer.Length + len];
Array.Copy(dataBuffer, temp, dataBuffer.Length);
Array.Copy(data, 0, temp, dataBuffer.Length, len);
byte[] bb = new byte[10];
int idx = -1;
bool start = false;
bool end = false;
int endIndex = -1;
for (int i = 0; i < temp.Length; i++)
{
byte b = temp[i];
if (b == 0x5A)
{
start = true;
idx = 0;
//bb[idx++] = result;
}
else if (b == 0x4A && start)
{
start = false;
end = true;
endIndex = i;
bb[idx++] = b;
}
if (start)
{
if (idx + 1 == bb.Length)
{
start = false;
end = false;
}
else
{
bb[idx++] = b;
}
}
if (end)
{
byte[] _buff = new byte[8];
_buff[0] = bb[0];
_buff[1] = bb[1];
_buff[2] = bb[2];
_buff[3] = bb[3];
_buff[4] = bb[5];
_buff[5] = bb[6];
_buff[6] = 0;
_buff[7] = 0;
end = false;
string itemStr = byteToStr(_buff, _buff.Length);
lastData = _buff;
LOGGER.Debug("[" + IP + "] Item:" + itemStr);
Received?.Invoke(IP, _buff);
}
}
endIndex++;
if (endIndex < temp.Length)
{
dataBuffer = new byte[temp.Length - endIndex];
Array.Copy(temp, endIndex, dataBuffer, 0, temp.Length - endIndex);
dataStr = byteToStr(dataBuffer, dataBuffer.Length);
LOGGER.Debug(IP + " Receive Code: 剩余 Length: " + dataBuffer.Length + " Data:" + dataStr);
}
}
else if (ErrCode == 14 && IsConnect)
{
LOGGER.Error(IP + " Receive Code: " + ErrCode + " 进行重连");
LogUtil.error(IP + " Receive Code: " + ErrCode + " 进行重连");
StopAutoScan();
StartAutoScan(this.Received);
break;
}
else
{
TimeSpan span = DateTime.Now - LastOkTime;
if (span.TotalMinutes > 2)
{
LastOkTime = DateTime.Now;
string dataStr = byteToStr(data, len);
LOGGER.Debug(IP + " Receive Code: " + ErrCode + " Len: " + len + " Data:" + dataStr);
}
}
System.Threading.Thread.Sleep(60);
}
catch (Exception e)
{
LOGGER.Error("ReadScanModeData Error:", e);
}
}
}
public byte[] ReadAndClear()
{
byte[] data = lastData;
lastData = new byte[8];
LOGGER.Debug("Read and clear RFID[" + IP + "] data:" + byteToStr(data, data.Length));
return data;
}
public byte[] Read()
{
return lastData;
}
private int OpenAutoScanMode(byte addr)
{
byte scanMode = 1;
return SetAutoScanMode(addr, scanMode);
}
private string byteToStr(byte[] data, int len)
{
string s = "";
for (int i = 0; i < len; i++)
{
s += data[i].ToString("X2") + " ";
}
return s;
}
}
}
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Asa.RFID.RFIDAuto;
namespace Asa.RFID
{
public class RFIDAutoReader
{
public static readonly ILog LOGGER = LogManager.GetLogger("TheRFID");
private static Dictionary<string, RFIDAuto> rfidMap = new Dictionary<string, RFIDAuto>();
/// <summary>
/// 打开所有
/// </summary>
/// <param name="ipArr"></param>
/// <param name="OnReceive"></param>
/// <param name="ip"></param>
public static void Open(Received_Event OnReceive, params string[] ipArr)
{
foreach (var ip in ipArr)
{
if (rfidMap.ContainsKey(ip))
{
rfidMap[ip].StartAutoScan(OnReceive);
}
else
{
RFIDAuto rfid = new RFIDAuto(ip);
rfid.StartAutoScan(OnReceive);
rfidMap.Add(ip, rfid);
}
}
}
/// <summary>
/// 获取最后读到的标签并清理
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static byte[] ReadAndClear(string ip)
{
if (rfidMap.ContainsKey(ip))
{
return rfidMap[ip].ReadAndClear();
}
return null;
}
/// <summary>
/// 获取最后读到的标签
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static byte[] Read(string ip)
{
if (rfidMap.ContainsKey(ip))
{
return rfidMap[ip].Read();
}
return null;
}
/// <summary>
/// 关闭所有
/// </summary>
public static void CloseAll()
{
foreach (var rfid in rfidMap.Values)
{
rfid.StopAutoScan();
}
}
}
}

using Asa.RFID;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OnlineStore.DeviceLibrary
{
public class RFIDManager
{
private static bool IsOpen = false;
public static void Open(string[] iparray)
{
if (IsOpen)
{
LogUtil.info("RFIDManager.Open , IsOpen = true , return ");
return;
}
Task.Factory.StartNew(delegate
{
LogUtil.info("RFIDManager.Open , set IsOpen = true");
IsOpen = true;
try
{
RFIDAutoReader.Open(null, iparray);
}
catch (Exception ex)
{
LogUtil.error("Open 出错:" + ex.ToString());
}
});
}
public static RFIDData ReadRFID(string ip, bool isClear = false)
{
try
{
if (String.IsNullOrEmpty(ip).Equals(false))
{
byte[] bdata = null;
if (isClear)
{
bdata = RFIDAutoReader.ReadAndClear(ip);
}
else
{
bdata = RFIDAutoReader.Read(ip);
}
if (bdata == null)
{
LogUtil.error("RFID [ " + ip + " ] 读到数据=null");
}
RFIDData data = new RFIDData(bdata);
return data;
}
}
catch (Exception ex)
{
//LogUtil.error("ReadRFID["+ip+"]出错:" + ex.ToString());
}
return new RFIDData();
}
public static void Close()
{
Task.Factory.StartNew(delegate
{
IsOpen = false;
LogUtil.info("RFIDManager.Close ,set IsOpen= false");
try
{
RFIDAutoReader.CloseAll();
}
catch (Exception ex)
{
LogUtil.error("关闭RFID出错:" + ex.ToString());
}
});
}
}
public class RFIDData
{
/// <summary>
/// RFID类型,区分是料架还是托盘,托盘E=69,包装料料架A=65,料串=B
/// </summary>
public char RFType = '0';
public int Num = 0;
public RFIDData(int num = 0, int t = 0)
{
if (t < 65)
{
RFType = '0';
}
else
{
this.RFType = (char)t;
}
this.Num = num;
}
public RFIDData(byte[] data)
{
try
{
if (data != null && data.Length > 2)
{
if (data[1] < 65)
{
RFType = '0';
}
else
{
RFType = (char)data[1];
}
Num = (int)(data[2]);
}
}
catch (Exception ex)
{
LogUtil.error("RFIP 数据【" + data + "】 获取编码失败");
}
}
public byte[] ToData()
{
return new byte[] { (byte)RFType, (byte)Num };
}
public string NumStr()
{
return "" + RFType + "" + Num.ToString() + "";
}
}
}
using log4net;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Asa.RFID
{
public class RFID
{
public static readonly ILog LOGGER = LogManager.GetLogger("TheRFID");
private System.Threading.Thread tScan;
private bool loop;
private string IP = "";
private byte addr;
private int portIndex;
private bool IsConnect = false;
private byte[] lastData = new byte[8];
internal byte[] _buff; //8字节缓存
private byte[] _uid; //卡片ID
public bool IsExist = false;
public int ErrCode = 0;
/// <summary>
/// 接收事件
/// </summary>
/// <param name="ip"></param>
/// <param name="buff"></param>
public delegate void Received_Event(string ip, byte[] buff);
/// <summary>
/// 接收数据
/// </summary>
public event Received_Event Received;
public RFID(string ip)
{
this.IP = ip;
}
public int Close()
{
try
{
loop = false;
IsConnect = false;
int ErrCode = ReaderA.StaticClassReaderA.CloseNetPort(portIndex);
LOGGER.Info("Close RFID [" + IP + "]:" + ErrCode);
return ErrCode;
}
catch (Exception e)
{
LOGGER.Error("Close RFID[" + IP + "] has error", e);
}
return -1;
}
public int Open()
{
if (IsConnect)
{
LOGGER.Warn(" RFID [" + IP + "] is already connected, no need to start again");
return 0;
}
//IP合法
string pattern = @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$";
bool rtn = System.Text.RegularExpressions.Regex.IsMatch(IP, pattern);
if (!rtn)
{
LOGGER.Error(IP + " is error");
return -1;
}
string[] arr = IP.Split('.');
if (arr.Length != 4)
{
LOGGER.Error(IP + " length is not 4");
return -1;
}
addr = Convert.ToByte(arr[3]);
int port = 6000 + Convert.ToInt32(arr[3]);
portIndex = 0;
int ErrCode = ReaderA.StaticClassReaderA.OpenNetPort(port, IP, ref addr, ref portIndex);
LOGGER.Info("Connect [" + IP + "] :" + ErrCode);
// 在 OpenNetPort 后面使用
System.Threading.Thread.Sleep(100);
OpenCloseRF(false);
System.Threading.Thread.Sleep(100);
OpenCloseRF(true);
return ErrCode;
}
public void FindMode()
{
loop = true;
tScan = new System.Threading.Thread(new System.Threading.ThreadStart(Scan));
tScan.Start();
}
private void Scan()
{
while (loop)
{
bool rtn = FindRFID();
if (rtn)
{
ReadRFID();
byte[] bb = new byte[] { _buff[0], _buff[1], _buff[2] };
string dataStr = byteToStr(bb, bb.Length);
LOGGER.Info(IP + " Scan Data:" + dataStr);
Received?.Invoke(IP, bb);
}
System.Threading.Thread.Sleep(50);
}
}
private void OpenCloseRF(bool rtn)
{
if (rtn)
ErrCode = ReaderA.StaticClassReaderA.OpenRf(ref addr, portIndex);
else
ErrCode = ReaderA.StaticClassReaderA.CloseRf(ref addr, portIndex);
}
/// <summary>
/// 查找电子标签,扫描模式不能使用
/// </summary>
/// <returns></returns>
public bool FindRFID()
{
try
{
//0 不带AFI
//1 带AFI
//2 不带AFI,继续查询
//3 带AFI,继续查询
//6 不带AFI,新的查询
//7 带AFI,新的查询
byte state = 0;
//Select模式需要AFI
byte AFI = 0;
//输出,1字节DSFID,8字节UID
byte[] DSFIDAndUID = new byte[9];
//输出,标签数量
byte cardNumber = 0;
if (_uid == null)
{
_uid = new byte[8];
}
ErrCode = ReaderA.StaticClassReaderA.Inventory(ref addr, ref state, ref AFI, DSFIDAndUID, ref cardNumber, portIndex);
if (ErrCode == 0)
{
//查询时间
//fCmdRet = StaticClassReaderA.GetInventoryTime(ref n_time, portIndex);
Array.Copy(DSFIDAndUID, 1, _uid, 0, 8);
IsExist = true;
return true;
}
else
{
for (int i = 0; i < _uid.Length; i++)
_uid[i] = 0;
IsExist = false;
}
}catch(Exception ex)
{
LOGGER.Error(IP + "FindRFID Error:" + ex.ToString());
}
return false;
}
public byte[] Read(bool isNeedFind = false)
{
if (isNeedFind)
{
FindRFID();
}
if (IsExist)
{
ReadRFID();
return _buff;
}
else
{
LOGGER.Info(IP + " Read: IsExist=false ");
}
return null;
}
private void ReadRFID()
{
try
{
//0 不带AFI
//1 带AFI
//2 不带AFI,继续查询
//3 带AFI,继续查询
//6 不带AFI,新的查询
//7 带AFI,新的查询
byte state = 0;
//起始块号
byte blockNum = 0;
//块数量
byte blockCount = 2;
//输出,块内安全信息,长度为BlockCount个字节
byte[] blockSecStatus = new byte[blockCount];
//输出,块内数据信息,长度为块的大小(4或8字节)乘以BlockCount个字节
byte[] data = new byte[4 * blockCount];
//错误代码
byte errorCode = 0;
if (_buff == null)
{
_buff = new byte[8];
}
ErrCode = ReaderA.StaticClassReaderA.ReadMultipleBlock(ref addr, ref state, _uid, blockNum, blockCount, blockSecStatus, data, ref errorCode, portIndex);
if (ErrCode == 0)
{
Array.Copy(data, 0, _buff, 0, data.Length);
string dataStr = byteToStr(data, data.Length);
LOGGER.Info(IP + " ReadMultipleBlock: Length: " + data.Length + " Data:" + dataStr);
}
else
{
LOGGER.Info(IP + " ReadMultipleBlock: ErrCode: " + ErrCode);
}
}
catch (Exception ex)
{
LOGGER.Error(IP + "ReadRFID Error:" + ex.ToString());
}
}
private string byteToStr(byte[] data, int len)
{
string s = "";
for (int i = 0; i < len; i++)
{
s += data[i].ToString("X2") + " ";
}
return s;
}
}
public class RFIDReader
{
public static readonly ILog LOGGER = LogManager.GetLogger("TheRFID");
private static Dictionary<string, RFID> rfidMap = new Dictionary<string, RFID>();
private static Dictionary<string, byte[]> LastRfidData = new Dictionary<string, byte[]>();
public static void Open(params string[] ipArr)
{
try
{
foreach (var ip in ipArr)
{
if (rfidMap.ContainsKey(ip))
{
rfidMap[ip].Open();
rfidMap[ip].FindMode();
}
else
{
RFID rfid = new RFID(ip);
rfid.Received += Rfid_Received;
rfid.Open();
rfid.FindMode();
rfidMap.Add(ip, rfid);
}
}
}
catch (Exception ex)
{
LOGGER.Error ("RFIDReader Open Error:" + ex.ToString());
}
}
private static void Rfid_Received(string ip, byte[] buff)
{
if (buff == null)
{
return;
}
string dataStr = byteToStr(buff, buff.Length);
LOGGER.Info(ip + " Rfid_Received 保存数据:" + dataStr);
if (LastRfidData.ContainsKey(ip))
{
LastRfidData[ip] = buff;
}
else
{
LastRfidData.Add(ip, buff);
}
}
//public static bool FindRFID(string ip)
//{
// if (rfidMap.ContainsKey(ip))
// {
// return rfidMap[ip].FindRFID();
// }
// return false;
//}
public static byte[] Read(string ip, bool isClear = false)
{
if (rfidMap.ContainsKey(ip))
{
if (LastRfidData.ContainsKey(ip))
{
byte[] data= LastRfidData[ip];
if (isClear)
{
LastRfidData.Remove(ip);
}
return data;
}
// return rfidMap[ip].Read(isNeedFind);
}
return null;
}
public static void CloseAll()
{
foreach (var rfid in rfidMap.Values)
{
rfid.Close();
}
}
private static string byteToStr(byte[] data, int len)
{
string s = "";
for (int i = 0; i < len; i++)
{
s += data[i].ToString("X2") + " ";
}
return s;
}
}
}
using Asa.RFID;
using OnlineStore.Common;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OnlineStore.DeviceLibrary
{
public class RFIDManager
{
private static ReadAll readAll = new ReadAll("TheRFID");
private static bool IsOpen = false;
private static ConcurrentDictionary<string, string> LastRfidMap = new ConcurrentDictionary<string, string>();
public static void Open(string[] iparray)
{
if (IsOpen)
{
return;
}
try
{
// readAll.Received += ReadAll_Received;
// readAll.Log += ReadAll_Log;
readAll.Start();
IsOpen = true;
}
catch (Exception ex)
{
LogUtil.error("Open 出错:" + ex.ToString());
}
}
private static void ReadAll_Log(string ip, string log)
{
LogUtil.LOGGER.Info("RFID : [" + ip + "] " + log);
}
public static RFIDData ReadRFID(string ip, bool isClear = false)
{
try
{
if (String.IsNullOrEmpty(ip).Equals(false))
{
string outValue = readAll.Read(ip);
if (isClear)
{
readAll.Clear(ip);
}
if (outValue.Equals("000"))
{
// LogUtil.error("ReadRFID[" + ip + "]=" + outValue);
}
RFIDData data = new RFIDData(outValue);
return data;
}
}
catch (Exception ex)
{
LogUtil.error("ReadRFID[" + ip + "] [" + isClear + "] 出错:" + ex.ToString());
}
return new RFIDData();
}
public static void Close()
{
try
{
readAll.Stop();
}
catch (Exception ex)
{
LogUtil.error("关闭RFID出错:" + ex.ToString());
}
}
}
public class RFIDData
{
public string StrData = "";
public RFIDData(string data = "00")
{
if (String.IsNullOrEmpty(data))
{
data = "00";
}
this.StrData = data;
}
public string NumStr()
{
return StrData;
}
internal static bool IsRealRfid(string shelfRfid)
{
//判断料架号是否是真实的料架号
if (shelfRfid.StartsWith("C") || shelfRfid.StartsWith("D"))
{
int num = -1;
try
{
num = Convert.ToInt32(shelfRfid.Substring(1, shelfRfid.Length - 1));
}
catch (Exception ex)
{
}
if (num > 0)
{
return true;
}
}
return false;
}
}
}
......@@ -67,9 +67,9 @@ namespace OnlineStore.DeviceLibrary
//添加调试
IsDebug = config.IsInDebug.Equals(1);
Name = (" BOX_" + config.Id + " ").ToUpper();
this.DeviceID = config.Id; ;
this.ID = config.Id; ;
this.Config = config;
DoorBean = new EnteryDoorBean(DeviceID);
DoorBean = new EnteryDoorBean(ID);
//温湿度
humBean = new HumitureBean(config.Humiture_Port, Name);
// humBean.Init();
......@@ -79,20 +79,12 @@ namespace OnlineStore.DeviceLibrary
PositionNumList = new List<string>();
foreach (ACBoxPosition position in positionList)
{
if (position.StoreId.Equals(DeviceID) && ACBoxPosition.CheckPosition(position, Config))
if (position.StoreId.Equals(ID) && ACBoxPosition.CheckPosition(position, Config))
{
PositionNumList.Add(position.PositionNum);
}
}
List<ShelfPosition> sPlist = CSVPositionReader<ShelfPosition>.getPositionList();
ShelfPosList = new List<string>();
foreach (ShelfPosition p in sPlist)
{
if (p.StoreId.Equals(DeviceID))
{
ShelfPosList.Add(p.PositionNum);
}
}
mainTimer.Enabled = false;
int isAuto = ConfigAppSettings.GetIntValue(Setting_Init.App_AutoRun);
if (isAuto == 1)
......
......@@ -339,11 +339,7 @@ namespace OnlineStore.DeviceLibrary
}
private void UpdateShelfId()
{
//读取RFID
RFIDData data = RFIDManager.ReadRFID(Config.RFID_IP, true);
CurrShelfID = data.NumStr();
LogUtil.info(Name + "更新当前料架号CurrShelfID=【" + CurrShelfID + "】,LastRfidID=【" + GetLastRfid() + "】");
{
}
......
......@@ -77,7 +77,7 @@ namespace OnlineStore.DeviceLibrary
{
if (MoveInfo.MoveParam.ShelfPosID.Equals(""))
{
MoveInfo.MoveParam.UpdateShelfPosId(ShelfPosList[0]);
// MoveInfo.MoveParam.UpdateShelfPosId(ShelfPosList[0]);
InOutStoreLog("出库 " + MoveInfo.SLog + ": ShelfPosID未设置,默认ShelfPosID=" + MoveInfo.MoveParam.ShelfPosID);
}
}
......@@ -129,7 +129,7 @@ namespace OnlineStore.DeviceLibrary
{
if (MoveInfo.MoveParam.ShelfPosID.Equals(""))
{
MoveInfo.MoveParam.UpdateShelfPosId(ShelfPosList[0]);
// MoveInfo.MoveParam.UpdateShelfPosId(ShelfPosList[0]);
InOutStoreLog("出库 " + MoveInfo.SLog + ": ShelfPosID未设置,默认ShelfPosID=" + MoveInfo.MoveParam.ShelfPosID);
}
// LastRfidID = MoveInfo.MoveParam.rfid;
......@@ -337,7 +337,7 @@ namespace OnlineStore.DeviceLibrary
{
MoveInfo.NextMoveStep(StoreMoveStep.BS_09_WaitTime);
InOutStoreLog(moveName + MoveInfo.SLog + ":再转动1000 , 等待入料口无信号,清理RFID");
RFIDManager.ReadRFID(Config.RFID_IP, true);
// RFIDManager.ReadRFID(Config.RFID_IP, true);
MoveInfo.WaitList.Add(WaitResultInfo.WaitTime(1000));
MoveInfo.WaitList.Add(WaitResultInfo.WaitIO(IO_Type.LineIn_Check, IO_VALUE.LOW));
}
......@@ -568,7 +568,7 @@ namespace OnlineStore.DeviceLibrary
if (humBean.NeedGetTem())
{
lineOperation.op = 5;
LogUtil.error(Name + "没有湿度预警范围,需要从服务器获取,发送OP=" + lineOperation.op, DeviceID + 105);
LogUtil.error(Name + "没有湿度预警范围,需要从服务器获取,发送OP=" + lineOperation.op, ID + 105);
}
string server = ConfigAppSettings.GetValue(Setting_Init.http_server);
Operation resultOperation = HttpHelper.Post(StoreManager.GetPostApi(server), lineOperation, false);
......
......@@ -46,9 +46,9 @@ namespace OnlineStore.DeviceLibrary
InitTimer();
baseConfig = lineConfig;
this.Config = lineConfig;
this.DeviceID = lineConfig.Id;
this.ID = lineConfig.Id;
MoveInfo = new StoreMoveInfo(DeviceID);
MoveInfo = new StoreMoveInfo(ID );
Name = (" Store_" + Config.CID + " ").ToUpper();
List<string> ioList = new List<string>();
......@@ -138,7 +138,7 @@ namespace OnlineStore.DeviceLibrary
StartTime = DateTime.Now;
//AgvClient.SetCancelState(false);
RFIDManager.Open(rfidList.ToArray());
//RFIDManager.Open(rfidList.ToArray());
mainTimer.Enabled = false;
isInSuddenDown = false;
......
......@@ -110,25 +110,7 @@ namespace OnlineStore.DeviceLibrary
LogUtil.info("加载料仓完成!");
}
string shelfConfig = appPath + ConfigAppSettings.GetValue(Setting_Init.Shelf_Position_Config);
//if (File.Exists(shelfConfig))
//{
// CSVPositionReader<ShelfPosition>.AddCSVFile(shelfConfig);
//}
if (count > 1 || (!File.Exists(shelfConfig)))
{
for (int i = 1; i <= count; i++)
{
string nameStr = i.ToString().PadLeft(1, '0');
string fileN = shelfConfig.Replace(".csv", "_" + nameStr + ".csv");
CSVPositionReader<ShelfPosition>.AddCSVFile(fileN, i);
}
}
else
{
CSVPositionReader<ShelfPosition>.AddCSVFile(shelfConfig);
}
Store = new DUOStoreBean(Config, storeConfig);
}
}
......@@ -195,12 +177,7 @@ namespace OnlineStore.DeviceLibrary
LogUtil.error(box.Name + "GetPositon[" + param.PosID + "]=null,没有库位不能执行出入库");
return false;
}
ShelfPosition sp = CSVPositionReader<ShelfPosition>.GetPositon(param.ShelfPosID);
if (sp == null && needCheckShelf)
{
LogUtil.error(box.Name + "GetPositon[" + param.ShelfPosID + "]=null,没有库位不能执行出入库");
return false;
}
if (String.IsNullOrEmpty(param.PlateH))
{
param.PlateH = position.BagHigh.ToString();
......@@ -209,13 +186,7 @@ namespace OnlineStore.DeviceLibrary
{
param.PlateW = position.BagWidth.ToString();
}
if (sp != null)
{
p.InOut_P101 = sp.InoutAxis_P101;
p.UpDown_LP101 = sp.UpDownAxis_LP101;
p.UpDown_HP102 = sp.UpDownAxis_HP102;
p.Middle_P101 = sp.MiddleAxis_P101;
}
p.ComPress_P1 = box.Config.CompAxis_P1_Position;
p.InOut_P1 = box.Config.InOutAxis_P1_Position;
......
......@@ -153,21 +153,7 @@ namespace OnlineStore.DeviceLibrary
return "["+rfid+"][" + PosID + "][" + ShelfPosID + "][" + PlateW + "x" + PlateH + "][" + WareCode + "]";
}
internal void UpdateShelfPosId(string pId)
{
this.ShelfPosID = pId;
ShelfPosition sp = CSVPositionReader<ShelfPosition>.GetPositon(ShelfPosID);
if (sp == null)
{
LogUtil.error( "GetPositon[" + ShelfPosID + "]=null,没有库位不能执行出入库");
}
MoveP.InOut_P101 = sp.InoutAxis_P101;
MoveP.UpDown_LP101 = sp.UpDownAxis_LP101;
MoveP.UpDown_HP102 = sp.UpDownAxis_HP102;
MoveP.Middle_P101 = sp.MiddleAxis_P101;
}
internal void UpdatePosId(string message, string posId, string plateW, string plateH,int comP2,int comP3)
{
......@@ -198,16 +184,8 @@ namespace OnlineStore.DeviceLibrary
WareCode = wareNo;
this.ShelfPosID = ShelfPosID;
this.rfid = rfid;
ShelfPosition sp = CSVPositionReader<ShelfPosition>.GetPositon(ShelfPosID);
if (sp == null)
{
LogUtil.error("GetPositon[" + ShelfPosID + "]=null,没有库位不能执行出入库");
}
MoveP.InOut_P101 = sp.InoutAxis_P101;
MoveP.UpDown_LP101 = sp.UpDownAxis_LP101;
MoveP.UpDown_HP102 = sp.UpDownAxis_HP102;
MoveP.Middle_P101 = sp.MiddleAxis_P101;
PosID = posId;
ACBoxPosition position = CSVPositionReader<ACBoxPosition>.GetPositon(posId);
......
......@@ -33,7 +33,7 @@ namespace OnlineStore.DeviceLibrary
/// </summary>
protected static Color storeMoveColor = Color.Blue;
public string Name { get; set; }
public int DeviceID { get; set; }
public int ID { get; set; }
public BaseConfig baseConfig = null;
/// <summary>
/// 料仓状态
......@@ -196,7 +196,7 @@ namespace OnlineStore.DeviceLibrary
break;
default: break;
}
alarmInfo = new AlarmInfo(DeviceID, aType, alarmDetial, alarmMsg, inoutStatus);
alarmInfo = new AlarmInfo(ID, aType, alarmDetial, alarmMsg, inoutStatus);
}
/// <summary>
......@@ -325,7 +325,7 @@ namespace OnlineStore.DeviceLibrary
{
if (!isInit)
{
MoveInfo = new StoreMoveInfo(DeviceID);
MoveInfo = new StoreMoveInfo(ID);
mainTimer = new System.Timers.Timer();
mainTimer.Enabled = false;
......
......@@ -461,9 +461,9 @@ namespace OnlineStore.DeviceLibrary
/// <summary>
/// 送出空料架:关闭仓门
/// </summary>
BS_11_CloseDoor = 4011,
BS_11_CloseDoor = 4011,
#endregion
}
public enum StoreAlarmType
......
......@@ -163,7 +163,7 @@ namespace OnlineStore.DeviceLibrary
currShelfPosId = ShelfPositionList[currShelfIndex];
//设置取料位置
MoveParam.UpdateShelfPosId(currShelfPosId);
// MoveParam.UpdateShelfPosId(currShelfPosId);
return true;
}
/// <summary>
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OnlineStore.LoadCSVLibrary
{
public class CSVResourceControl
{
public delegate string GetStrDelegate(string id, string defaultStr);
public static event GetStrDelegate GetStrEvent;
public delegate string GetStringDelegate(string id, string defaultStr, params object[] param);
public static event GetStringDelegate GetStringEvent;
public static string GetString(string id, string defaultStr)
{
string result = GetStrEvent?.Invoke(id, defaultStr);
return result;
}
public static string GetString(string id, string defaultStr, params object[] param)
{
string result = GetStringEvent?.Invoke(id, defaultStr, param);
return result;
}
}
}
......@@ -48,7 +48,6 @@
<ItemGroup>
<Compile Include="CSVExection.cs" />
<Compile Include="position\ACBoxPosition.cs" />
<Compile Include="position\ShelfPosition.cs" />
<Compile Include="storeConfig\ConfigItemBase.cs" />
<Compile Include="storeConfig\ConfigProAttribute.cs" />
<Compile Include="storeConfig\config\Store_Config.cs" />
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OnlineStore.LoadCSVLibrary
{
public class ShelfPosition:PostionBase
{
/// <summary>
/// 旋转轴位置P101
/// </summary>
[CSVAttribute("旋转轴位置P101")]
public int MiddleAxis_P101{ get; set; }
/// <summary>
/// 升降轴低点P101
/// </summary>
[CSVAttribute("升降轴低点P101")]
public int UpDownAxis_LP101 { get; set; }
/// <summary>
/// 升降轴高点P102
/// /// </summary>
[CSVAttribute("升降轴高点P102")]
public int UpDownAxis_HP102{ get; set; }
/// <summary>
/// 进出轴位置P101
/// </summary>
[CSVAttribute("进出轴位置P101")]
public int InoutAxis_P101 { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OnlineStore.LoadCSVLibrary
{
public class StorePostionBase
{
/// <summary>
/// 位置
/// </summary>
[CSVAttribute("位置", "PositionNum")]
public string PositionNum { get; set; }
/// <summary>
/// 优先级
/// </summary>
[CSVAttribute("优先级","Priority")]
public int Priority { get; set; }
/// <summary>
/// 高
/// </summary>
[CSVAttribute("高度", "Height")]
public int BagHeight { get; set; }
/// <summary>
/// 宽
/// </summary>
[CSVAttribute("宽度", "Width")]
public int BagWidth { get; set; }
}
}
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!