C#调用LEDSender.dll进行LED开发
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C#调用LEDSender.dll进行LED开发
首先对ledsender.dll声明
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Led
{
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct DeviceParam
{
public int devType;
public int Speed;
public int ComPort;
public int FlowCon;
public int locPort;
public int rmtPort;
public int memory;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]
public byte[] Phone;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 5, ArraySubType = UnmanagedType.U4)]
public int[] Reserved;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct TNotifyMessage
{
[MarshalAs(UnmanagedType.I4)]
public Int32 Message;
[MarshalAs(UnmanagedType.I4)]
public Int32 Command;
[MarshalAs(UnmanagedType.I4)]
public Int32 Result;
[MarshalAs(UnmanagedType.I4)]
public Int32 Status;
[MarshalAs(UnmanagedType.I4)]
public Int32 Address;
[MarshalAs(UnmanagedType.I4)]
public Int32 Size;
[MarshalAsAttribute(UnmanagedType.LPStr)]
public string Buffer;
public DeviceParam param;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 16)]
public string Host;
[MarshalAs(UnmanagedType.I4)]
public Int32 Port;
}
public struct Rect
{
public Int32 left;
public Int32 top;
public Int32 right;
public Int32 bottom;
};
public struct TimeStamp
{
Int32 date;
long time;
};
public static class API
{
public const int GWL_WNDPROC = (-4);
public const int WM_USER = 1024;
public const int WM_LED_NOTIFY = WM_USER + 1;
//显示屏类型
public const int FONT_SET_16 = 0; //16点阵字符
public const int FONT_SET_24 = 1; //24点阵字符
public const int PKC_QUERY = 4;
public const int PKC_ADJUST_TIME = 6;
public const int PKC_GET_POWER = 9;
public const int PKC_SET_POWER = 10;
public const int PKC_GET_BRIGHT = 11;
public const int PKC_SET_BRIGHT = 12;
//通讯方式
public const int DEV_COM = 0; //串口通讯
public const int DEV_UDP = 1; //UDP通讯
public const int DEV_MODEM = 2; //Modem通讯
//串行通讯速度
public const int SBR_9600 = 0; //串口速率9600
public const int SBR_14400 = 1; //14400
public const int SBR_19200 = 2; //串口速率19200
public const int SBR_38400 = 3; //串口速率38400
public const int SBR_57600 = 4; //串口速率57600
public const int SBR_115200 = 5; //串口速率115200
//播放类型常量
public const int ROOT_PLAY = 0×11; //下发节目为播放数据
public const int ROOT_DOWNLOAD = 0×12; //下发节目为保存并播放
public const int SCREEN_UNICOLOR = 1; //单色显示屏
public const int SCREEN_COLOR = 2; //双色显示屏
public const int SCREEN_FULLCOLOR = 3; //全彩色显示屏
public const
int SCREEN_GRAY = 4; //256级灰度屏
//响应消息
public const int LM_RX_COMPLETE = 1;
public const int LM_TX_COMPLETE = 2;
public const int LM_RESPOND = 3;
public const int LM_TIMEOUT = 4;
public const int LM_NOTIFY = 5;
public const int LM_PARAM = 6;
public const int LM_TX_PROGRESS = 7;
public const int LM_RX_PROGRESS = 8;
//时间格式定义,用于AddDateTime函数中format参数
public const int DF_YMD = 1; //年月日 “2004年12月31日”
public const int DF_HN = 2; //时分 “19:20″
public const int DF_HNS = 3; //时分秒 “19:20:30″
public const int DF_Y = 4; //年 “2004″
public const int DF_M = 5; //月 “12″ “01″ 注意:始终显示两位数字
public const int DF_D = 6; //日
public const int DF_H = 7; //时
public const int DF_N = 8; //分
public const int DF_S = 9; //秒
public const int DF_W = 10; //星期 “星期三”
//正计时、倒计时format参数
public const int CF_DAY = 0; //天数
public const int CF_HOUR = 1; //小时数
public const int CF_HMS = 2; //时分秒
public const int CF_HM = 3; //时分
public const int CF_MS = 4; //分秒
public const int CF_S = 5; //秒
//电源状态
public const int LEM_BUFFER_FULL = 1; //错误代码:发送缓冲满
public const int LED_POWER_ON = 1; //显示屏电源打开
public const int LED_POWER_OFF = 0; //显示屏电源已关闭
/////
///// 初始化LEDSender动态链接库
/////
//[DllImport("LEDSender.dll", EntryPoint = "LED_Startup", CharSet = CharSet.Ansi)]
//public static extern void LED_Startup();
/////
///// 终止LEDSender动态链接库
/////
//[DllImport("LEDSender.dll", EntryPoint = "LED_Cleanup", CharSet = CharSet.Ansi)]
//public static extern void LED_Cleanup();
///
/// 打开通讯信道
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_Open", CharSet = CharSet.Ansi)]
public static extern Int32 LED_Open(ref DeviceParam param, Int32 Notify, Int32 Window, Int32 Message);
///
/// 关闭通讯信道
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_Close", CharSet = CharSet.Ansi)]
public static extern Int32 LED_Close(Int32 dev);
///
/// 查询显示屏状态
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_Query", CharSet = CharSet.Ansi
)]
//public static extern Int32 LED_Query(Int32 dev, byte Group, ref char[] Host, Int32 port);
public static extern Int32 LED_Query(Int32 dev, byte Group, string Host, Int32 port);
///
/// 校正显示屏时间
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_AdjustTime", CharSet = CharSet.Ansi)]
//public static extern Int32 LED_AdjustTime(Int32 dev, byte Group, byte ScrNo, ref char[] Host, Int32 port);
public static extern Int32 LED_AdjustTime(Int32 dev, byte Group, string Host, Int32 port);
///
/// 发送数据到显示屏
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_SendToScreen", CharSet = CharSet.Ansi)]
public static extern Int32 LED_SendToScreen(Int32 dev, Byte group, string Host, Int32 Port);
///
/// 设置显示屏电源
///
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_SetPower", CharSet = CharSet.Ansi)]
//public static extern Int32 LED_SetPower(Int32 dev, byte Group, byte ScrNo, string Host, Int32 port, Int32 Power);
public static extern Int32 LED_SetPower(Int32 dev, byte Group, string Host, Int32 port, Int32 Power);
///
/// 读取显示屏电源状态
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_GetPower", CharSet = CharSet.Ansi)]
//public static extern Int32 LED_GetPower(Int32 dev, byte Group, byte ScrNo, string Host, Int32 port);
public static extern Int32 LED_GetPower(Int32 dev, byte Group, string Host, Int32 port);
///
/// 读取显示屏亮度
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_GetBrightness", CharSet = CharSet.Ansi)]
public static extern Int32 LED_GetBrightness(Int32 dev, byte Group, string Host, Int32 port);
///
/// 调整显示屏亮度
///
///
///
///
///
///
ame=”port”>
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_SetBrightness", CharSet = CharSet.Ansi)]
//public static extern Int32 LED_SetBrightness(Int32 dev, byte Group, byte ScrNo, ref char[] Host, Int32 port, byte Brightness);
public static extern Int32 LED_SetBrightness(Int32 dev, byte Group, string host, Int32 port, byte Brightness);
///
/// 调整显示屏IP地址
///
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_SetIPAddress", CharSet = CharSet.Ansi)]
//public static extern Int32 LED_SetIPAddress(Int32 dev, byte Group, byte ScrNo, ref char[] Host, Int32 port, ref char[] NewIP);
public static extern Int32 LED_SetIPAddress(Int32 dev, byte Group, string host, Int32 port, string newip);
///
///
///
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "LED_DownLoadFontSet", CharSet = CharSet.Ansi)]
//public static extern Int32 LED_DownLoadFontSet(Int32 dev, byte Group, byte ScrNo, ref char[] Host, Int32 port, ref char[] filename);
public static extern Int32 LED_DownLoadFontSet(Int32 dev, byte Group, string host, Int32 port, string filename);
///
/// 开始形成发送数据
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "MakeRoot", CharSet = CharSet.Ansi)]
public static extern Int32 MakeRoot(Int32 RootType, Int32 ScreenType);
///
/// 追加显示页面
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "AddLeaf", CharSet = CharSet.Ansi)]
public static extern Int32 AddLeaf(Int32 DisplayTime);
///
///
///
///
///
///
///
///
[DllImport("user32.dll", EntryPoint = "SetRect", CharSet = CharSet.Ansi)]
public static extern void SetRect(ref Rect rect, Int32 left, Int32 top, Int32 right, Int32 bottom);
///
///
///
///
///
///
///
///
///
///
///
rns>
[DllImport("LEDSender.dll", EntryPoint = "LED_GetNotifyMessage", CharSet = CharSet.Ansi)]
public static extern Int32 LED_GetNotifyMessage(ref TNotifyMessage Notify);
///
/// 追加时间显示
///
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "AddDateTime", CharSet = CharSet.Ansi)]
public static extern Int32 AddDateTime(ref Rect rect, Int32 transparent, string fontname, int fontsize, int fontcolor, int format);
///
/// 显示内码汉字
///
///
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "AddString", CharSet = CharSet.Ansi)]
public static extern Int32 AddString(string str, ref Rect rect1, Int32 method, Int32 speed, Int32 transparent, Int32 fontset, Int32 fontcolor);
///
/// 在当前页面创建一个字符显示区域
///
///
///
///
///
///
///
///
///
///
[DllImport("LEDSender.dll", EntryPoint = "AddText", CharSet = CharSet.Ansi)]
public static extern Int32 AddText(string text, ref Rect rect, Int32 method, Int32 speed, Int32 transparent, string fontname, Int32 fontsize, Int32 fontcolor);
///
/// 在当前显示页面上创建一个显示区域,显示内容来自于dc
/// 显示方式:
/// 1立即显示 2左滚显示 3连续上滚 4中间向上下展开 5中间向两边展开 6中间向四周展开
/// 7向左移入 8向右移入 9从左向右展开 10从右向左展开 11右上角移入 12右下角移入 13左上角移入
/// 14左下角移入 15从上向下移入 16从下向上移入 17闪烁
///
/// 设备句柄
/// 截取图片的宽度
/// 截取图片的高度
/// 显示区域(left, top, right, bottom)
/// 显示方式
/// 显示速度(1-8)越大越快
/// 是否透明 0=不透明 1=透明
///
[DllImport("LEDSender.dll", EntryPoint = "AddWindow", CharSet = CharSe
t.Ansi)]
public static extern Int32 AddWindow(int dc, int Width, int Height,ref Rect rect, int method, int speed, int transparent);
///
/// 加动画显示区域(只有256级灰度和全彩色才可显示动画)
///
///
///
///
///
//[DllImport("LEDSender.dll", EntryPoint = "AddMovie", CharSet = CharSet.Ansi)]
//public extern Int32 AddMovie(ref char[] filename, LPRECT rect, Int32 stretch);
///
/// 追加正计时显示区域
///
///
///
///
///
///
///
///
///
//[DllImport("LEDSender.dll", EntryPoint = "AddCountUp", CharSet = CharSet.Ansi)]
//public extern Int32 AddCountUp(LPRECT rect, Int32 transparent, ref char[] fontname, Int32 fontsize, Int32 fontcolor, Int32 format, LPSYSTEMTIME starttime);
///
/// 追加倒计时显示区域
///
///
///
///
///
///
///
///
///
//[DllImport("LEDSender.dll", EntryPoint = "AddCountDown", CharSet = CharSet.Ansi)]
//public extern Int32 AddCountDown(LPRECT rect, Int32 transparent, ref char[] fontname, Int32 fontsize, Int32 fontcolor, long format, LPSYSTEMTIME endtime);
}
}
然后在C#中启用对出现消息机制的控制
///
/// 重载窗口的消息机制,抓取LEDSender.dll发来的消息
///
/// 消息
protected override void WndProc(ref Message m)
{
if (m.Msg == API.WM_LED_NOTIFY)
{
TNotifyMessage msg = new TNotifyMessage();
string content = string.Empty;
if (API.LED_GetNotifyMessage(ref msg) != 0)
{
switch (msg.Message)
{
case API.LM_TX_COMPLETE:
content = “发送完成”;
break;
case API.LM_RESPOND:
switch (mand)
{
case API.PKC_QUERY:
content = “查询状态成功”;
break;
case API.PKC_GET_POWER:
content = (msg.Status == API.LED_POWER_ON) ? “电源已打开” : “电源已关闭”;
break;
case API.PKC_SET_POWER:
if (msg.Result == 99)
content = “处于自动开关屏模式,不能进行电源操作!”;
else if (msg.Status == API.LED_POWER_ON)
content = “电源已打开”;
else if (msg.Status == API.LED_POWER_OFF)
content = “电源已关闭”;
break;
case API.PKC_GET_BRIGHT:
content = “显示屏亮度为:” + msg.Status;
break;
case API.PKC_SET_BRIGHT:
content = “亮度调整完毕”;
break;
case API.PKC_ADJUST_TIME:
conten
t = “时钟校正完成”;
break;
}
break;
case API.LM_TIMEOUT:
content = “传送超时”;
break;
}
OnLedResponse(content, msg);
}
}
else
{
base.WndProc(ref m); //如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理
}
}