请把这段C转成Pascal,我不熟悉C啊,先谢谢了(100分)

  • 主题发起人 主题发起人 jerry1001
  • 开始时间 开始时间
J

jerry1001

Unregistered / Unconfirmed
GUEST, unregistred user!
#include <windows.h>
#include <string.h>
#include "IrLib.h"

HANDLE hDLLInst = 0;

static int FillDCB (int nBaud);
static int TestIR (DWORD spec);

// static char *szComPort[] = {"COM1", "COM1", "COM2", "COM3", "COM4",
// "COM5", "COM6", "COM7", "COM8", "COM9"};

static HANDLE hComm=NULL
// the handle of the opened Comm Device.
static BYTE cInBuf [256]
// Input buffer for receiving data.
static BYTE szBuf [256]
// Record Buffer.
static BYTE szIR [8]
// for IR command

static COMMTIMEOUTS TimeOuts;
static DCB dcb;
static DWORD nChar;
static BOOL bShowErrorMessage=TRUE;
static int nDlyTime=200
// delay time for DIP switch cradle, 2004.02.20

//-------------------------------------------------------------------------//
BOOL WINAPI DllMain (HANDLE hModule, DWORD dwFunc, LPVOID lpNot)
{
hDLLInst = hModule;

switch (dwFunc)
{
case DLL_PROCESS_ATTACH:
case DLL_PROCESS_DETACH:
default:
break;
}
return TRUE;
}

//-------------------------------------------------------------------------//
DllExport HANDLE WINAPI OpenIrCom (int nPort, int nBaud)
{
char szComPort[16];

wsprintf (szComPort, "////.//COM%d", nPort)
// 2006.08.16, to support COM port > 9
if ((hComm = CreateFile (szComPort, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
{
if (bShowErrorMessage)
MessageBox (GetActiveWindow(), "Can not open the COM port!!",
"Error", MB_OK | MB_ICONEXCLAMATION);
return NULL;
}

SetupComm (hComm, 256, 256)
// allocate transmit &amp
receive buffer
if (!FillDCB (nBaud))
{
CloseHandle (hComm);
if (bShowErrorMessage)
MessageBox (GetActiveWindow(), "Can not configure the IR device!!",
"Error", MB_OK | MB_ICONEXCLAMATION);
return NULL;
}

if (SetCommState (hComm, &amp;dcb) < 0)
{
CloseHandle (hComm);
if (bShowErrorMessage)
MessageBox (GetActiveWindow(), "Can not initialize the COM port!!",
"Error", MB_OK);
hComm = NULL;
return NULL;
}

TimeOuts.ReadIntervalTimeout = MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
TimeOuts.WriteTotalTimeoutMultiplier = 5;
TimeOuts.WriteTotalTimeoutConstant = 50;
SetCommTimeouts (hComm, &amp;TimeOuts)
// check dwProvCapabilities

return hComm;
}

//-------------------------------------------------------------------------//
DllExport void WINAPI CloseIrCom (void)
{
if (hComm != INVALID_HANDLE_VALUE &amp;&amp
hComm != NULL)
{
CloseHandle (hComm)
// close the COM Port.
hComm = NULL;
}
}

//-------------------------------------------------------------------------//
DllExport void WINAPI WriteIrCom (LPSTR lpStr)
{
DWORD nChar, nCount;

nCount = lstrlen (lpStr);
WriteFile (hComm, lpStr, nCount, &amp;nChar, NULL);
}

//-------------------------------------------------------------------------//
DllExport LPSTR WINAPI ReadIrCom (void)
{
DWORD i, nChar, nCount;
BYTE cRet;
DWORD dwTime0;

dwTime0 = GetTickCount ();
nCount = 0
// reset char counter
cRet = 0;

while (cRet != '/r') // while not get the return char keep on reading
{
if (ReadFile (hComm, cInBuf, 1, &amp;nChar, NULL))
{
for (i=0
i<nChar
i++)
if (nCount < 255)
szBuf [nCount++] = cInBuf ;
cRet = cInBuf[i-1];
}

if (GetTickCount() - dwTime0 > 5000) // check if time out
{
if (bShowErrorMessage)
MessageBox (GetActiveWindow(), "Time out!", "Error", MB_OK);
return NULL;
}
}
szBuf [nCount] = 0;

return szBuf;
}

//-------------------------------------------------------------------------//
DllExport int WINAPI vbOpenIrCom (int nPort, int nBaud)
{
char szComPort[16];

wsprintf (szComPort, "////.//COM%d", nPort)
// 2006.08.16, to support COM port > 9
if ((hComm = CreateFile (szComPort, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
{
if (bShowErrorMessage)
MessageBox (GetActiveWindow(), "Can not open the COM port!!",
"Error", MB_OK | MB_ICONEXCLAMATION);
return 0;
}

SetupComm (hComm, 256, 256)
// allocate transmit &amp
receive buffer
if (!FillDCB (nBaud))
{
CloseHandle (hComm);
if (bShowErrorMessage)
MessageBox (GetActiveWindow(), "Can not configure the IR device!!",
"Error", MB_OK | MB_ICONEXCLAMATION);
return -1;
}

if (SetCommState (hComm, &amp;dcb) < 0)
{
CloseHandle (hComm);
if (bShowErrorMessage)
MessageBox (GetActiveWindow(), "Can not initialize the COM port!!",
"Error", MB_OK);
hComm = NULL;
return -1;
}

TimeOuts.ReadIntervalTimeout = MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
TimeOuts.WriteTotalTimeoutMultiplier = 5;
TimeOuts.WriteTotalTimeoutConstant = 50;
SetCommTimeouts (hComm, &amp;TimeOuts)
// check dwProvCapabilities

return 1;
}

//-------------------------------------------------------------------------//
DllExport int WINAPI vbReadIrCom (LPSTR lpBuf)
{
DWORD i, nChar, nCount;
BYTE cRet;
DWORD dwTime0;

dwTime0 = GetTickCount ();
nCount = 0
// reset char counter
cRet = 0;

while (cRet != '/r') // while not get the return char keep on reading
{
if (ReadFile (hComm, cInBuf, 1, &amp;nChar, NULL))
{
for (i=0
i<nChar
i++)
if (nCount < 255)
szBuf [nCount++] = cInBuf ;
cRet = cInBuf[i-1];
}

if (GetTickCount() - dwTime0 > 5000) // check if time out
{
if (bShowErrorMessage)
MessageBox (GetActiveWindow(), "Time out!", "Error", MB_OK);
return -1;
}
}
szBuf [nCount] = 0;

// lstrcpy (lpBuf, szBuf)

memcpy (lpBuf, szBuf, nCount)
// 2003.9.29

return nCount;
}

//-------------------------------------------------------------------------//
DllExport int WINAPI ShowErrorMessage (int nShow)
{
bShowErrorMessage = (nShow > 0)? TRUE : FALSE;
return 1;
}

//-------------------------------------------------------------------------//
#pragma optimize ("", off)

static int FillDCB (int nBaud)
{
GetCommTimeouts (hComm, &amp;TimeOuts);
TimeOuts.ReadIntervalTimeout = MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier = 0;
TimeOuts.ReadTotalTimeoutConstant = 0;
TimeOuts.WriteTotalTimeoutMultiplier = 5;
TimeOuts.WriteTotalTimeoutConstant = 50;
SetCommTimeouts (hComm, &amp;TimeOuts);

dcb.BaudRate = 38400;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fRtsControl = RTS_CONTROL_ENABLE
// set RTS on (low!)
dcb.fDtrControl = DTR_CONTROL_ENABLE
// set DTR on (low!)
SetCommState (hComm, &amp;dcb)
// get power from DTR
nDlyTime = 200
// 2004.02.20, for DIP switch cradle
Sleep (nDlyTime);

if (TestIR (CBR_9600)) // test current baud rate
dcb.BaudRate = CBR_9600;
else if (TestIR (CBR_38400))
dcb.BaudRate = CBR_38400;
else if (TestIR (CBR_115200))
dcb.BaudRate = CBR_115200;
else if (TestIR (CBR_57600))
dcb.BaudRate = CBR_57600;
else if (TestIR (CBR_19200))
dcb.BaudRate = CBR_19200;
else
{
return 0;
}

dcb.fRtsControl = RTS_CONTROL_DISABLE
// set RTS off (high!)
SetCommState (hComm, &amp;dcb);
Sleep (nDlyTime);

*szIR = 0x07;
WriteFile (hComm, szIR, 1, &amp;nChar, NULL)
// disable Echo
Sleep (nDlyTime);

if (nBaud == 115200)
{
dcb.BaudRate = CBR_115200;
*szIR = 0x36;
}
else if (nBaud == 57600)
{
dcb.BaudRate = CBR_57600;
*szIR = 0x35;
}
else if (nBaud == 38400)
{
dcb.BaudRate = CBR_38400;
*szIR = 0x34;
}
else if (nBaud == 19200)
{
dcb.BaudRate = CBR_19200;
*szIR = 0x33;
}
else // use default value:9600
{
dcb.BaudRate = CBR_9600;
*szIR = 0x32;
}

WriteFile (hComm, szIR, 1, &amp;nChar, NULL)
// set to new baud rate
Sleep (nDlyTime);

*szIR = 0x51;
WriteFile (hComm, szIR, 1, &amp;nChar, NULL)
// load new baud rate
Sleep (nDlyTime);

dcb.fRtsControl = RTS_CONTROL_ENABLE
// set RTS on (low!)
SetCommState (hComm, &amp;dcb);
Sleep (nDlyTime);

dcb.Parity = NOPARITY;
dcb.ByteSize = 8;
dcb.fOutxCtsFlow = FALSE
// no flow control
dcb.fOutX = FALSE;

return 1;
}

//-------------------------------------------------------------------------//
static int TestIR (DWORD spec)
{
static BYTE szIn [4];
DWORD nChar, dwTime;
BOOL nRtn;

dcb.BaudRate = spec;
dcb.fRtsControl = RTS_CONTROL_DISABLE
// set RTS off (high !!)
SetCommState (hComm, &amp;dcb);
Sleep (nDlyTime);

*szIR = 0x0f
// set Control register 1
WriteFile (hComm, szIR, 1, &amp;nChar, NULL);
Sleep (nDlyTime);

dwTime = GetTickCount();
nRtn = *szIn = 0;
while (GetTickCount() - dwTime < 100) // 100 msec
{
if (ReadFile (hComm, szIn, 1, &amp;nChar, NULL)) // waiting for echo
{
if (*szIn == 0x0f) // receive the same char
nRtn = 1;
break;
}
}

dcb.fRtsControl = RTS_CONTROL_ENABLE
// set RTS on (LOW !!)
SetCommState (hComm, &amp;dcb);
Sleep (nDlyTime);
nDlyTime = 100
// 2004.02.20, for DIP switch cradle, set to 100 except 9600bps
return nRtn;
}

#pragma optimize ("", on)
//-------------------------------------------------------------------------//
我转后的如下:
//动态库的函数声明
function OpenIrCom(nport:smallint
nBaud:longint): longint
stdcall;far;external 'IrLib.dll';
function vbOpenIrCom(nport:smallint
nBaud:longint): longint
stdcall;far;external 'IrLib.dll';
procedure CloseIrCom()stdcall;far;external 'IrLib.dll';
procedure WriteIrCom(Str:Pchar)stdcall;far;external 'IrLib.dll';
function ReadIrCom():Pchar;stdcall;far;external 'IrLib.dll';
function vbReadIrCom(arr:Pchar):longint;stdcall;far;external 'IrLib.dll';
function ShowErrorMessage(nShow:smallint):smallint;stdcall;far;external 'IrLib.dll';
//-----------------------------------------------------------------
procedure TFormCpt.Button1Click(Sender: TObject);
var
szData : array[0..255] of char;
begin
if combobox1.ItemIndex < 0 then
begin
MessageBox (Handle, '端口没有选择!', '错误', MB_OK);
exit;
end;
memo1.Lines.Clear;
memo1.Lines.Add('开始');
OpenIrCom(Combobox1.ItemIndex+1, 115200);
WriteIrCom('READ' + #13);
while True do
begin
fillchar(szData[low(szData)],length(szData),#0);
StrPCopy(szData, ReadIrCom());
if szData = 'OVER' + #13 then
begin
MessageBox (Handle, '传输完成!', '提示', MB_OK);
break;
end;
if szData = 'NAK' + #13 then
begin
MessageBox (Handle, '命令错误!', '提示', MB_OK);
break;
end;
// if szData = '' then //注意:第一条记录总是出现为空,没办法,暂时不用
// begin
// MessageBox (Handle, '传输错误!', '提示', MB_OK);
// break;
// end;
WriteIrCom ('ACK' + #13)
//如果NAK则表示接受错误,需要重发数据
memo1.Lines.Add(szData);
sleep(100)
end;
memo1.Lines.Add('结束');
CloseIrCom();
end;
//------------------------------------------
读出来的数据,第1行为空(有数据啊){错误就在这里},第2行以后就是对的。
 
****************************************************************************
***** Protocol for receiving transaction data from CPT-7xx terminals *****
****************************************************************************

Note : All commands or records sent to or received from the CPT-7xx
terminals should be ended with a return character (0x0d).

1. Send the "READ" command to the CPT-7xx terminal and wait for return.

2. If the terminal returns "ACK", then it is ready to send data to PC.

3. The format of each record received from the terminal is as follows,

A. The first byte is a sequence count rotated from 0 to 9. It's purpose
is to ensure the correct order of data transmission.
B. The last two bytes are the checksum values. The checksum is calculated
by adding up the sequence count and all the data bytes.
C. Devide the sum calculated above by 256 will get the last byte checksum
value. If this value happens to be 13 (the return character: 0x0d),
change it to 14 (0x0e).
D. The remaining of the above calculation is the first byte checksum
value. If it happens to be 13, change it to 14.
E. Please note that the checksum byte-order for this protocol is different
to that of downloading lookup files.

4. If the received data is correct, the PC program should return "ACK" to the
CPT-7xx terminal so that it can send the next record.

5. If the received data is not correct, the PC program should return "NAK" to
the CPT-7xx terminal so that it can resend the record.

6. If the received data is duplicated (i.e. the sequence count is same as
previous record), then the PC program should discard this record but still
return "ACK" to the CPT-7xx terminal so that it can send the next record.

7. Repeat the above procedures until receiving the "OVER" command from the
CPT-7xx terminal.
 
串口代码 何必DLL。
 
是CPT-8000的数据采集器.
 
这个DLL是CPT-8000自带的,使用DLL可以自由存取数据采集器的条形码.
 
后退
顶部