W
watter
Unregistered / Unconfirmed
GUEST, unregistred user!
本人最近在写一串口控制程序,通过串口给一个编码解码器发送控制信息,要求一次发送9个bit,不要
起始位和结束位,硬件会自动加上,我找了好多文章,都没有找到,信息不能分两次发送,MSCOMM和SPCOMM的
bit属性最大只能设为8,后来有位老兄提供了C++BUILDER写的部分代码,哪位大哥肯帮忙翻译成DELPHI,小弟向谢了。
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdlib.h>
#pragma hdrstop
#include "COMM.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
BOOL send_char(int comm,BYTE cha)
{
long i;
if(comm==1)
{
i=0;
do{
i++;
asm mov DX,0x3fd
asm in AL,DX
}while((_AL&0x20)==0 && i<100000);
if(i>=100000)
{
return 0;
}
asm mov DX,0x3f8
asm mov AL,cha
asm out DX,AL
return 1;
}
else
{
i=0;
do{
i++;
asm mov DX,0x2fd
asm in AL,DX
}while((_AL&0x20)==0 && i<100000);
if(i>=100000)
{
return 0;
}
asm mov DX,0x2f8
asm mov AL,cha
asm out DX,AL
return 1;
}
}
//---------------------------------------
BYTE receive_char(int comm)
{
long i;
if(comm==1)
{
i=0;
do{
i++;
asm mov DX,0x3fd
asm in AL,DX
}while( (_AL&0x01)==0 && i<100000);
if(i>=100000)
{
return 0;
}
asm mov DX,0x3f8
asm in AL,DX
return(_AL);
}
else
{
i=0;
do{
i++;
asm mov DX,0x2fd
asm in AL,DX
}while( (_AL&0x01)==0 && i<100000);
if(i>=100000)
{
return 0;
}
asm mov DX,0x2f8
asm in AL,DX
return(_AL);
}
}
void initCom(int comm)
{
if(comm==1)
{
asm mov dx,0x3fb
asm mov al,0x80
asm out dx,al
asm mov dx,0x3f8
asm mov al,0x30
asm out dx,al
asm inc dx
asm mov al,0x00
asm out dx,al
asm mov dx,0x3fb
asm mov al, 0x1b
asm out dx,al
asm inc dx
asm mov al,0x03
asm out dx,al
asm mov dx,0x3f9
asm mov al,0
asm out dx,al
}
else
{
asm mov dx,0x2fb
asm mov al,0x80
asm out dx,al
asm mov dx,0x2f8
asm mov al,0x30
asm out dx,al
asm inc dx
asm mov al,0x00
asm out dx,al
asm mov dx,0x2fb
asm mov al, 0x1b
asm out dx,al
asm inc dx
asm mov al,0x03
asm out dx,al
asm mov dx,0x2f9
asm mov al,0
asm out dx,al
}
}
//
BYTE inttoge(int num)
{
div_t t1;
BYTE ge;
t1=div(num,10);
ge=0x30+(BYTE)t1.rem;
return ge;
}
BYTE inttoshi(int num)
{
div_t t1,t2;
BYTE shi;
t1=div(num,100);
t2=div(t1.rem,10);
shi=0x30+(BYTE)t2.quot;
return shi;
}
BYTE inttobai(int num)
{
div_t t;
BYTE bai;
t=div(num,100);
bai=0x30+(BYTE)t.quot;
return bai;
}
BYTE inttoqian(int num)
{
div_t t;
BYTE qian;
t=div(num,1000);
qian=0x30+(BYTE)t.quot;
return qian;
}
//--------------------------------------------------------------------------
void SetAddr(int comm)
{
switch(comm)
{
case 1:
asm
{
mov dx,0x3fb
mov al,0x3b
out dx,al
}
break;
case 2:
asm
{
mov dx,0x2fb
mov al,0x3b
out dx,al
}
break;
}
}
//--------------------------------------------------------------------------
void SetData(int comm)
{
switch(comm)
{
case 1:
asm
{
mov dx,0x3fb
mov al,0x2b
out dx,al
}
break;
case 2:
asm
{
mov dx,0x2fb
mov al,0x2b
out dx,al
}
break;
}
}
//--------------------------------------------------------------------------
void OpenDogFirst(void)
{
asm
{
mov dx,443h
mov al,100
out dx,al
mov dx,443h
in al,dx
}
}
//-------------------------------------------------------------------------
void OpenWatchDog(void)
{
asm
{
mov dx,443h
mov al,25
out dx,al
mov dx,443h
in al,dx
}
}
//---------------------------------------------------------------------------
void CloseWatchDog(void)
{
asm
{
mov dx,043h
in al, dx
}
}
//---------------------------------------------------------------------------
void ResetWathcDog(void)
{
asm
{
mov dx,043h
in al,dx
mov dx,443h
in al,dx
}
}
//---------------------------------------------------------------------------
void StartListenKar1(short CurCameraNo)
{
BYTE data1;
data1=CurCameraNo-1;
data1<<=4;
data1|=0x80;
asm
{
mov dx,283h
mov al,data1
out dx,al
}
}
//---------------------------------------------------------------------------
void StartListenKar2(short CurCameraNo)
{
BYTE data1;
data1=CurCameraNo-1;
data1<<=4;
data1|=0x80;
asm
{
mov dx,303h
mov al,data1
out dx,al
}
}
//---------------------------------------------------------------------------
void StopListenKar1(void)
{
BYTE data1=0x10;
asm
{
mov dx,283h
mov al,data1
out dx,al
}
}
//---------------------------------------------------------------------------
void StopListenKar2(void)
{
BYTE data1=0x10;
asm
{
mov dx,303h
mov al,data1
out dx,al
}
}
//---------------------------------------------------------------------------
void ResetAudioKard1(void)
{
asm
{
mov dx,285h
mov al,55
out dx,al
}
}
//---------------------------------------------------------------------------
void ResetAudioKard2(void)
{
asm
{
mov dx,305h
mov al,55
out dx,al
}
}
//---------------------------------------------------------------------------
void ResetRead(void)
{
asm
{
mov dx,286h
mov al,55
out dx,al
}
}
//---------------------------------------------------------------------------
void ResetRead2(void)
{
asm
{
mov dx,306h
mov al,55
out dx,al
}
}
//---------------------------------------------------------------------------
short ReadDataNum(void)
{
short Num;
BYTE DataH,DataL;
asm
{
mov dx,281h
in al,dx
mov DataH,al
mov dx,280h
in al,dx
mov DataL,al
}
Num=DataH*255+DataL;
return Num;
}
//---------------------------------------------------------------------------
short ReadDataNum2(void)
{
short Num;
BYTE DataH,DataL;
asm
{
mov dx,301h
in al,dx
mov DataH,al
mov dx,300h
in al,dx
mov DataL,al
}
Num=DataH*255+DataL;
return Num;
}
//---------------------------------------------------------------------------
BYTE ReadData(void)
{
BYTE data;
asm
{
mov dx,284h //DATA
in al,dx
mov data,al
}
return data;
}
//---------------------------------------------------------------------------
BYTE ReadData2(void)
{
BYTE data;
asm
{
mov dx,304h //DATA
in al,dx
mov data,al
}
return data;
}
//---------------------------------------------------------------------------
comm.h
//---------------------------------------------------------------------------
#ifndef CommH
#define CommH
BOOL send_char(int comm,BYTE cha);
BYTE receive_char(int comm);
void initCom(int comm);
BYTE inttoge(int num);
BYTE inttoshi(int num);
BYTE inttobai(int num);
BYTE inttoqian(int num);
void SetAddr(int comm);
void SetData(int comm);
void OpenDogFirst(void);
void OpenWatchDog(void);
void CloseWatchDog(void);
void ResetWathcDog(void);
void StartListenKar1(short CurCameraNo);
void StartListenKar2(short CurCameraNo);
void StopListenKar1();
void StopListenKar2();
void ResetAudioKard1(void);
void ResetAudioKard2(void);
void ResetRead(void);
void ResetRead2(void);
short ReadDataNum(void);
short ReadDataNum2(void);
BYTE ReadData(void);
BYTE ReadData2(void);
//---------------------------------------------------------------------------
#endif
起始位和结束位,硬件会自动加上,我找了好多文章,都没有找到,信息不能分两次发送,MSCOMM和SPCOMM的
bit属性最大只能设为8,后来有位老兄提供了C++BUILDER写的部分代码,哪位大哥肯帮忙翻译成DELPHI,小弟向谢了。
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdlib.h>
#pragma hdrstop
#include "COMM.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
BOOL send_char(int comm,BYTE cha)
{
long i;
if(comm==1)
{
i=0;
do{
i++;
asm mov DX,0x3fd
asm in AL,DX
}while((_AL&0x20)==0 && i<100000);
if(i>=100000)
{
return 0;
}
asm mov DX,0x3f8
asm mov AL,cha
asm out DX,AL
return 1;
}
else
{
i=0;
do{
i++;
asm mov DX,0x2fd
asm in AL,DX
}while((_AL&0x20)==0 && i<100000);
if(i>=100000)
{
return 0;
}
asm mov DX,0x2f8
asm mov AL,cha
asm out DX,AL
return 1;
}
}
//---------------------------------------
BYTE receive_char(int comm)
{
long i;
if(comm==1)
{
i=0;
do{
i++;
asm mov DX,0x3fd
asm in AL,DX
}while( (_AL&0x01)==0 && i<100000);
if(i>=100000)
{
return 0;
}
asm mov DX,0x3f8
asm in AL,DX
return(_AL);
}
else
{
i=0;
do{
i++;
asm mov DX,0x2fd
asm in AL,DX
}while( (_AL&0x01)==0 && i<100000);
if(i>=100000)
{
return 0;
}
asm mov DX,0x2f8
asm in AL,DX
return(_AL);
}
}
void initCom(int comm)
{
if(comm==1)
{
asm mov dx,0x3fb
asm mov al,0x80
asm out dx,al
asm mov dx,0x3f8
asm mov al,0x30
asm out dx,al
asm inc dx
asm mov al,0x00
asm out dx,al
asm mov dx,0x3fb
asm mov al, 0x1b
asm out dx,al
asm inc dx
asm mov al,0x03
asm out dx,al
asm mov dx,0x3f9
asm mov al,0
asm out dx,al
}
else
{
asm mov dx,0x2fb
asm mov al,0x80
asm out dx,al
asm mov dx,0x2f8
asm mov al,0x30
asm out dx,al
asm inc dx
asm mov al,0x00
asm out dx,al
asm mov dx,0x2fb
asm mov al, 0x1b
asm out dx,al
asm inc dx
asm mov al,0x03
asm out dx,al
asm mov dx,0x2f9
asm mov al,0
asm out dx,al
}
}
//
BYTE inttoge(int num)
{
div_t t1;
BYTE ge;
t1=div(num,10);
ge=0x30+(BYTE)t1.rem;
return ge;
}
BYTE inttoshi(int num)
{
div_t t1,t2;
BYTE shi;
t1=div(num,100);
t2=div(t1.rem,10);
shi=0x30+(BYTE)t2.quot;
return shi;
}
BYTE inttobai(int num)
{
div_t t;
BYTE bai;
t=div(num,100);
bai=0x30+(BYTE)t.quot;
return bai;
}
BYTE inttoqian(int num)
{
div_t t;
BYTE qian;
t=div(num,1000);
qian=0x30+(BYTE)t.quot;
return qian;
}
//--------------------------------------------------------------------------
void SetAddr(int comm)
{
switch(comm)
{
case 1:
asm
{
mov dx,0x3fb
mov al,0x3b
out dx,al
}
break;
case 2:
asm
{
mov dx,0x2fb
mov al,0x3b
out dx,al
}
break;
}
}
//--------------------------------------------------------------------------
void SetData(int comm)
{
switch(comm)
{
case 1:
asm
{
mov dx,0x3fb
mov al,0x2b
out dx,al
}
break;
case 2:
asm
{
mov dx,0x2fb
mov al,0x2b
out dx,al
}
break;
}
}
//--------------------------------------------------------------------------
void OpenDogFirst(void)
{
asm
{
mov dx,443h
mov al,100
out dx,al
mov dx,443h
in al,dx
}
}
//-------------------------------------------------------------------------
void OpenWatchDog(void)
{
asm
{
mov dx,443h
mov al,25
out dx,al
mov dx,443h
in al,dx
}
}
//---------------------------------------------------------------------------
void CloseWatchDog(void)
{
asm
{
mov dx,043h
in al, dx
}
}
//---------------------------------------------------------------------------
void ResetWathcDog(void)
{
asm
{
mov dx,043h
in al,dx
mov dx,443h
in al,dx
}
}
//---------------------------------------------------------------------------
void StartListenKar1(short CurCameraNo)
{
BYTE data1;
data1=CurCameraNo-1;
data1<<=4;
data1|=0x80;
asm
{
mov dx,283h
mov al,data1
out dx,al
}
}
//---------------------------------------------------------------------------
void StartListenKar2(short CurCameraNo)
{
BYTE data1;
data1=CurCameraNo-1;
data1<<=4;
data1|=0x80;
asm
{
mov dx,303h
mov al,data1
out dx,al
}
}
//---------------------------------------------------------------------------
void StopListenKar1(void)
{
BYTE data1=0x10;
asm
{
mov dx,283h
mov al,data1
out dx,al
}
}
//---------------------------------------------------------------------------
void StopListenKar2(void)
{
BYTE data1=0x10;
asm
{
mov dx,303h
mov al,data1
out dx,al
}
}
//---------------------------------------------------------------------------
void ResetAudioKard1(void)
{
asm
{
mov dx,285h
mov al,55
out dx,al
}
}
//---------------------------------------------------------------------------
void ResetAudioKard2(void)
{
asm
{
mov dx,305h
mov al,55
out dx,al
}
}
//---------------------------------------------------------------------------
void ResetRead(void)
{
asm
{
mov dx,286h
mov al,55
out dx,al
}
}
//---------------------------------------------------------------------------
void ResetRead2(void)
{
asm
{
mov dx,306h
mov al,55
out dx,al
}
}
//---------------------------------------------------------------------------
short ReadDataNum(void)
{
short Num;
BYTE DataH,DataL;
asm
{
mov dx,281h
in al,dx
mov DataH,al
mov dx,280h
in al,dx
mov DataL,al
}
Num=DataH*255+DataL;
return Num;
}
//---------------------------------------------------------------------------
short ReadDataNum2(void)
{
short Num;
BYTE DataH,DataL;
asm
{
mov dx,301h
in al,dx
mov DataH,al
mov dx,300h
in al,dx
mov DataL,al
}
Num=DataH*255+DataL;
return Num;
}
//---------------------------------------------------------------------------
BYTE ReadData(void)
{
BYTE data;
asm
{
mov dx,284h //DATA
in al,dx
mov data,al
}
return data;
}
//---------------------------------------------------------------------------
BYTE ReadData2(void)
{
BYTE data;
asm
{
mov dx,304h //DATA
in al,dx
mov data,al
}
return data;
}
//---------------------------------------------------------------------------
comm.h
//---------------------------------------------------------------------------
#ifndef CommH
#define CommH
BOOL send_char(int comm,BYTE cha);
BYTE receive_char(int comm);
void initCom(int comm);
BYTE inttoge(int num);
BYTE inttoshi(int num);
BYTE inttobai(int num);
BYTE inttoqian(int num);
void SetAddr(int comm);
void SetData(int comm);
void OpenDogFirst(void);
void OpenWatchDog(void);
void CloseWatchDog(void);
void ResetWathcDog(void);
void StartListenKar1(short CurCameraNo);
void StartListenKar2(short CurCameraNo);
void StopListenKar1();
void StopListenKar2();
void ResetAudioKard1(void);
void ResetAudioKard2(void);
void ResetRead(void);
void ResetRead2(void);
short ReadDataNum(void);
short ReadDataNum2(void);
BYTE ReadData(void);
BYTE ReadData2(void);
//---------------------------------------------------------------------------
#endif