1,593
社区成员
发帖
与我相关
我的任务
分享
/*定义网络环境变量 出厂默认IP为192.168.1.10,端口为7*/
public static UdpClient DUCP_NetSocket;
public static IPAddress DeviceIP = IPAddress.Parse("192.168.250.10");
public static IPEndPoint RPoint = new IPEndPoint(DeviceIP, 7);
public static IPEndPoint TPoint = new IPEndPoint(DeviceIP, 7);
/*
#0.实现协议栈数据输出回调函数
*/
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
unsafe public delegate byte cbDUCP_DataOutType(byte* Data, int Size);
[DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
extern static void MB_STK_SetOutCallback(cbDUCP_DataOutType cb);
[DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe extern static sbyte MB_STK_In(byte* Data, int Size);
[DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe extern static sbyte DUCP_HOST_SYS_UpdateTime(ushort Year, byte Month, byte Day, byte Week, byte Hour, byte Minute, byte Second);
unsafe static byte cbDUCP_DataOut(byte* Data, int Size)
{
sbyte Ret;
byte[] buff = new byte[256];
byte[] RxBuff = new byte[256];
for (int i = 0; i < Size; i++)
{
buff[i] = Data[i];
}
/*发送数据到显示屏*/
DUCP_NetSocket.Send(buff, Size, TPoint);
/*接收显示屏回复的数据*/
RxBuff = DUCP_NetSocket.Receive(ref RPoint);
/*将显示屏的回复的数据传输给协议栈*/
unsafe
{
fixed (byte* pArray = RxBuff)
Ret = MB_STK_In(pArray, RxBuff.Length);
}
return (byte)Ret;
}
public Form1()
{
InitializeComponent();
/*#1.设置协议栈的回调函数*/
unsafe
{
MB_STK_SetOutCallback(cbDUCP_DataOut);
}
/*#2.创建UDP网络套接字*/
DUCP_NetSocket = new UdpClient();
TextBox.Text = "欢迎光临";
TextBox_WID.Text = "0";
}
[DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe extern static byte DUCP_HOST_TTS_Play(byte* pText, byte Opt);
[DllImport("DUCP.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe extern static byte DUCP_HOST_TWIN_DisText( int WinID,
byte* pText,
int EnterMode,
int EnterSpeed,
int DelayMode,
int DelayTime,
int EixtMode,
int EixtSpeed,
int FontIndex,
int TextColor,
int BkColor,
int DisTimes);
private void DISTEXT_Click(object sender, EventArgs e)
{
unsafe
{
byte[] Buff = System.Text.Encoding.Default.GetBytes(TextBox.Text);
//byte[] Str = new byte[Buff.Length + 1];
int WID;
//int i;
//for(i = 0;i < Buff.Length;i ++) {
// Str[i] = Buff[i];
//}
//Str[i] = 0;
int.TryParse(TextBox_WID.Text, out WID);
fixed (byte* pArray = Buff)
DUCP_HOST_TWIN_DisText(WID, pArray, 0X15, 1, 0X00, 2, 0X15, 1, 3, 0XFF, 0X00, 0);
}
}
}
函数声明:
type
TDucpCallBack = function(data: TBytes; size: integer): byte; stdcall;
procedure MB_STK_SetOutCallBack(callback: TDucpCallBack); cdecl;
external 'DUCP.dll' name 'MB_STK_SetOutCallback';
function MB_STK_In(data: TBytes; size: integer): ShortInt; cdecl;
external 'DUCP.dll' name 'MB_STK_In';
function DUCP_HOST_TWIN_DisText(WinID: integer;
pText: TBytes;
EnterMode : integer;
EnterSpeed: integer;
DelayMode : integer;
DelayTime : integer;
ExitMode : integer;
ExitSpeed : integer;
FontIndex : integer;
TextColor : integer;
BkColor : integer;
DisTimes : integer): byte; cdecl;
external 'DUCP.dll' name 'DUCP_HOST_TWIN_DisText';
调用:
procedure TForm1.btnSendClick(Sender: TObject);
var
buf: TBytes;
begin
buf := BytesOf(RawByteString(edtTxt.Text));
//此处报错,提示内存访问越界
DUCP_HOST_TWIN_DisText(2, buf, $15, 1, $00, 2, $15, 1, 3, $FF, $00, 0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
udp := TUdpSocket.Create(self);
udp.RemoteHost := '192.168.250.10';
udp.RemotePort := '7';
udp.LocalPort := '7';
udp.Active := true;
MB_STK_SetOutCallBack(OnDucpCallBack);
end;