You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
5.2 KiB

3 years ago
using System.Text;
namespace EC.AutoWeightServer.IfManager.LedZK
{
public class LedZKManager
{
private byte[] _ip;
private ushort _port;
private uint _handle;
private byte _width;
private byte _height;
public LedZKManager()
{
_ip = Encoding.Default.GetBytes("192.168.0.199");
_port = 5005;
}
public LedZKManager(string ip, ushort port)
{
_ip = Encoding.Default.GetBytes(ip);
_port = port;
}
#region Start & Stop Server
/// <summary>
/// 开启服务
/// </summary>
/// <returns></returns>
public void StartServer()
{
//初始化动态库
LedZKSDK.InitSdk(2, 2);
_handle = LedZKSDK.CreateClient(_ip, _port, LedZKSDK.bx_5k_card_type.BX_6K1, 3, 1, null);
ReadScreen();
}
/// <summary>
/// 停止服务
/// </summary>
/// <returns></returns>
public void StopServer()
{
LedZKSDK.Destroy(_handle);
//释放动态库
LedZKSDK.ReleaseSdk();
}
#endregion Start & Stop Server
#region Util Methods
/// <summary>
/// 4x6 或 5x8
/// \C1代表红色,\C2代表绿色,\C3代表黄色
/// </summary>
/// <param name="areaId"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="text"></param>
/// <param name="singleLine">0x01:单行,0x02:多行</param>
/// <param name="newLine">0x01:手动换行,0x02:自动换行</param>
/// <param name="speed"></param>
/// <returns></returns>
public int SendDynamicArea(byte areaId, ushort x, ushort y, ushort width, ushort height, string text,
byte singleLine, byte newLine, byte speed)
{
var outText = Encoding.Default.GetBytes(text);
var header = new LedZKSDK.bx_5k_area_header
{
DynamicAreaLoc = areaId,
AreaType = 0x00,
AreaX = x,
AreaWidth = width,
AreaY = y,
AreaHeight = height,
RunMode = 0,
Timeout = 2,
SingleLine = singleLine,
NewLine = newLine,
Lines_sizes = 0,
DisplayMode = 0x01,
Speed = speed,
StayTime = (byte)(35 * 2),
Reserved1 = 0,
Reserved2 = 0,
Reserved3 = 0,
ExitMode = 0x00,
DataLen = outText.Length
};
return LedZKSDK.SCREEN_SendDynamicArea(_handle, header, (ushort)outText.Length, outText);
}
/// <summary>
/// 4x6 或 5x8
/// \C1代表红色,\C2代表绿色,\C3代表黄色
/// </summary>
/// </summary>
/// <param name="areaId"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <param name="text"></param>
/// <param name="singleLine">0x01:单行,0x02:多行</param>
/// <param name="newLine">0x01:手动换行,0x02:自动换行</param>
/// <param name="speed"></param>
/// <returns></returns>
public int SendDynamicMoveArea(byte areaId, ushort x, ushort y, ushort width, ushort height, string text,
byte singleLine, byte newLine, byte speed)
{
var outText = Encoding.Default.GetBytes(text);
var header = new LedZKSDK.bx_5k_area_header
{
DynamicAreaLoc = areaId,
AreaType = 0x00,
AreaX = x,
AreaWidth = width,
AreaY = y,
AreaHeight = height,
RunMode = 0,
Timeout = 2,
SingleLine = singleLine,
NewLine = newLine,
Lines_sizes = 0,
DisplayMode = 0x03,
Speed = speed,
StayTime = (byte)(35 * 2),
Reserved1 = 0,
Reserved2 = 0,
Reserved3 = 0,
ExitMode = 0x00,
DataLen = outText.Length
};
return LedZKSDK.SCREEN_SendDynamicArea(_handle, header, (ushort)outText.Length, outText);
}
public int DelDynamicArea(byte areaId)
{
return LedZKSDK.SCREEN_DelDynamicArea(_handle, areaId);
}
#endregion Util Methods
#region Common Methods
public int ReadScreen()
{
var status = new byte[24];
ushort len = 0;
var ret = LedZKSDK.CON_ReadScreen(_handle, status, ref len);
_width = status[4];
_height = status[6];
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="lightValue">当前亮度,强制调节亮度时有效,亮度值为0-15共16级。亮度值为15时亮度最高</param>
/// <returns></returns>
public int ForceSetBrightness(byte lightValue)
{
lightValue %= 16;
return LedZKSDK.SCREEN_SetBrightness(_handle, 1, lightValue, null);
}
/// <summary>
/// 获取屏号
/// </summary>
/// <param name="screenId"></param>
/// <returns></returns>
public int GetScreenId(ref ushort screenId)
{
return LedZKSDK.CON_ReadScreenID(_handle, ref screenId);
}
/// <summary>
/// 设置屏号
/// </summary>
/// <param name="screenid"></param>
/// <returns></returns>
public int SetScreenId(ushort screenid)
{
return LedZKSDK.CON_SetScreenID(_handle, screenid);
}
/// <summary>
/// 强制开机
/// </summary>
/// <returns></returns>
public int ForceTurnOn()
{
return LedZKSDK.SCREEN_ForceOnOff(_handle, 1);
}
/// <summary>
/// 强制关机
/// </summary>
/// <returns></returns>
public int ForceTurnOff()
{
return LedZKSDK.SCREEN_ForceOnOff(_handle, 2);
}
/// <summary>
/// 若ping命令执行成功,说明与控制卡通信成功
/// </summary>
/// <returns></returns>
public int Ping()
{
return LedZKSDK.CON_PING(_handle); ;
}
public bool IsConnected()
{
return Ping() == 0;
}
#endregion Common Methods
}
}