123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Text;
- using Aitex.Core.RT.Log;
- using MECF.Framework.Common.Communications;
- namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.VCE.SiasunVCE
- {
- public class SiasunVCEMessage : AsciiMessage
- {
- public string Data { get; set; }
- public string ErrorText { get; set; }
- public bool IsRDY { get; set; }
- }
- public class SiasunVCEConnection : SerialPortConnectionBase
- {
- //private bool _isBKGPlusMode = true;//should read from config file
- private string _cachedBuffer = string.Empty;
- public SiasunVCEConnection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
- : base(portName, baudRate, dataBits, parity, stopBits, "\r", true)
- {
- }
- public override bool SendMessage(byte[] message)
- {
- return base.SendMessage(message);
- }
- //1) Format: id,X,category,data<LF><CR>
- //2) request response
- //00,R,ARM,R,EX
- //00,X,ARM,R,EX,0169500
- //3) action response
- //S,BUFFER,NS,2
- //_RDY
- //_BKGRDY
- //A,DO
- //_RDY
- //_BKGERR A6
- //BAD,COMMAND
- //_ERR S4
- //_RDY
- protected override MessageBase ParseResponse(string rawText)
- {
- SiasunVCEMessage msg = new SiasunVCEMessage();
- msg.RawMessage = rawText;
- if (rawText.Length <= 0)
- {
- LOG.Error($"empty response,");
- msg.IsFormatError = true;
- return msg;
- }
- rawText = rawText.Replace("\r", "").Replace("\n", "");
- msg.MessagePart = rawText.Split(',');
- if (msg.MessagePart.Length < 2)
- {
- LOG.Error($"too short response,");
- msg.IsFormatError = true;
- return msg;
- }
- if (msg.MessagePart[0] == "M")
- {
- if (msg.MessagePart[1] == "OK")
- {
- msg.Data = rawText;
- msg.IsComplete = true;
- }
- else
- {
- msg.Data = msg.MessagePart[1];
- msg.IsError = true;
- }
- }
- else if (msg.MessagePart[0] == "X")
- {
- if (msg.MessagePart.Length > 2)
- msg.Data = string.Join(",", msg.MessagePart, 1, msg.MessagePart.Length - 1);
- else
- msg.Data = msg.MessagePart[1];
- msg.IsComplete = true;
- }
- else if (msg.MessagePart[0] == "H")
- {
- if (msg.MessagePart.Length != 3)
- {
- msg.IsError = true;
- }
- else
- {
- if (msg.MessagePart[2] == "OK")
- {
- msg.Data = rawText;
- msg.IsComplete = true;
- }
- else
- {
- msg.IsError = true;
- }
- }
- }
- return msg;
- }
- }
- }
|