虽然我不懂DEPHI,总算懂点VC和PASCAL,我试试吧
unsigned int CRC(int length,unsigned char *byte,unsigned int orgi)
{
unsigned int arc;
int i,j;
arc=orgi;
for(i=0;i<length;i++)
{ arc=arc^(*byte);
for(j=0;j<8;j++)
{ if(arc&0x1) { arc=arc>>1;
arc=arc^0xA001;
}
else arc=arc>>1;
}
byte++;
}
return arc;
}
上面可以不动,完全是C++的语法
校时例程:sstime_all_3720(int port_ID);
{
dwore A;
dword msec;
word thiscrc;
Tsystemtime DT;
const byte byte_stime_2[31]={$ff,$ff,$14,$cd,$00,25,
$00,$00, //设备ID号,0表示广播;
$ff,$ff,$40,$00,
$8f,$0c,$00,$00,18,$00, //应用层
$00,$04,0,0,0,0,$01,$04,0,0,0,0,
$00,$00}; //校验码;
{
getlocaltime(DT);
A=rchtime(DT); ——格林威治秒钟
msec= DT.wMilliseconds *1000 ; ——毫秒;
move(A,byte_stime_2[20],4);
move(msec,byte_stime_2[26],4);
thiscrc=CRC(26,&byte_stime_2[4],$ffff);
byte_stime_2[30]=LOBYTE(thiscrc);
byte_stime_2[31]=HIBYTE(thiscrc);
}
}
搞定!
unsigned int CRC(int length,unsigned char *byte,unsigned int orgi)
{
unsigned int arc;
int i,j;
arc=orgi;
for(i=0;i<length;i++)
{ arc=arc^(*byte);
for(j=0;j<8;j++)
{ if(arc&0x1) { arc=arc>>1;
arc=arc^0xA001;
}
else arc=arc>>1;
}
byte++;
}
return arc;
}
上面可以不动,完全是C++的语法
校时例程:sstime_all_3720(int port_ID);
{
dwore A;
dword msec;
word thiscrc;
Tsystemtime DT;
const byte byte_stime_2[31]={$ff,$ff,$14,$cd,$00,25,
$00,$00, //设备ID号,0表示广播;
$ff,$ff,$40,$00,
$8f,$0c,$00,$00,18,$00, //应用层
$00,$04,0,0,0,0,$01,$04,0,0,0,0,
$00,$00}; //校验码;
{
getlocaltime(DT);
A=rchtime(DT); ——格林威治秒钟
msec= DT.wMilliseconds *1000 ; ——毫秒;
move(A,byte_stime_2[20],4);
move(msec,byte_stime_2[26],4);
thiscrc=CRC(26,&byte_stime_2[4],$ffff);
byte_stime_2[30]=LOBYTE(thiscrc);
byte_stime_2[31]=HIBYTE(thiscrc);
}
}
搞定!
解决方案 »
- 为什么我这段代码绘不了图~请大神们指导一下~
- 大侠们帮帮忙! 如何把自己编写的进程权限提升到system或系统管理员权限??
- CRect(point1,point2); 这个语句有什么效果?
- odbc连接sqlserver为什么报“未发现数据源名称并且未指定默认驱动程序
- 请教个文件打开的问题,谢谢
- 我的信誉值又减少了,不知道怎么搞的,没有做什么过分的事情啊
- 我在用DAO时,选择数据库那一步 ,选择数据库后,OK时,为什么 提示一个错无,不识别数据库。我用ACCESS建的数据库
- 请问下怎样在DrawItem中把按钮disable时的字体画成windows按钮默认的样子?
- 希望多交朋友
- 路由器的问题
- 请问,怎么样才能得到正在运行的应用程序的窗口的标题,就象2000里面按ctrl+alt+del然后任务管理器的应用程序一样 --【
- 请问大家SHOOSECOLOR结构中的lpCustColors怎么赋值呢???
{
DWORD A;
DWORD msec;
WORD thiscrc;
SYSTEMTIME DT;
BYTE byte_stime_2[32]=
{0xff,0xff,0x14,0xcd,0x00,25,
0x00,0x00, //设备ID号,0表示广播;
0xff,0xff,0x40,0x00,
0x8f,0x0c,0x00,0x00,08,0x00, //应用层
0x00,0x04,0,0,0,0,0x01,0x04,0,0,0,0,
0x00,0x00}; //校验码; GetSystemTime(&DT); //直接取格林威治秒钟,不需转换
msec = DT.wMilliseconds *1000 ; //——毫秒;
memcpy(byte_stime_2[20],A ,4);
memcpy(byte_stime_2[26],msec,4);
thiscrc = CRC(26,&byte_stime_2[4],0xffff);
byte_stime_2[30]=LOBYTE(thiscrc);
byte_stime_2[31]=HIBYTE(thiscrc);
}
不过第二个函数好象还有些问题
void CMOXA3720testDlg::MOXA3720(int portID)
{
...
WORD thiscrc;
const BYTE byte_2[14]={0xff,0xff,0x14,0xcd,0x00,25,
0x00,0x00, //设备ID号,0表示广播;
0xff,0xff,0x40,0x00,
0x00,0x00}; //校验码;
{
thiscrc=CRC(26,&byte_2[4],0xffff);
byte_2[12]=LOBYTE(thiscrc);
byte_2[13]=HIBYTE(thiscrc);
}
...
}
出现以下三个错误
error C2664: 'CRC' : cannot convert parameter 2 from 'const unsigned char *' to 'unsigned char *'
Conversion loses qualifiers
error C2166: l-value specifies const object
error C2166: l-value specifies const object
你把const去掉了问题解决了,
但你能告诉我为什么?
等下就给分!
我也没上机试,但是你原来的代码有太明显的错误。
to cris919:
C2664 因为你传递的实参与型参类型不一致。
C2166 因为你定义byte_2 为const参数,你就不能修改它。不能作为左值。
unit crc16;interface
uses windows;
// 注意:因最高位一定为“1”,故略去
const cnCRC_16:word = $8005; // CRC-16 = X16 + X15 + X2 + X0
const cnCRC_CCITT:word = $1021; // CRC-CCITT = X16 + X12 + X5 + X0procedure BuildTable16( aPoly :word );
function CRC_16( aData: pByte; aSize: word ;com:integer):word;var
Table_CRC:array of array [0..255] of word ;implementation
procedure BuildTable16( aPoly :word );
var
nData:word;
i,j,k:word;
nAccum:word;
begin
for i := 0 to 255 do
begin
nData := word ( i shl 8 );
nAccum := 0;
for j := 0 to 7 do
begin
if ( ( nData xor nAccum ) and $8000 )<>0 then
nAccum := ( nAccum shl 1 ) xor aPoly
else
nAccum :=nAccum shl 1;
nData := nData shl 1;
end;
for k:=0 to commCount-1 do
Table_CRC[k][i] := word (nAccum);
end;
end;// 计算 16 位 CRC 值,CRC-16 或 CRC-CCITT
function CRC_16( aData: pByte; aSize: word;com:integer ):word;
var
i: word ;
nAccum :word ;
tempData:word;
begin
nAccum:=0; for i := 0 to aSize-1 do
begin
tempData:= word(pByte((integer(aData)+i))^);
nAccum := ( nAccum shl 8 ) xor word (Table_CRC[com][( nAccum shr 8 ) xor tempData]);
end;
result:=nAccum;
end;