Delphi中使用spcomm控件接收数据的代码,请问在BCB5中代码应如何改写

sdjifeng 2001-07-15 02:26:35
这是Delphi中使用spcomm控件接收数据的代码
Comm1: TComm;
Memo1: TMemo;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
var
s: string;
begin
SetLength(S, BufferLength); //接收RS232的数据并显示Memo1上。
Move(Buffer^, PChar(S)^, BufferLength);
Memo1.Lines.Add(S);
Memo1.Invalidate;
end;

请问在BCB5中以上代码应如何改写。
void __fastcall TForm1::OnReceiveData(TObject *Sender, Pointer Buffer,
WORD BufferLength)
{
/*代码请在此填写*/
}
...全文
148 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sephil 2001-07-15
  • 打赏
  • 举报
回复
心情不好
进来看看
sdjifeng 2001-07-15
  • 打赏
  • 举报
回复
使string类真的很方便。
傻乐tao 2001-07-15
  • 打赏
  • 举报
回复
void *memcpy( void *dest, const void *src, size_t count );
Parameters
dest
New buffer
src
Buffer to copy from
count
Number of characters to copy
/* MEMCPY.C: Illustrate overlapping copy: memmove
* handles it correctly; memcpy does not.
*/

#include <memory.h>
#include <string.h>
#include <stdio.h>

char string1[60] = "The quick brown dog jumps over the lazy fox";
char string2[60] = "The quick brown fox jumps over the lazy dog";
/* 1 2 3 4 5
* 12345678901234567890123456789012345678901234567890
*/

void main( void )
{
printf( "Function:\tmemcpy without overlap\n" );
printf( "Source:\t\t%s\n", string1 + 40 );
printf( "Destination:\t%s\n", string1 + 16 );
memcpy( string1 + 16, string1 + 40, 3 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );

/* Restore string1 to original contents */
memcpy( string1 + 16, string2 + 40, 3 );

printf( "Function:\tmemmove with overlap\n" );
printf( "Source:\t\t%s\n", string2 + 4 );
printf( "Destination:\t%s\n", string2 + 10 );
memmove( string2 + 10, string2 + 4, 40 );
printf( "Result:\t\t%s\n", string2 );
printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );

printf( "Function:\tmemcpy with overlap\n" );
printf( "Source:\t\t%s\n", string1 + 4 );
printf( "Destination:\t%s\n", string1 + 10 );
memcpy( string1 + 10, string1 + 4, 40 );
printf( "Result:\t\t%s\n", string1 );
printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
}

Output
Function: memcpy without overlap
Source: fox
Destination: dog jumps over the lazy fox
Result: The quick brown fox jumps over the lazy fox
Length: 43 characters

Function: memmove with overlap
Source: quick brown fox jumps over the lazy dog
Destination: brown fox jumps over the lazy dog
Result: The quick quick brown fox jumps over the lazy dog
Length: 49 characters

Function: memcpy with overlap
Source: quick brown dog jumps over the lazy fox
Destination: brown dog jumps over the lazy fox
Result: The quick quick brown dog jumps over the lazy fox
Length: 49 characters





sdjifeng 2001-07-15
  • 打赏
  • 举报
回复
HorkyTao:这是个好办法,我没想到,先给你20分。
string的使用我实在不清楚,你能讲讲吗?
sdjifeng 2001-07-15
  • 打赏
  • 举报
回复
不好意思,我用bcb仅仅半个月。
memcopy干什么用,查不到。
傻乐tao 2001-07-15
  • 打赏
  • 举报
回复
给分吧!
傻乐tao 2001-07-15
  • 打赏
  • 举报
回复

char *s;
s = (char *) Buffer ;
Memo1->Lines->Add(AnsiString(s));
s=NULL;
Wingsun 2001-07-15
  • 打赏
  • 举报
回复
不会吧,自己不能写啊!

void __fastcall TForm1::OnReceiveData(TObject *Sender, LPVOID Buffer,
WORD BufferLength)
{
char * ReadBuffer=(char *)malloc(BufferLength);
memcopy(ReadBuffer,Buffer,BufferLength);
Memo1->Lines->Add(ReadBuffer);
free(ReadBuffer);
Memo1->Invalidate();
}

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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