April 24, 2024, 10:36:19 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


The Windows version of the terminal program using the Serial Comms Library.

Started by Egil, October 23, 2014, 05:52:20 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Egil

This is a Windows version of the communications terminal program using the IW Serial comms Library.
Also attached is a zip archive containing the compiled program and a simple help file.

Enjoy!

'
' WTerm-iwb  -  serial comms program using IW Serial Comms Library
'
AutoDefine "Off"
$include "windowssdk.inc"

TYPE COMPARAMS
dim comport:int
dim baud:int
dim bits:int
dim parity:int
dim stopbits:int
dim oldport:int
dim localecho:int
ENDTYPE
dim pb:COMPARAMS

CONST EchoOn  = 1
CONST EchoOff = 0

CONST COM1 = 1
CONST COM2 = 2
CONST COM3  = 3
CONST COM4  = 4
CONST COM5  = 5
CONST COM6  = 6
CONST COM7  = 7
CONST COM8  = 8
CONST COM9  = 9
CONST COM10 = 10
CONST COM11 = 11
CONST COM12 = 12
CONST COM13 = 13
CONST COM14 = 14
CONST COM15 = 15
CONST COM16 = 16

CONST WM_GETTEXTLENGTH = 0xE
DIM l,t,w,h:UINT
dim Tx$[512]:Istring
dim CRLF:string
CRLF=chr$(10)+chr$(13)
dim lft,tp,wdt,hgt:UINT
int cpchanged
Istring rx$[2048]
string echo,ctext
string tpar[3]
tpar[0]="N","O","E"
string cfgfile="WTerm.cfg"
cpchanged=FALSE

def win:window  
def run,wstyle:int

wstyle = @minbox|@maxbox|@size ' thick borders

' Create window:  
OpenWindow win,0,0,600,400,wstyle,0,"  WTerm  -  Set up comport!",&messages  

BeginMenu win
' File menu:
MenuTitle "File"
MenuItem "Save setup",0,1
MenuItem "Exit",0,2

' Seting up menu:
MenuTitle "Setup"
BeginPopup "Select Port"
MenuItem "Com1",1,3
MenuItem "Com2",1,4
MenuItem "Com3",1,5
MenuItem "Com4",1,6
MenuItem "Com5",1,7
MenuItem "Com6",1,8
MenuItem "Com7",1,9
MenuItem "Com8",1,10
MenuItem "Com9",1,11
MenuItem "Com10",1,12
MenuItem "Com11",1,13
MenuItem "Com12",1,14
MenuItem "Com13",1,15
MenuItem "Com14",1,16
MenuItem "Com15",1,17
MenuItem "Com16",1,18
EndPopup

BeginPopup "Select baudrate"
MenuItem "   110",0,19
MenuItem "   300",0,20
MenuItem "   600",0,21
MenuItem "  1200",0,22
MenuItem "  2400",0,23
MenuItem "  4800",0,24
MenuItem "  9600",0,25
MenuItem " 14400",0,26
MenuItem " 19200",0,27
MenuItem " 28800",0,28
MenuItem " 38400",0,29
MenuItem " 57600",0,30
MenuItem "115200",0,31
EndPopup

BeginPopup "Select Bits"
MenuItem "7",0,32
MenuItem "8",0,33
EndPopup

BeginPopup "Select Parity"
MenuItem "No",0,34
MenuItem "Odd",0,35
MenuItem "Even",0,36
EndPopup

BeginPopup "Select Stopbits"
MenuItem "1",0,37
MenuItem "2",0,38
EndPopup

BeginPopup "Local Echo"
MenuItem "On",0,39
MenuItem "Off",0,40
EndPopup

MenuTitle "Help"
MenuItem "User Guide",0,98
MenuItem "About",0,99
EndMenu

'Get window "estate":
GETCLIENTSIZE win,lft,tp,wdt,hgt

'Edit controls:
' RX-window:
Control win,@EDIT,"",0,0,wdt,hgt-80,@CTEDITRO|@CTEDITMULTI|@CTEDITRETURN|@CTEDITAUTOV|@VSCROLL,201
' TX-window:
Control win,@EDIT,"",0,hgt-80,wdt,80,@CTEDITMULTI,202
SETCONTROLNOTIFY(win,202,0,1)
' Set focus to TX-window:
SetFocus win,202

FindComPorts(win)
LoadSetup(win,GETSTARTPATH() + cfgfile)
StartTimer win,50

run = 1  
WAITUNTIL run = 0  
CLOSEWINDOW win  
END  

'
SUB messages(),int
'----------------------------------------------------------------------------------------
' Main loop
'----------------------------------------------------------------------------------------
SELECT @CLASS

CASE @IDTIMER
rx$=""
rx$ = GetRxD(pb.comport)
if rx$<>"" then AppendEdit(win,201,rx$,false)
' Set focus to TX-window:
SetFocus win,202

CASE @IDSIZE 'Resize window
Resize(win)
AppendEdit(win,201,"",FALSE)

CASE @IDMENUPICK
select @menunum

case 1 'Save setup
SaveSetup(cfgfile,win)
case 2 'Exit program
CleanUp(win)

case 3
pb.comport  = COM1:SetUpComPort()
case 4
pb.comport  = COM2:SetUpComPort()
case 5
pb.comport  = COM3:SetUpComPort()
case 6
pb.comport  = COM4:SetUpComPort()
case 7
pb.comport  = COM5:SetUpComPort()
case 8
pb.comport  = COM6:SetUpComPort()
case 9
pb.comport  = COM7:SetUpComPort()
case 10
pb.comport  = COM8:SetUpComPort()
case 11
pb.comport  = COM9:SetUpComPort()
case 12
pb.comport  = COM10:SetUpComPort()
case 13
pb.comport  = COM11:SetUpComPort()
case 14
pb.comport  = COM12:SetUpComPort()
case 15
pb.comport  = COM13:SetUpComPort()
case 16
pb.comport  = COM14:SetUpComPort()
case 17
pb.comport  = COM15:SetUpComPort()
case 18
pb.comport  = COM16:SetUpComPort()

case 19
pb.baud = 110:SetUpComPort()
case 20
pb.baud = 300:SetUpComPort()
case 21
pb.baud = 600:SetUpComPort()
case 22
pb.baud = 1200:SetUpComPort()
case 23
pb.baud = 2400:SetUpComPort()
case 24
pb.baud = 4800:SetUpComPort()
case 25
pb.baud = 9600:SetUpComPort()
case 26
pb.baud = 14400:SetUpComPort()
case 27
pb.baud = 19200:SetUpComPort()
case 28
pb.baud = 28800:SetUpComPort()
case 29
pb.baud = 38400:SetUpComPort()
case 30
pb.baud = 57600:SetUpComPort()
case 31
pb.baud = 115200:SetUpComPort()

case 32
pb.bits = 7:SetUpComPort()
case 33
pb.bits = 8:SetUpComPort()

case 34
pb.parity = 0:SetUpComPort()
case 35
pb.parity = 1:SetUpComPort()
case 36
pb.parity = 2:SetUpComPort()

case 37
pb.stopbits= 1:SetUpComPort()
case 38
pb.stopbits = 2:SetUpComPort()

case 39 ' echo on
pb.localecho = EchoOn:SetUpComPort()
case 40 ' echo off
pb.localecho = EchoOff:SetUpComPort()

case 98 ' help
system "WTerm.chm"
case 99 ' about
About(win)

endselect

CASE @idcreate  
centerwindow win  

CASE @idclosewindow  
SaveSetup(GETSTARTPATH() + cfgfile,win)
StopTimer win 'stop timer
run = 0  

CASE @idcontrol  
select @controlID  
case 1  
run = 0  
CASE 202
select @NotifyCode
case @ENENTERKEY
Tx$=GetControlText win,202
PutTxD(pb.comport,Tx$+chr$(13))
if pb.localecho=TRUE then AppendEdit(win,201,Tx$,TRUE)
SetControlText win,202,""
endselect
endselect  

ENDSELECT  
RETURN  0
ENDSUB


'
SUB Resize(WINDOW wnd)
'----------------------------------------------------------------------------------------
' Resize window
'----------------------------------------------------------------------------------------
getclientsize win,l,t,w,h
setsize win,l,t,w,h-80,201 'RX-window
setsize win,l,h-80,w,h-10,202 'TX-window
RETURN
ENDSUB

'
SUB GetRxD(CP:int),string
'----------------------------------------------------------------------------------------
' Reads whatever arrives at the comport
' PARAMETERS: CP - Comport number
' NOTE: Need MB Serial Library to work
'----------------------------------------------------------------------------------------
dim ret[2048]:istring
dim pRet:pointer
dim cnt,buf:int
pRet = ret
if IWGetBytesWaiting(CP)
buf = IWGetBytesWaiting(CP)
cnt = IWReadPort(CP,pRet,buf)
ret[cnt] = ""
else
ret=""
endif
RETURN ret
ENDSUB

'
SUB PutTxD(port:int,text:string)
'----------------------------------------------------------------------------------------
' Send keyboard input
'----------------------------------------------------------------------------------------
if text="" then text=chr$(13)
IWSendPort(pb.comport,text,len(text))
RETURN
ENDSUB


'
Sub AppendEdit(window win,int id,string text,int addNL)
'----------------------------------------------------------------------------------------
' Appends text in an edit control
'----------------------------------------------------------------------------------------
int _textlen
_textlen =SendMessage(win, WM_GETTEXTLENGTH,0,0,id)
ControlCmd win, id, @EDSETSELECTION, _textlen, _textlen
if(addNL)
ControlCmd win, id, @EDREPLACESEL, text + "\n"
else
ControlCmd win, id, @EDREPLACESEL, text
endif
RETURN
ENDSUB

'
SUB FindComPorts(WINDOW wnd)
'----------------------------------------------------------------------------------------
' Find available comports, and correct menues accordingly
'----------------------------------------------------------------------------------------
int i

for i = 1 to 16
if IWOpenPort(i,115200)
EnableMenuItem wnd,i+2,TRUE
IWClosePort(i)
else
EnableMenuItem wnd,i+2,FALSE
endif
next i
RETURN
ENDSUB

'
SUB SetUpComPort()
'----------------------------------------------------------------------------------------
' Set ComPort
'----------------------------------------------------------------------------------------
string cp

if pb.localecho=TRUE then echo=" - Local Echo: ON"
if pb.localecho=FALSE then echo=" - Local Echo: OFF"
IWClosePort(pb.comport)
IWOpenPort(pb.comport,pb.baud)
IWSetReadTimeout(pb.comport,-1)
IWSetPort(pb.comport,pb.bits,pb.parity,pb.stopbits)

cp= str$(pb.comport)
ctext = "  WTerm   -   Port status = COM"+right$(cp,len(cp)-1)+":"+str$(pb.baud)+","+right$(str$(pb.bits),1)+ ","+tpar[pb.parity]+","+ right$(str$(pb.stopbits),1) + echo
SetCaption win,ctext
RETURN
ENDSUB

'
SUB LoadSetup(WINDOW wnd,string sFilePath)
'----------------------------------------------------------------------------------------
'  Load setup from file - or use default settings
'----------------------------------------------------------------------------------------
FILE iFile
string buf
OPENFILE(iFile,sFilePath,"R")
if iFile
READ(iFile,buf) : pb.comport   = val(buf)
READ(iFile,buf) : pb.baud      = val(buf)
READ(iFile,buf) : pb.bits      = val(buf)
READ(iFile,buf) : pb.parity    = val(buf)
READ(iFile,buf) : pb.stopbits  = val(buf)
READ(iFile,buf) : pb.localecho = val(buf)
endif
CLOSEFILE iFile
if pb.comport = 0
AppendEdit(win,201,"  *****   COMPORT PARAMETERS MISSING   *****",TRUE)
else
SetUpComPort()
endif
RETURN
ENDSUB

'
SUB SaveSetup(sFilePath:string,wnd:window)
'----------------------------------------------------------------------------------------
'  Save setup to file
'----------------------------------------------------------------------------------------
FILE iFile
OPENFILE(iFile,sFilePath,"W")
if iFile
WRITE iFile,str$(pb.comport)
WRITE iFile,str$(pb.baud)
WRITE iFile,str$(pb.bits)
WRITE iFile,str$(pb.parity)
WRITE iFile,str$(pb.stopbits)
WRITE iFile,str$(pb.localecho)
CLOSEFILE iFile
Else
AppendEdit(win,201,"Unable to save Config file",TRUE)
EndIf
RETURN
ENDSUB

'
SUB CleanUp(window wnd)
'----------------------------------------------------------------------------------------
' Clean Up and END program
'----------------------------------------------------------------------------------------
StopTimer wnd
SaveSetup(GETSTARTPATH()+cfgfile,wnd)
IWClosePort(pb.comport)
RETURN
ENDSUB

'
'----------------------------------------------------------------------------------------
' About
'----------------------------------------------------------------------------------------
SUB About(window wnd)
MESSAGEBOX(wnd,_
"       This is FREEWARE from LA2PJ"+chr$(13),_
"About WTerm",@MB_OK|@MB_ICONINFORMATION)
RETURN
ENDSUB




Support Amateur Radio  -  Have a ham  for dinner!

Bill-Bo

Egil,

You need to indicate what OS your program
will operate in. I does not work on my Win7
64-bit HP laptop. But of course, it only uses
USB ports. I have not tried it on my XP
system.

The program locks up on me and it is hard to
exit. Just get "Not Responding" popups and
keep clicking until it closes.

Help,

Bill

RitchieF


Egil

Quote from: Bill-Bo on October 23, 2014, 08:18:26 AMThe program locks up on me and it is hard to
exit. Just get "Not Responding" popups and
keep clicking until it closes.

Help,

Bill

Sorry to hear that Bill.

It may be the User Access Control  blocking the program.
Tonight I have tried the compiled version on my neighbours computers, and have started the program on XP, Vista, Win7 and Win8.1 .
On the Vista system we had to right click on the exe and choose Properties. The program was blocked. By removing this, the program played ok. Maybe you can try the same?

And if you have IWB or EB installed on your computer, maybe you can try to recompile it?

Otherwise I have no solution available.

Support Amateur Radio  -  Have a ham  for dinner!

Egil

Bill,
There is two more things...
The program will not find any comports unless a USB/RS-232 converter is connected.
If the converter cable is connected, you can check Windows Device Manager to see that the Comport driver is loaded. And if the driver is old, search the internet for a newer version.



Edited to remove typos...
Support Amateur Radio  -  Have a ham  for dinner!

Bill-Bo