怎么没有人回答??
我先回答自己的第一个问题 ---- 那个芯片的编程接口没有改变。
第二个问题还请大家帮忙看看(50分呢!)
------------------------------------------------------------------------------
昨天,我用内嵌汇编成功的编写了读写串口的程序。(我用一根线把自己的com1
com2连起来,自发自收,用于50米内近距离通讯(不用modam),你稍改一下即可
用到自己的程序中了。)
我把程序贴在这儿 供大家参考:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
程序的核心代码如下:
-- <font color=blue><i>窗体文件:</i></font> -----------------------------------
object Form1: TForm1
Left = 277
Top = 114
Width = 544
Height = 375
Caption = 'Form1'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -15
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 120
TextHeight = 15
object Button2: TButton
Left = 120
Top = 20
Width = 94
Height = 31
Caption = '发 --> 收'
TabOrder = 0
OnClick = Button2Click
end
object Button3: TButton
Left = 20
Top = 20
Width = 94
Height = 31
Caption = '初始化'
TabOrder = 1
OnClick = Button3Click
end
object Edit1: TEdit
Left = 20
Top = 90
Width = 151
Height = 20
TabOrder = 2
end
object Button1: TButton
Left = 180
Top = 90
Width = 61
Height = 31
Caption = '=>aa'
TabOrder = 3
OnClick = Button1Click
end
end
-- <font color=blue><i>单元文件</i></font> ----------------------------------------
unit Unit_com;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button2: TButton;
Button3: TButton;
Edit1: TEdit;
Button1: TButton;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
aa:char;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);<font color=green><i>//发送和接收</i></font>
var sc,rc:char; yy:integer;
begin
sc := aa;
asm
push ax
push dx
// ------------
mov dx,2FBH
in al,dx
and al,7FH <font color=green><i>//将线路控制寄存器LCR的最高位置 0,表示可以访问RBR或THR</i></font>
out dx,al
//
@_1:
mov dx,2F8H
mov al,sc <font color=green><i>//将待发送数据送THR</i></font>
out dx,al
//
mov dx,2FDH <font color=green><i>//测试LSR,确定是否正确发送</i></font>
in al,dx
test al,20H
jnz @_1
//
mov dx,3F8H
in al,dx
// ------------
pop dx
pop ax
end;
// ----------------
asm
push ax
push dx
// ------------
mov dx,3FBH
in al,dx
and al,7FH
out dx,al
//
@_2:
mov dx,3FDH
in al,dx
or al,01H <font color=green><i>//接收数据准备好</i></font>
out dx,al
test al,01H
jnz @_2
//
mov dx,3F8H <font color=green><i>//接收数据到RBR</i></font>
in al,dx
mov rc,al
// ------------
pop dx
pop ax
end;
aa := rc;
showmessage('收到的数据'+aa+'。');
//
yy := 0; <font color=green><i>//将收到的数据用ASCII码显示</i></font>
while not(aa=chr(yy)) do
yy := yy +1;
showmessage(inttostr(yy));
end;
procedure TForm1.Button3Click(Sender: TObject);<font color=green><i>//初始化串口</i></font>
begin
asm
push dx
push ax
// -----------
mov dx,3F8H <font color=green><i>//设置波特率:1200</i></font>
mov al,60H
out dx,al
mov dx,3F9H
mov al,0
out dx,al
//
mov dx,3FBH <font color=green><i>//奇校验,1位停止位,7位数据位</i></font>
mov al,0AH
out dx,al
//
mov dx,3F9H //设中断允许寄存器为0,屏蔽4种中断
mov al,0
out dx,al
// ----------
mov dx,2F8H
mov al,60H
out dx,al
mov dx,2F9H
mov al,0
out dx,al
//
mov dx,2FBH
mov al,0AH
out dx,al
//
mov dx,2F9H
mov al,0
out dx,al
// ----------
pop ax
pop dx
end;
showMessage(' 串口:COM1 COM2 初始化完毕。'+chr(13)+
chr(13)+' * --- 波特率:1200'+
chr(13)+' * --- 奇校验,1位停止位,7位数据位 ');
end;
<font color=green><i>//功能:将用户输入edit1内的一个字符转成char型</i></font>
procedure TForm1.Button1Click(Sender: TObject);
var dd:string;
begin
dd := edit1.Text; <font color=red><i>//只能输入一个字符(多了会出错)</i></font>
aa := dd[1];
end;
end.
+++++++++++++++++++++++++++++++++++++++++
<font color=red><i>使用说明</i></font>
先点击 窗体上的“<b>初始化</b>”按纽
再 向文本框中输入<b>一个</b>ASCII字符
然后点 窗体上的“<b>=>aa</b>”按纽
最后点 窗体的“<b>发 --> 收</b>”按纽