From 1f6dbc868446767788a1c11442acf8a5b78aa7f0 Mon Sep 17 00:00:00 2001 From: lhiven Date: Sun, 3 Dec 2023 17:11:33 +0900 Subject: [PATCH] =?UTF-8?q?=E9=95=AD=E5=B0=84=E5=A4=B4=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AuxiliaryEquipment/LaserEquipment.cs | 70 +++++++++++++++++-- Rs.SkyLine/Flow/AlarmConstID.cs | 4 ++ Rs.SkyLine/Program.cs | 2 + 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/Rs.SkyLine/AuxiliaryEquipment/LaserEquipment.cs b/Rs.SkyLine/AuxiliaryEquipment/LaserEquipment.cs index 4af13c1..c672ed2 100644 --- a/Rs.SkyLine/AuxiliaryEquipment/LaserEquipment.cs +++ b/Rs.SkyLine/AuxiliaryEquipment/LaserEquipment.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO.Ports; using System.Linq; using System.Text; @@ -13,8 +14,19 @@ namespace Rs.MotionPlat.AuxiliaryEquipment /// public class LaserEquipment { + List bufferList = new List(); SerialPort serialPort; const string regStr = @"[+-]\d{5}"; + + public LaserEquipment() + { + serialPort = new SerialPort(); + serialPort.PortName = "COM1"; + serialPort.Parity = Parity.None; + serialPort.StopBits = StopBits.One; + serialPort.DataBits = 8; + serialPort.BaudRate = 38400; + } /// /// 是否已经连接上设备 /// @@ -22,22 +34,72 @@ namespace Rs.MotionPlat.AuxiliaryEquipment public bool Open() { - return false; + try + { + if (serialPort != null && serialPort.IsOpen) + { + serialPort.Close(); + } + IsConnected = true; + serialPort.Open(); + return true; + } + catch (Exception ex) + { + IsConnected = false; + return false; + } } public double Read() { + bool readOk = false; double val = -999.999; if (IsConnected) { serialPort.Write("%01#RMD**\r"); - string value=serialPort.ReadLine(); - if(Regex.IsMatch(value, regStr)) + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + while (!readOk && stopwatch.ElapsedMilliseconds<3000) { - + int datalen = serialPort.BytesToRead; + if (datalen>0) + { + byte[] buffer=new byte[datalen]; + serialPort.Read(buffer, 0, datalen); + foreach (byte b in buffer) + { + if(b!=13) + { + bufferList.Add(b); + } + else + { + bufferList.Add((byte)b); + readOk = true; + return ParseValue(); + } + } + } } } return val; } + + + double ParseValue() + { + string val = Encoding.ASCII.GetString(bufferList.ToArray()); + + Regex reg = new Regex("[+-][^*]*"); + if(reg.IsMatch(val)) + { + string v1 = reg.Match(val).Value; + double v2 = double.Parse(v1)/10000; + return v2; + } + bufferList.Clear(); + return -999.999; + } } } diff --git a/Rs.SkyLine/Flow/AlarmConstID.cs b/Rs.SkyLine/Flow/AlarmConstID.cs index 655bbe6..3879a35 100644 --- a/Rs.SkyLine/Flow/AlarmConstID.cs +++ b/Rs.SkyLine/Flow/AlarmConstID.cs @@ -11,6 +11,10 @@ namespace Rs.MotionPlat.Flow /// public class AlarmConstID { + /// + /// 读取数据NG + /// + public const double NG = -999.999; /// /// Tray盘取料失败报警 /// diff --git a/Rs.SkyLine/Program.cs b/Rs.SkyLine/Program.cs index b5bc986..d18a8eb 100644 --- a/Rs.SkyLine/Program.cs +++ b/Rs.SkyLine/Program.cs @@ -1,5 +1,6 @@ using Newtonsoft.Json; using Rs.Framework; +using Rs.MotionPlat.AuxiliaryEquipment; using Rs.MotionPlat.Commom; using Rs.MotionPlat.Flow; using System; @@ -22,6 +23,7 @@ namespace Rs.MotionPlat Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new FormIO2()); + Application.Run(new Home()); } }