From 17cfa5bcca56a78ec2d8c12b07824e620a41d2db Mon Sep 17 00:00:00 2001 From: "nodyang@aliyun.com" Date: Mon, 14 Oct 2024 16:34:20 +0800 Subject: [PATCH] 12 --- DB/DB.csproj | 8 ++-- ...{Form1.Designer.cs => FrmMain.Designer.cs} | 28 ++++--------- RfidWeb/{Form1.cs => FrmMain.cs} | 42 ++++++++++++++++--- RfidWeb/{Form1.resx => FrmMain.resx} | 0 RfidWeb/Program.cs | 4 +- RfidWeb/RfidWeb.csproj | 18 ++++---- Tool/HttpUtil.cs | 10 +++-- Tool/PlcConnect.cs | 21 +++++++--- Tool/RfidSetting.cs | 2 +- 9 files changed, 84 insertions(+), 49 deletions(-) rename RfidWeb/{Form1.Designer.cs => FrmMain.Designer.cs} (59%) rename RfidWeb/{Form1.cs => FrmMain.cs} (63%) rename RfidWeb/{Form1.resx => FrmMain.resx} (100%) diff --git a/DB/DB.csproj b/DB/DB.csproj index a2ec0cd..b01ebc1 100644 --- a/DB/DB.csproj +++ b/DB/DB.csproj @@ -47,13 +47,13 @@ 1.1.1 - 8.0.1 + 8.0.2 - 8.0.1 + 8.0.2 - 8.0.4 + 8.0.5 @@ -80,7 +80,7 @@ 8.0.0 - 8.0.4 + 8.0.5 8.0.0 diff --git a/RfidWeb/Form1.Designer.cs b/RfidWeb/FrmMain.Designer.cs similarity index 59% rename from RfidWeb/Form1.Designer.cs rename to RfidWeb/FrmMain.Designer.cs index 3088e88..7dfbb27 100644 --- a/RfidWeb/Form1.Designer.cs +++ b/RfidWeb/FrmMain.Designer.cs @@ -1,6 +1,6 @@ namespace RfidWeb { - partial class Form1 + partial class FrmMain { /// /// 必需的设计器变量。 @@ -28,31 +28,19 @@ /// private void InitializeComponent() { - this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // - // button1 + // FrmMain // - this.button1.Location = new System.Drawing.Point(662, 241); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(137, 97); - this.button1.TabIndex = 0; - this.button1.Text = "初始化数据库"; - this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.button1_Click); - // - // Form1 - // - this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F); + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1770, 830); - this.Controls.Add(this.button1); - this.Name = "Form1"; - this.Text = "Form1"; + this.ClientSize = new System.Drawing.Size(1180, 553); + this.Margin = new System.Windows.Forms.Padding(2); + this.Name = "FrmMain"; + this.Text = "主程序"; this.ResumeLayout(false); - } - private System.Windows.Forms.Button button1; + } #endregion } diff --git a/RfidWeb/Form1.cs b/RfidWeb/FrmMain.cs similarity index 63% rename from RfidWeb/Form1.cs rename to RfidWeb/FrmMain.cs index 48cece7..b24b9a6 100644 --- a/RfidWeb/Form1.cs +++ b/RfidWeb/FrmMain.cs @@ -16,20 +16,21 @@ using DB.Entity; using NewLife.Data; using NewLife.Log; using Tool; +using HslCommunication; namespace RfidWeb { - public partial class Form1 : Form + public partial class FrmMain : Form { private ILogNet logNet = ILogNetFactory.GetLogNet; - public Form1() + public FrmMain() { - AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); - AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + InitializeComponent(); + // 如果所有的日志在记录之前需要在控制台显示出来 logNet.BeforeSaveToFile += (object sender, HslEventArgs e) => @@ -38,7 +39,38 @@ namespace RfidWeb }; logNet.ConsoleOutput = true; logNet.WriteInfo("nihao"); - + Init(); + } + + + + private void Init() + { + //if (this.FormBorderStyle == FormBorderStyle.None) + //{ + // this.FormBorderStyle = FormBorderStyle.FixedSingle; + // this.WindowState = FormWindowState.Normal; + //} + //else + //{ + // this.FormBorderStyle = FormBorderStyle.None; + // this.WindowState = FormWindowState.Maximized; + //} + + + + // 当前写入操作的代码 The code for the current write operation + OperateResult write = PlcConnect.Instance.Write("A1", short.Parse("4")); + if (write.IsSuccess) + { + Console.WriteLine("Write [A1] success"); + } + else + { + Console.WriteLine("Write [A1] failed: " + write.Message); + } + + } private void button1_Click(object sender, EventArgs e) diff --git a/RfidWeb/Form1.resx b/RfidWeb/FrmMain.resx similarity index 100% rename from RfidWeb/Form1.resx rename to RfidWeb/FrmMain.resx diff --git a/RfidWeb/Program.cs b/RfidWeb/Program.cs index 1f840ba..5762ed1 100644 --- a/RfidWeb/Program.cs +++ b/RfidWeb/Program.cs @@ -14,9 +14,11 @@ namespace RfidWeb [STAThread] static void Main() { + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new Form1()); + Application.Run(new FrmMain()); } } } diff --git a/RfidWeb/RfidWeb.csproj b/RfidWeb/RfidWeb.csproj index 9e037f4..15a1840 100644 --- a/RfidWeb/RfidWeb.csproj +++ b/RfidWeb/RfidWeb.csproj @@ -56,17 +56,17 @@ 1.1.1 - 8.0.1 + 8.0.2 - 8.0.1 + 8.0.2 13.0.3 - 8.0.4 + 8.0.5 @@ -95,7 +95,7 @@ 8.0.0 - 8.0.4 + 8.0.5 8.0.0 @@ -118,16 +118,16 @@ - + Form - - Form1.cs + + FrmMain.cs - - Form1.cs + + FrmMain.cs ResXFileCodeGenerator diff --git a/Tool/HttpUtil.cs b/Tool/HttpUtil.cs index e6aaaf0..0b2edb2 100644 --- a/Tool/HttpUtil.cs +++ b/Tool/HttpUtil.cs @@ -1,4 +1,6 @@ using System; +using System.Net.Http; +using NewLife.Http; using RestSharp; namespace Tool @@ -13,7 +15,7 @@ namespace Tool /// /// /// - public static string PostJson(string url, object obj) + public static (bool b,string msg) PostJson(string url, object obj) { try { @@ -23,13 +25,15 @@ namespace Tool request.Method = Method.Post; request.Timeout = TimeSpan.FromMinutes(2); request.AddBody(obj, ContentType.Json); + + var response = client.Execute(request); - return response.Content; + return (response.IsSuccessful, response.IsSuccessful ? response.Content : response.ErrorMessage); } catch (Exception e) { ILogNetFactory.GetLogNet.WriteError(e.Message); - return e.Message; + return (false, e.Message); } } } diff --git a/Tool/PlcConnect.cs b/Tool/PlcConnect.cs index 0c10c39..e043d8f 100644 --- a/Tool/PlcConnect.cs +++ b/Tool/PlcConnect.cs @@ -21,16 +21,25 @@ namespace Tool private AllenBradleyNet CreateAb() { var rfidSetting = RfidSetting.Current; - AllenBradleyNet ab = new AllenBradleyNet(); - ab.IpAddress = rfidSetting.PlcIp; - ab.Port = rfidSetting.Port; - ab.ConnectServer(); - return ab; + AllenBradleyNet plc = new AllenBradleyNet(); + plc.Slot = 0; + plc.CommunicationPipe = new HslCommunication.Core.Pipe.PipeTcpNet(rfidSetting.PlcIp, 44818) + { + ConnectTimeOut = 5000, // 连接超时时间,单位毫秒 + ReceiveTimeOut = 10000, // 接收设备数据反馈的超时时间 + SleepTime = 0, + SocketKeepAliveTime = -1, + IsPersistentConnection = true, + }; + + return plc; } - public OperateResult Write(string db, string value) + + + public OperateResult Write(string db, string value) { return Instance.Write(db, value); } diff --git a/Tool/RfidSetting.cs b/Tool/RfidSetting.cs index 7cf8d66..fe725aa 100644 --- a/Tool/RfidSetting.cs +++ b/Tool/RfidSetting.cs @@ -16,7 +16,7 @@ namespace Tool public string Db { get; set; } = "server=127.0.0.1;database=gaosu;uid=postgres;pwd=123456"; - public string PlcIp { get; set; } = "127.0.0.1"; + public string PlcIp { get; set; } = "192.168.1.100"; public int Port { get; set; }=44818;