C#发送数据到缓冲区的问题

hzhao2008 2010-04-15 09:19:00
有一张发送卡,需要把要发送的数据线放到缓冲区里 ,然后通过指针发出去 ,给了下面一些接口 ,我该怎么实现,程序是用c#写的??请高手支招

DeviceId=0 我这里只有一张卡 DeviceId都为0


/*
原型 : int start_transmit(float rate, int frame, int DeviceId)
功能 :
发送卡准备发送和设置参数。
在多次transmit()调用的第一次之前调用一次。
参数 :
rate 发送卡输出信号的速率,bps为单位
frame 发送卡输出TS流的包长度,188或204
DeviceId 发送卡编号,0开始
返回值:
0表示成功, 负数表示失败
*/
int start_transmit(float rate, int frame, int DeviceId);


/*
原型 : int transmit(void* pBuffer,int nLength,int DeviceId)
功能 :
发送卡发送数据.可以反复多次调用,建议nLength取值在20K到2M之间
参数 :
pBuffer 待发送数据缓冲区的指针;
nLength 要发送的长度,字节单位,建议取值在20480到204800之间.
DeviceId 发送卡编号,0开始.
返回值:
实际发送的数据长度,字节单位
负数表示操作出错
*/
int transmit(void* pBuffer,int nLength, int DeviceId);


/*
原型 : int flush(int DeviceId)
功能 :
强制刷新输出缓冲区,建议在单次发送文件数据全部完成时调用
参数 :
DeviceId 发送卡编号,0开始
返回值:
实际发送的数据长度,字节单位
负数表示操作出错
*/
int flush(int DeviceId);


/*
原型 : int stop_transmit(int DeviceId)
功能 :
发送卡结束发送。
在多次bst_transmit()的最后一次之后调用一次。
参数 :
DeviceId 发送卡编号,0开始
返回值:
0表示成功, 负数表示失败
*/
stop_transmit(int DeviceId);
...全文
318 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
marissa1010 2010-07-22
  • 打赏
  • 举报
回复
最后怎么解决的啊???
lh1611 2010-04-16
  • 打赏
  • 举报
回复
学习学习
lh1611 2010-04-16
  • 打赏
  • 举报
回复
学习学习
lh1611 2010-04-16
  • 打赏
  • 举报
回复
学习学习
hzhao2008 2010-04-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hapen_zhang 的回复:]
C# code

string a = "hello";
int MAXLENGTH = 188;

byte[] source = Encoding.ASCII.GetBytes(a);
int totalSend = 0;

……
[/Quote]
谢谢楼上的,可能是我没有表达清楚transmit(buff, MAXLENGTH, 0)中要发送的是字节数组的指针,而不是单纯的字节数组,你现在发送的buff只是数组,不是指针。
c++接口函数上有说明:
/*
原型 : int transmit(void* pBuffer,int nLength,int DeviceId)
功能 :
发送卡发送数据.可以反复多次调用,建议nLength取值在20K到2M之间
参数 :
pBuffer 待发送数据缓冲区的指针;
nLength 要发送的长度,字节单位,建议取值在20480到204800之间.
DeviceId 发送卡编号,0开始.
返回值:
实际发送的数据长度,字节单位
负数表示操作出错
*/
int transmit(void* pBuffer,int nLength, int DeviceId);
pBuffer 待发送数据缓冲区的指针;
pBuffer是现在字节数组的指针。
那字节数组的指针该怎么实现?
whowhen21 2010-04-16
  • 打赏
  • 举报
回复
我顶置了这个帖子~~
平生我自如 2010-04-16
  • 打赏
  • 举报
回复
没弄过阿
帮顶~~~~~~~~~~~~~~~~~~~~~
hapen_zhang 2010-04-15
  • 打赏
  • 举报
回复
你这几个接口函数是C++的吧,可以通过DllImport的形式导入来用

[DllImport("dll的名字.dll")]
public static extern Init16 transmit(IntPtr pBuffer, Int16 nLength, Int16 deviceID);

string a = "hello" ;
IntPtr pBuffer = System.Runtime.InteropServices.Marshal.StringToCoTaskMemAuto(a);
transmit(pBuffer,a.Length, 0);
hzhao2008 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hapen_zhang 的回复:]
看注释来调用就好
开始发送前调用int start_transmit(float rate, int frame, int DeviceId);

需要发送数据时调用int transmit(void* pBuffer,int nLength, int DeviceId);
一批数据发送完成时调用int flush(int DeviceId);

全部发送结束之后调用stop_tran……
[/Quote]
这个流程我知道
但是具体实现 比如说我要发送一个字符串 string a = "hello" 先要把这个放到缓冲区里 然后通过指针调用int transmit()函数 把数据发送出去 这个我该怎么实现??
hapen_zhang 2010-04-15
  • 打赏
  • 举报
回复
看注释来调用就好
开始发送前调用int start_transmit(float rate, int frame, int DeviceId);

需要发送数据时调用int transmit(void* pBuffer,int nLength, int DeviceId);
一批数据发送完成时调用int flush(int DeviceId);

全部发送结束之后调用stop_transmit(int DeviceId);


碰到具体问题再来问吧。
June1991 2010-04-15
  • 打赏
  • 举报
回复
学习学习
hapen_zhang 2010-04-15
  • 打赏
  • 举报
回复
函数引入那改下
[DllImport("dll的名字.dll")]
public static extern Init16 transmit(byte[] pBuffer, Int16 nLength, Int16 deviceID);
hapen_zhang 2010-04-15
  • 打赏
  • 举报
回复

string a = "hello";
int MAXLENGTH = 188;

byte[] source = Encoding.ASCII.GetBytes(a);
int totalSend = 0;

while (true)
{
byte[] buff = new byte[MAXLENGTH];
buff[0] = 0x40;
int left = source.Length - totalSend;
if (left > MAXLENGTH - 1)
{
Array.Copy(source, totalSend, buff, 1, MAXLENGTH - 1);
totalSend += MAXLENGTH - 1;
transmit(buff, MAXLENGTH, 0);
}
else
{
Array.Copy(source, totalSend, buff, 1, left);
transmit(buff, MAXLENGTH, 0);
break;
}

}
hzhao2008 2010-04-15
  • 打赏
  • 举报
回复
顶起来 有高手么?
hzhao2008 2010-04-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hapen_zhang 的回复:]
你这几个接口函数是C++的吧,可以通过DllImport的形式导入来用

C# code

[DllImport("dll的名字.dll")]
public static extern Init16 transmit(IntPtr pBuffer, Int16 nLength, Int16 deviceID);

string a = "hello" ;
IntPtr pBuffe……
[/Quote]
谢谢你的回答 如果发送字符串变量确实是这样实现
但是问题又出现了 刚了解到这个发送卡 不能直接发送字符串变量 它只能发送一个个188字节数组,并且第一个字节规定为0x40 才能把数据发送出去
比如:

string a = "hello" ;
byte[] buff = new byte[188];
byte[0] = 0x40;
//然后再把a放到buff 这个字节数组里面发送出去 请问这该如何让实现
并且此时
IntPtr pBuffer = System.Runtime.InteropServices.Marshal.StringToCoTaskMemAuto(a);
//还能用么

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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