汇编实现的串口通信,一次发送9个bit,C++Buildere->Delphi(100分)

  • 主题发起人 主题发起人 watter
  • 开始时间 开始时间
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&amp;0x20)==0 &amp;&amp; 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&amp;0x20)==0 &amp;&amp; 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&amp;0x01)==0 &amp;&amp; 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&amp;0x01)==0 &amp;&amp; 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


 
串口是可以发送9bits的,就是利用校验位来做第9位数据。
 
校验位是硬件芯片自动计算出来的,
即使你设成9位,也不可能用最后一位来传数据。
 
此言差矣,RS232定义的校验位有多种形式:奇校验、偶校验、清位、置位;
后两种就是为特殊的算法乃至用法预留的
 
to:iie
应用协议的部分内容如下:
“8个数据位(最低位在前),一个可编程的第九位数据位,发送地址时为1,发送数据时为0,”
现在要发四帧数据:
1、地址信息;
2、功能码;
3、控制信息;
4、控制信息;
其中第一帧的第九位应为1,其余三帧的第九位应为0,那么按你的说法我应该在发送完第一帧数据
以后改变COM口的设置参数,改变奇偶校验方式,再发送后面的三帧数据,对吗?
 
正是。你所说的这种通信协议,是早期单片机串行通信中用的比较多的协议之一。
 
问题已经解决,谢谢你的提醒。100分送上
 

Similar threads

I
回复
0
查看
649
import
I
I
回复
0
查看
718
import
I
I
回复
0
查看
632
import
I
I
回复
0
查看
628
import
I
后退
顶部