Programming Serial Port 
Author Message
 Programming Serial Port

Does anyone know how to make a program like a terminal?
I've tried almost everything. the stuff you type in sends, but it's like the
computer isn't receiving it, but i know that the modem is sending a response
(because mine is external). Can someone tell me what's wrong with this code?
the output (currently) of it is just garbage.

.model small
.stack 0400h
.data
psp  dw 0
.code cseg

start:
     mov ax, ds
     push ax
     mov ax, _data
     mov ds, ax
     mov es, ax
     pop ax
     mov word ptr psp, ax

     mov ax, 0500h
     xor dx, dx
     mov bl, 00000111b
     int  14h

getit:
     mov dx, 03f8h
     xor ax, ax
     int  16h
     cmp al, 024h
     jz  getout
     out dx, al
     push dx
     mov dl, al
     xor ax, ax
     mov ah, 02h
     int  21h
     pop dx
     in  al, dx
     mov dl, al
     mov ah, 02h
     int  21h
     jmp getit
getout:

     mov ax, 4C00h
     int  21h
end start

GS d- s: a---- C++++ W++
 N++ K- W+++++ M-- PS
PE+ Y+ PGP- t 5 X++ R
tv+ b DI+++ D---- Ge-- h! r+



Sat, 12 Jan 2002 03:00:00 GMT  
 Programming Serial Port
First you need to program the UART (usually 16450/16550A) check the data
sheets for the way to program these. A note on the 16550, to enable
the 16550 to issue interrupts you must program every register.
Next step is to define some buffers which will hold some number of
characters for transmitting and some for receiving. You then need to
write some code that allows you get and put information to/from these
buffers. Preferably by using code which you can pass a pointer to a
buffer so as the same code will work on both the receive and transmit
buffers.
You will then need to write some code that implements some protocol a
simple protocol to use is the CTRL-S/CTRL-Q protocol.
From your code, it looks like you are trying to rely too heavily on slow
BIOS/DOS interrupts namely int 21h To obtain any (decent) speed from the
UART, you must program it directly. You can however rely on DOS/BIOS
function calls for your input from the keyboard.

Brendan

----------------------------------------------------------------------
|Brendan Crawford                                                    |
|3rd Year Student in Computer Engineering                            |
|University of Newcastle                                             |
|Australia                                                           |


----------------------------------------------------------------------


Quote:
> Does anyone know how to make a program like a terminal?
> I've tried almost everything. the stuff you type in sends, but it's like the
> computer isn't receiving it, but i know that the modem is sending a response
> (because mine is external). Can someone tell me what's wrong with this code?
> the output (currently) of it is just garbage.

> .model small
> .stack 0400h
> .data
> psp  dw 0
> .code cseg

> start:
>      mov ax, ds
>      push ax
>      mov ax, _data
>      mov ds, ax
>      mov es, ax
>      pop ax
>      mov word ptr psp, ax

>      mov ax, 0500h
>      xor dx, dx
>      mov bl, 00000111b
>      int  14h

> getit:
>      mov dx, 03f8h
>      xor ax, ax
>      int  16h
>      cmp al, 024h
>      jz  getout
>      out dx, al
>      push dx
>      mov dl, al
>      xor ax, ax
>      mov ah, 02h
>      int  21h
>      pop dx
>      in  al, dx
>      mov dl, al
>      mov ah, 02h
>      int  21h
>      jmp getit
> getout:

>      mov ax, 4C00h
>      int  21h
> end start

> GS d- s: a---- C++++ W++
>  N++ K- W+++++ M-- PS
> PE+ Y+ PGP- t 5 X++ R
> tv+ b DI+++ D---- Ge-- h! r+



Sat, 12 Jan 2002 03:00:00 GMT  
 Programming Serial Port


   >Does anyone know how to make a program like a terminal?
   >I've tried almost everything. the stuff you type in sends, but it's
   >like the computer isn't receiving it, but i know that the modem is
   >sending a response (because mine is external). Can someone tell me
   >what's wrong with this code? the output (currently) of it is just
   >garbage.

One problem with your code is that it's using interrupts to access the COM
port.  In a communications program, you're MUCH better off accessing the
hardware directly, IMHO.

Here's the code for a very simple, unbuffered terminal program that directly
accesses the hardware.  It's not real sophisticated, but it should serve as
an illustration of the principle involved.  Note that this program does =NOT=
initialize the port.

The code is written for the A86 shareware compiler.  If you're using one of
those "name-brand" compilers -- or a clone thereof -- you're on your own. :)

;
; TERMINAL.ASM  Version 0.2
; DOS Mini-Terminal Program   -= Requires ANSI.SYS or equivalent =-
; Freeware from Kirk Lawrence
;
;   As is always the case with "freeware," there are no guarantees whatsoever.
;   If you use this program, you do so entirely at your own risk and peril.
;   Your use of this TERMINAL.ASM source code, or any executable file compiled
;   therefrom, shall be prima facie evidence under law that you understand
;   and accept these terms and conditions.  
;
code    segment                 ;start of code segment
        org     100h            ;leave room for base page
        jmp     start           ;go start the program
;
esc     equ     1Bh
save    dw      0               ;storage variable for cursor position
ansi    db      27,'[25;80H$'   ;ANSI sequence to set cursor position
back    db      13,'         $' ;screen erase sequence
moddatp dw      0               ;modem data port variable
modctlp dw      0               ;modem control port variable
noansi  db      13,10,'  You idiot!  There''s no ANSI driver!',13,10,07,'$'
syntax  db      10,13,'  Syntax: TERMINAL x',10,13,07,'$'
msg     db      esc,'[2J',esc,'[30;47m  TERMINAL Version 0.2  Y   DOS '
        db      'Mini-Terminal - COM'
comprt  db      0               ;storage variable for COM port number
        db      '   T ALT-C=cls t ALT-Q=exit ',esc,'[37;40m',10,13,'$'
clr     db      esc,'[2J$'      ;ANSI clear-screen sequence
;
;----------------------------------------------------------------
; This routine prints a text string (in offset DX) to the screen.
;
pstring:
        mov     ah,9            ;function 9 - print string
        int     21h             ;call DOS services
        ret                     ;return
;
;----------------------------------------------------------------
; Check to make sure that an ANSI driver is loaded.  So many idiots
; are playing with computers today that you can't take ANYTHING for
; granted!
;
start:
        mov     ah,3            ;function 3 - get cursor position
        mov     bh,0            ;assume page 0
        int     10h             ;call BIOS video services
        mov     save,dx         ;store result in variable
        mov     dx,offset ansi  ;point DX to ANSI test string
        call    pstring         ;go print it
        mov     ah,3            ;read cursor position again
        mov     bh,0            ;page 0
        int     10h             ;call BIOS video services
        cmp     dx,184Fh        ;is cursor at row 25, column 80?
        jnz     nope            ;no, go say 'no ANSI'
        mov     dx,save         ;restore original position from variable
        mov     ah,2            ;function 2 - restore cursor position
        mov     bh,0            ;page 0
        int     10h             ;call BIOS video services
        jmp     okay            ;go continue with the program
nope:
        mov     dx,offset back  ;point DX to screen erase sequence
        call    pstring         ;go print it
        mov     dx,offset noansi  ;point DX to 'no ansi' message
        call    pstring         ;go print it
        int     20h             ;exit to DOS
;
;----------------------------------------------------------------
; Parse the command line for the user-specified COM port number,
; and assign the addresses accordingly.
;
okay:
        mov     si,81h          ;location of the command line in SI
parse:
        lodsb                   ;load a byte from the command line (in AL)
        cmp     al,20h          ;is it a space?
        je      parse           ;yes, so ignore it & check the next byte
        cmp     al,'1'          ;is it a 1?
        je      comone          ;yes, go set up for COM1
        cmp     al,'2'          ;is it a 2?
        je      comtwo          ;yes, go set up for COM2
        cmp     al,'3'          ;is it a 3?
        je      comthr          ;yes, go set up for COM3
        cmp     al,'4'          ;is it a 4?
        je      comfou          ;yes, go set up for COM4
        jmp     synt            ;unknown argument, go display syntax msg
comone:
        mov     moddatp,03F8h   ;store modem data register address
        mov     modctlp,03FDh   ;store modem status register address
        jmp     setup
comtwo:
        mov     moddatp,02F8h
        mov     modctlp,02FDh
        jmp     setup
comthr:
        mov     moddatp,03E8h
        mov     modctlp,03EDh
        jmp     setup
comfou:
        mov     moddatp,02E8h
        mov     modctlp,02EDh
setup:
        mov     comprt,al       ;store COM port number in variable
prepre:
        mov     dx,offset msg   ;point to the screen header message
        call    pstring         ;print it
        jmp     modstatus       ;go check the modem
;
;----------------------------------------------------------------
; The main program logic starts here.
;
prestatus:
        call    clscrn          ;clear the screen
modstatus:                      ;routine to check modem status
        mov     dx,modctlp      ;point to modem status register
        in      al,dx           ;read it
        and     al,1            ;is anything there?
        jz      keystatus       ;no, so go check keyboard
        mov     dx,moddatp      ;yes, so point to modem data register
        in      al,dx           ;read it
        cmp     al,0Ch          ;have we got an ASCII 12?
        je      prestatus       ;yes, go clear the screen
        int     29h             ;call fast-screen-write service
        jmp     modstatus       ;go see if another char. is waiting
keystatus:                      ;routine to check keyboard status
        mov     ah,1            ;function 1 - keyboard status
        int     16h             ;call BIOS keyboard services
        jz      modstatus       ;no character waiting, so go check modem
        mov     ah,0            ;function 1 - read keyboard buffer
        int     16h             ;call BIOS keyboard services
        cmp     ax,02E00h       ;has ALT-C been pressed?
        je      prepre          ;yes, go clear the screen
        cmp     ax,01000h       ;has ALT-Q been pressed?
        je      exit            ;yes, go exit the program
        mov     dx,moddatp      ;point to modem data register
        out     dx,al           ;send the keypress to the modem
        jmp     modstatus       ;go check modem again
;
;----------------------------------------------------------------
;This routine clears the screen.
;
clscrn:
        mov     dx,offset clr   ;point to the clear-screen sequence
        call    pstring         ;print it
        ret                     ;return
;
;----------------------------------------------------------------
;This routine prints the syntax message.
;
synt:
        mov     dx,offset syntax ;point to the syntax message
        call    pstring         ;print it
        jmp     abort           ;go exit the program
;
exit:
        call    clscrn          ;clear the screen
abort:
        int     20h             ;exit to DOS
end

.....................................................................
Return address is mangled to foil spambots. Remove all "g"s to e-mail.

Net-Tamer V 1.08X - Test Drive



Sat, 12 Jan 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How can I program serial port?

2. Parallel Port / Serial Port Programming

3. Newbe: Serial Port Programming

4. serial port programming and protokoll extraction

5. 2 serial ports in the same program

6. serial port programming

7. serial port programming

8. programming the serial port

9. serial port programming

10. Win32 serial port programming

11. Serial port programming with G95

12. Serial port programming.

 

 
Powered by phpBB® Forum Software