镭射头数据读取

master
lhiven 2 years ago
parent 6bdb8552eb
commit 1f6dbc8684

@ -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
/// </summary>
public class LaserEquipment
{
List<byte> bufferList = new List<byte>();
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;
}
/// <summary>
/// 是否已经连接上设备
/// </summary>
@ -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;
}
}
}

@ -11,6 +11,10 @@ namespace Rs.MotionPlat.Flow
/// </summary>
public class AlarmConstID
{
/// <summary>
/// 读取数据NG
/// </summary>
public const double NG = -999.999;
/// <summary>
/// Tray盘取料失败报警
/// </summary>

@ -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());
}
}

Loading…
Cancel
Save