表9.15 串行通信口BIOS功能(INT 14H)

 AH   功能   调用参数   返回参数
 0  初始化串行   AL=初始化参数   AH=通信口状态
   通信口   DX=通信口号
  COM1=0
  COM2=1,etc
  AL=调制解调器状态
 1  向串行口写字符   AL=所写字符
  DX=通信口号
  COM1=0
  COM2=1,etc
  写字符成功:
  (AH)= 0  (AL)= 字符
  写字符失败:
  (AH)7 = 1 (AH)1-4 = 通信口状态
 2  从串行口读字符   DX=通信口号
  COM1=0
  COM2=1,etc
  读成功:
  (AH)7 = 0  (AL)= 字符
  读失败:
  (AH)7 = 1  (AH)1-4 =通信口状态
 3  取通信口状态   DX=通信口号
  COM1=0
  COM2=1
  (AH)=通信口状态
  (AL)=调制解调器状态

 图9.7 串行通信口初始化参数  
      

  例9.26 要求0号通信口的传输率为2400波特,字长为8位,1位终止位,无奇偶校验。
    MOV   AH,0   ; 初始化通信口
    MOV   AL,0A3H ; 0A3H=10100011B
    MOV   DX,0   ; COM1
    INT   14H    ; BIOS调用

  返回参数中,通信口状态字节各位置1的含义如图9.13。


 图9.8 串行通信口状态字节  
  

  PS/2以及所有的PS计算机,AH=04功能允许程序员将波特率设置为19,200,数据位的长度可以设置为5,6,7或8,而不是象AH=0功能那样只能设置成7或8位。INT14的AH=4和AH=5支持的功能见附录BIOS中断。
  
  按照例9.27的通信要求编程时,需要以下几个步骤:
  1. 检测按键,如果按下一键,则获取字符码并将它写到要传输的COM端口,同时也要检测是否按动退出键ESC。
  2. 如无任何键按下,检测COM端口的状态,如果已接收到一个字符,则读取并显示在屏幕上。
  3. 转向第1步

  例9.27 两台PC机通过COM2端口进行串行数据通信,编写一个汇编语言程序,要求从一台PC机上键盘输入的字符能传送到另一台PC机,若按下ESC键,则退出程序。在程序中,COM2端口初始化为4800波特,8位数据位,无校验,1位终止位;
 
  TITLE Serial data communication between two PCs
      . model small
      . stack
   ;------------------------------------------------------------------
      . data
   message db 'Serial communication via COM2,4800,'
       db 'no p,1 stop,8 bit data.',0ah,0dh
       db 'Any key press is sent to other PC.',0ah,0dh
       db 'Press ESC to exit','$'
  ;-------------------------------------------------------------------
     . code
  main  proc
      mov   ax, @data
      mov   ds, ax
      mov  ah, 09       ;display string
      mov  dx, offset message
      int  21h

  ;Initializing COM2
      mov   ah, 0        ;initialize COM port
      mov  dx, 1        ;COM 2
      mov  al, 0c3h       ;4800, n, 1, 8
      int  14h         ;call BIOS

  ;Checking key press and sending key to COM2 to be transferred
  again: mov   ah, 01       ;get keyboard state
      int  16h         ; for checking key press
      jz   next         ;if ZF=1,there is no key press
      mov  ah, 0        ;there is a key press, get it
      int  16h         ;call BIOS

  ;with AH=0 to get the char itself. AL=ASCII char pressed
      cmp   al, 1bh       ;is it ESC key?
      je   exit        ; yes, exit
      mov  ah, 1        ;no, send the char to COM port
      mov   dx, 1        ;COM 2
      int  14h         ;call BIOS

  ;Check COM2 port to see there is char.if so get it and display it
  next: mov  ah, 3        ;read COM states to AH
      mov  dx, 1        ;COM 2
      int  14h         ;call BIOS
      and  ah, 1        ;mask all bits except D0
      cmp  ah, 1        ;check D0 to see if there is a char
      jne  again        ;no char, goto monitor keyboard
      mov  ah, 2        ;yes, read char from COM2
      mov  dx, 1
      int  14h
      mov  dl, al        ;DL get char to be display
      mov  ah, 2        ;display char function
      int  21h         ;call DOS
      jmp  again        ;keep monitoring keyboard
  exit: mov  ah, 4ch        ;exit to DOS
      int  21h
  main endp
  ;--------------------------------------------------------------------
     end