Commit 299e1672 authored by JiangYixing's avatar JiangYixing

Upload New File

parent e8c4b816
Pipeline #118 canceled with stages
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using Newtonsoft.Json;
using System.IO;
using Jtext103.CFET2.Core;
using Jtext103.CFET2.Core.Attributes;
using Jtext103.CFET2.Core.Log;
using Jtext103.CFET2.Core.Event;
using HslCommunication.Serial;
using System.Threading;
using System.Threading.Tasks;
namespace Cfet2Vacuun
{
public class Vacuum : Thing
{
public ComConfig comconfig { get; set; }
public SerialPort port { get; set; }
public uint PresureValue;//压强数值4
public uint NegetiveExpValue;//压强负指数值
public double RealPressureValue;
public bool PortRunningNormal = false;
public Thread QueryThread;
[Cfet2Status]
public uint GetPresureValue()
{
return PresureValue;
}
[Cfet2Status]
public uint GetNegetiveExpValue()
{
return NegetiveExpValue;
}
public override void TryInit(object path)
{
comconfig = new ComConfig(getConfigFilePath((string)path));
}
public override void Start()
{
InitSerialPort();
PortRunningNormal = true;
QueryThread = new Thread(RollPollingQuery);
QueryThread.Start();
}
public void RollPollingQuery()
{
while (true)
{
try
{
Thread.Sleep(2000);
Query();
PortRunningNormal = true;
}
catch (Exception e)//如果报错了
{
PortRunningNormal = false;
continue;
}
}
}
public void InitSerialPort()
{
port = new SerialPort(comconfig.com)
{
BaudRate = comconfig.baudrate,
Parity = comconfig.parity,
StopBits = comconfig.stopbits,
DataBits = comconfig.databits,
Handshake = comconfig.handshake,
//不加这个有些带串口的板子用不了,但是有时候加了下面这一句又可能导致一些神秘的问题。
RtsEnable = true
};
port.DataReceived += new SerialDataReceivedEventHandler(DataReceived);
port.Open();
}
public void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int n = port.BytesToRead;
byte[] RecData = new byte[n];
port.Read(RecData, 0, n);
double newvalue;
if (SoftCRC16.CheckCRC16(RecData))
{
PresureValue = (uint)(RecData[3] * 256 + RecData[4]);
NegetiveExpValue = (uint)(RecData[5] * 256 + RecData[6]);
newvalue = (PresureValue / 10) * Math.Pow(10, -NegetiveExpValue);
if (newvalue != RealPressureValue)
{
VacuumChangedPayload payload = new VacuumChangedPayload();
payload.NewValue = newvalue;
//产生一个事件,表示真空值发生了改变
MyHub.EventHub.Publish(Path, "VacuumChanged", payload);
}
RealPressureValue = newvalue;
}
}
[Cfet2Method]
public void Query()
{
byte[] QueryCmd = new byte[] { 0x07, 0x04, 0x00, 0x00, 0x00, 0x02 };
byte[] QueryCmdAddCRC = SoftCRC16.CRC16(QueryCmd);
port.Write(QueryCmdAddCRC, 0, QueryCmdAddCRC.Length);
}
[Cfet2Status]
public double VaccuumValue()
{
return RealPressureValue;
}
[Cfet2Status]
public bool PortState()
{
return PortRunningNormal;
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment