1. Re: Term Program
- Posted by Greg Harris <blackdog at MAIL.CDC.NET>
Mar 10, 1997
-
Last edited Mar 11, 1997
> Does anyone have some code for a terminal program I could look at.
> Mike
Mike,
Most of the progran examples that I have seen use the open
command. I have routines that init the modem and send data but I
don't have anything written that will trap the incoming character.
(This requires a interrupt handler routine and my knowledge of
assembly is almost nill). Here is what I have. I
have tested it on COM2 and COM4 with the standard IRQs but I haven't
tried it on anything with nonstandard IRQs. Maybe we could persaude the machine
lang. people to help with the interrupt handler? PS the routines need
ver 1.5 beta to work (using the and_bits and or_bits routines)
(You may need to get some of the include files off the web page)
--Serial.e version .3 alpha
--Released to public domain
--By James G. Harris 3/1997
--If it blows your computer or someones elses up, don't blame me
--because its free. Use at own risk!
--If used please credit me somewhere. Hopefully not in the fine print
-- :)
include ports.e --Credit: Jacques Deschenes
include delay.e --Credit: Jacques Deschanes
include round.e --Credit: Lucius Hilley III
global integer COMPORT
--COMPORT must be set before calling routines
-- constants
global constant S_1BIT = 0, --Stop bits
S_2BIT = 4,
W_5BIT = 0, --Word Length
W_6BIT = 1,
W_7BIT = 2,
W_8BIT = 3,
P_NONE=0, --Parity
P_ODD=16,
P_EVEN=24
global constant COM1=#03F8,
COM2=#02F8,
COM3=#03E8,
COM4=#02E8
global constant IER = 1, -- control register offsets
LCR = 3,
MCR = 4,
LSR = 5,
MDMMSR = 6
global constant ENABLE_READY = 1,
MDMMOD = 11,
MDMCD = 128,
INTCTLR = #21
type bit_rate (integer i)
sequence valid_rates
valid_rates = {150,300,1200,2400,4800,9600,19200,38400,57600,115200}
if find(i,valid_rates) then
return i
end if
end type
--------------------------------------------------------------------
global procedure init_ports(bit_rate bitrate, integer wordlen,
integer parity, integer stopbits)
--init ports
--example init_ports(38400, W_8BIT, P_NONE, S_1BIT)
--sets port defined by COMPORT to 38400, 8 bits, no parity and 1 stop
--bit
integer brd, msb, lsb, setbyte
atom byte
brd = int_div(1843200, (16 * bitrate))
msb = int_div(brd,256)
lsb = and_bits(brd,255)
byte = or_bits(Input((COMPORT+LCR)), 128)
Output(byte, COMPORT+LCR)
Output(msb, COMPORT+IER)
Output(lsb, COMPORT)
byte = and_bits(Input((COMPORT+LCR)),127)
Output(byte, COMPORT+LCR)
setbyte = wordlen+parity+stopbits
Output(setbyte, (COMPORT+3))
end procedure
---------------------------------------------------------------------------
global procedure enable_ports()
--enable the ports for communication
integer a
a = Input(INTCTLR)
a = and_bits(a,239)
Output(a,INTCTLR)
a = Input(COMPORT+LCR)
a = and_bits(a,127)
Output(a,COMPORT+LCR)
Output(ENABLE_READY, COMPORT+IER)
Output(or_bits(8,MDMMOD), COMPORT+MCR)
Output(MDMCD, COMPORT+MDMMSR)
Output(32,20)
end procedure
----------------------------------------------------------------------
global procedure sendchar(integer b)
--send an individual character to a port
--example sendchar('a')
while and_bits(Input(COMPORT+LSR),32) != 32 do
end while
Output(b,COMPORT)
delay(.01)
puts(1,b)
end procedure
----------------------------------------------------------------------
global procedure StringToPort(sequence s, integer b)
--Send a string to the port
--integer b determine the end of string action
--example StringToPort("ATZ",2)
--Sends ATZ and Carg return and linefeed
integer a
for i = 1 to length(s) do
a = s[i]
sendchar(a)
end for
if b = 1 then
sendchar(13) -- carg. return only
elsif b = 2 then
sendchar(13) -- carg. return + linefeed
sendchar(10)
elsif b = 3 then
sendchar(10) -- linefeed only
end if
end procedure
----------------------------------------------------------------------
global procedure disable_ports()
--call to disable the comport
integer b
StringToPort("ATC0",0)
b = Input(INTCTLR)
b = or_bits(b,16)
Output(b, INTCTLR)
b = Input(COMPORT+LCR)
b = and_bits(b,122)
Output(b,COMPORT+LCR)
Output(0,COMPORT+IER)
Output(0,COMPORT+MCR)
Output(0,32)
end procedure
-- End Serial.e
Example usage program: sercom.ex
Init the modem and dial a number (I use this to test my pager)
include serial.e
------------------------------------------------
-- Set up the ports for communication --
COMPORT = COM2 --Set to the right comport
init_ports(57600, W_8BIT, P_NONE, S_1BIT)
enable_ports()
----------------------------------------------------------------------
StringToPort("ATH0",2)
delay(1)
StringToPort("ATZ",2) -- Init modem string
delay(3) -- wait 3 seconds
StringToPort("ATDT5555555;",2) --dail pager number
delay(9) -- wait about 9 seconds on pager service to answer
StringToPort("ATDT5555555;",2)-- number to page
delay(3) -- wait 3 secs
StringToPort("+++",0) -- send escape code
delay(3) -- wait 3 sec
StringToPort("ATH0",2) -- hang up
delay(2) -- wait 2 sec
disable_ports() -- call to reset comports