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
///
/// 开启服务
///
///
public void StartServer()
{
//初始化动态库
LedZKSDK.InitSdk(2, 2);
_handle = LedZKSDK.CreateClient(_ip, _port, LedZKSDK.bx_5k_card_type.BX_6K1, 3, 1, null);
ReadScreen();
}
///
/// 停止服务
///
///
public void StopServer()
{
LedZKSDK.Destroy(_handle);
//释放动态库
LedZKSDK.ReleaseSdk();
}
#endregion Start & Stop Server
#region Util Methods
///
/// 4x6 或 5x8
/// \C1代表红色,\C2代表绿色,\C3代表黄色
///
///
///
///
///
///
///
/// 0x01:单行,0x02:多行
/// 0x01:手动换行,0x02:自动换行
///
///
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);
}
///
/// 4x6 或 5x8
/// \C1代表红色,\C2代表绿色,\C3代表黄色
///
///
///
///
///
///
///
///
/// 0x01:单行,0x02:多行
/// 0x01:手动换行,0x02:自动换行
///
///
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;
}
///
///
///
/// 当前亮度,强制调节亮度时有效,亮度值为0-15共16级。亮度值为15时亮度最高
///
public int ForceSetBrightness(byte lightValue)
{
lightValue %= 16;
return LedZKSDK.SCREEN_SetBrightness(_handle, 1, lightValue, null);
}
///
/// 获取屏号
///
///
///
public int GetScreenId(ref ushort screenId)
{
return LedZKSDK.CON_ReadScreenID(_handle, ref screenId);
}
///
/// 设置屏号
///
///
///
public int SetScreenId(ushort screenid)
{
return LedZKSDK.CON_SetScreenID(_handle, screenid);
}
///
/// 强制开机
///
///
public int ForceTurnOn()
{
return LedZKSDK.SCREEN_ForceOnOff(_handle, 1);
}
///
/// 强制关机
///
///
public int ForceTurnOff()
{
return LedZKSDK.SCREEN_ForceOnOff(_handle, 2);
}
///
/// 若ping命令执行成功,说明与控制卡通信成功
///
///
public int Ping()
{
return LedZKSDK.CON_PING(_handle); ;
}
public bool IsConnected()
{
return Ping() == 0;
}
#endregion Common Methods
}
}