2026-03-30 17:09:29 +09:00

154 lines
4.8 KiB
C#

using Hermes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VisionLight {
/// <summary>
/// [스트로브 조명 컨트롤러 - 8CH / 시리얼 통신][Series]
/// Range : (0,999) us
/// Baudrate : 19200 bps
/// DataBit : 8 bit
/// StopBit : 1 bit
/// ParityBit : None
/// </summary>
internal class Light_LVS_ES08 : LightBase_Serial {
public const byte CMD_START_CODE = 0x01;
public const byte CMD_END_CODE = 0x04;
public const byte CMD_OP_CODE_WRITE = 0x00;
public const byte LENGTH_DATA_MSG = 0x01;
public const byte LENGTH_LEVEL_DATA_MSG = 0x02;
public const byte ADDR_PAGE_SELECT = 0x3C;
public const byte ADDR_CHANNEL_SELECT = 0x20;
public const byte ADDR_STEP_VALUE = 0x28;
public const byte LENGTH_SET_PAGE_REQUEST_MSG = 6;
public const byte LENGTH_SET_CHANNEL_REQUEST_MSG = 6;
public const byte LENGTH_SET_LEVEL_REQUEST_MSG = 7;
public Light_LVS_ES08(string deviceId) : base(deviceId) {
}
protected override eLightResult SetOnImpl(int channel) {
SetLevelImpl(LastChLevels[channel], channel);
return eLightResult.OK;
}
protected override eLightResult SetOffImpl(int channel) {
SetLevelImpl(0, channel);
return eLightResult.OK;
}
protected override eLightResult SetLevelImpl(int level, int channel) {
try {
lock (LockThis) {
if (PageSelect(1) == E1.OK) {
//page의 경우 일단 1로 설정
if (ChannelSelect(channel) == E1.OK) {
if (SendData_SetLevel(level) == E1.OK) {
return eLightResult.OK;
}
else {
return eLightResult.NG;
}
}
else {
return eLightResult.NG;
}
}
else {
return eLightResult.NG;
}
}
}
catch (Exception e) {
LOG.FuncE(e.Message);
}
return eLightResult.NG;
}
private int PageSelect(int page) {
try {
byte[] sendData = CreatePageSelectReqMsg(page);
SerialConn.Write(sendData, sendData.Length);
return E1.OK;
}
catch (Exception e) {
LOG.FuncE(e.Message);
}
return E1.GENERIC;
}
private byte[] CreatePageSelectReqMsg(int page) {
byte[] requestMsg = new byte[LENGTH_SET_PAGE_REQUEST_MSG];
requestMsg[0] = CMD_START_CODE;
requestMsg[1] = CMD_OP_CODE_WRITE;
requestMsg[2] = LENGTH_DATA_MSG;
requestMsg[3] = ADDR_PAGE_SELECT;
requestMsg[4] = (byte)page;
requestMsg[5] = CMD_END_CODE;
return requestMsg;
}
private int ChannelSelect(int channel) {
try {
byte[] sendData = CreateChannelSelectReqMsg(channel);
SerialConn.Write(sendData, sendData.Length);
return E1.OK;
}
catch (Exception e) {
LOG.FuncE(e.Message);
}
return E1.GENERIC;
}
private byte[] CreateChannelSelectReqMsg(int channel) {
byte[] requestMsg = new byte[LENGTH_SET_CHANNEL_REQUEST_MSG];
requestMsg[0] = CMD_START_CODE;
requestMsg[1] = CMD_OP_CODE_WRITE;
requestMsg[2] = LENGTH_DATA_MSG;
requestMsg[3] = ADDR_CHANNEL_SELECT;
requestMsg[4] = (byte)channel;
requestMsg[5] = CMD_END_CODE;
return requestMsg;
}
private int SendData_SetLevel(int level) {
try {
byte[] sendData = CreateSetLevelReqMsg(level);
SerialConn.Write(sendData, sendData.Length);
return E1.OK;
}
catch (Exception e) {
LOG.FuncE(e.Message);
}
return E1.GENERIC;
}
private byte[] CreateSetLevelReqMsg(int level) {
byte[] requestMsg = new byte[LENGTH_SET_LEVEL_REQUEST_MSG];
requestMsg[0] = CMD_START_CODE;
requestMsg[1] = CMD_OP_CODE_WRITE;
requestMsg[2] = LENGTH_LEVEL_DATA_MSG;
requestMsg[3] = ADDR_STEP_VALUE;
requestMsg[4] = (byte)(level % 256);
requestMsg[5] = (byte)(level / 256);
requestMsg[6] = CMD_END_CODE;
return requestMsg;
}
}
}