C#代码转Delphi代码

天行归来 2021-01-13 10:43:46
问题描述:需要对接厂家提供的LED屏设备,但对方提供的DEMO只有c#,现需要将代码转Delphi,C#代码如下:

/*定义网络环境变量 出厂默认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);
}
}
}

我也转了Delphi的测试代码,如下:


函数声明:

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;



有熟悉C#和Delphi的帮忙找找问题或转下代码,感谢!!!
...全文
245 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
参数char*、byte*不要翻译成TBytes,TBytes是动态数组,会有一个隐含的上界传入。可以用PAnsiChar或者PByte
天行归来 2021-01-13
  • 打赏
  • 举报
回复
感谢老铁帮忙。 问题解决了,用PByte会更好,TBytes也可以。 问题是出在回调上,加上const解决问题。 function OnDucpCallBack(const data: TBytes; size: integer): byte; cdecl;

1,593

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧