SimTerm, back to basics

For issues with communication ports, protocols, etc.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

SimTerm, back to basics

Post by dasyar »

My other program, FBterm, is a little complicated and the thread is very long, so I thought I would start this one. Below is a very basic serial terminal program.

I thought it would be very important to be able to make the program compilable in both Windows and Linux, without having to add stuff after the fact. I am working in a Linux system and do not have access to a Windows system, at the moment, so I can only vouch for a Linux build.

As mentioned, this is a very basic serial terminal program, with the port and BAUD access hard coded. You will have to change those
values to meet your needs and then compile the program.

The next improvement to this program is to figure out a way, in a clean manner, to set the port and BAUD value from within the program itself. Since most people would probably be using this on a GUI desktop, it would have to be done from within the program. So, does anybody have any suggestions as to how this could be accomplished? Any other ideas are also welcome.

Code: Select all

'SimTerm.bas
'Simple terminal program
'Mar 13, 2016
'
' SimTerm version .01
'
' Revision .01
'   Setup for Linux or Windows compile.
'   BAUD and COM port, hard coded.

' In Linux, needs this to run in GUI desktop, and not just command line.
Screen 12
Width 80,30


Dim As String Key,buffer
' Common for Windows and Linux
Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200", _
            "38400","57600","115200"}

Print "SimTerm, a simple terminal program."

'Change the Com port and BAUD rate settings to what you need
#ifdef __FB_WIN32__   ' Microsoft Windows
Dim As Long b=7, p=2  ' Settings for Windows
Dim As String port(0 to 9) = {"COM1:","COM2:","COM3:","COM1:","COM1:", _
             "COM1:","COM1:","COM1:","COM1:","COM1:"}
'Open Com "COM1:115200,n,8,1,cs0,ds0,cd0,rs" As #1
Open Com port(p) & baud(b) & ",n,8,1,cs0,ds0,cd0,rs" As #1
Print "COM1 115200 BAUD"
#else   ' Linux
Dim As Long b=7,p=3  ' Settings for Linux
Dim As String port(0 to 5) = {"/dev/tty0:","/dev/tty1:", _
             "/dev/ttyAMA0:","/dev/ttyUSB0:","/dev/ttyUSB1:"}
Open Com port(p) & baud(b) & ",n,8,1,cs0,ds0,cd0,rs" As #1
#Endif
' Check port status
	If Err <> 0 Then
		Print "Error opening",port(p);
		Sleep 2000
		Cls
		GoTo EndAll
	Else
		Print "Open " & "port:" & port(p) & " BAUD:" & baud(b)
	End If

Print "<Esc> to quit program."



'main serial send/receive routine
Do

   Key = Inkey

   If Key = Chr(27) Then    'Esc key
      Exit Do               'End the program
   End If

   If Key <> "" Then
      If Key = Chr(13) Then  'This handles CR/LF
         Print #1, Chr(13);
      Else
         Print #1,Key;        'Send the char
         Print Key;           'This is the echo
      End If
   End If

'Check for input
    While Loc(1) > 0
        buffer = Input(Loc(1),#1) 'Grab a char
        Print buffer;             'Print the char
    Wend

Sleep 1
Loop

' If a port is not available
EndAll:
'close the port(s)
Close

Print "Unable to open",port(p);

End
'********************
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SimTerm, back to basics

Post by MrSwiss »

@dasyar,

SimTerm simplified Version:

Code: Select all

'SimTerm.bas
'Simple terminal program
'by dasyar
'
' SimTerm version .01
'
' Revision .02, MrSwiss, Mar 14. 2016
'   Setup for Linux or Windows compile.
'   BAUD and COM port, hard coded.

' In Linux, needs this to run in GUI desktop, and not just command line.
Screen 12
Width 80,30


Dim As String Key, buffer
' Common for Windows and Linux
Dim As String baud(0 To 7) = {"1200","2400","4800","9600","19200","38400","57600","115200"}


#Ifdef __FB_WIN32__   ' Microsoft Windows

	Dim As Long b=7, p=2  ' Settings for Windows
	Dim As String port(0 to 9) = {"COM1:","COM2:","COM3:","COM4:","COM5:","COM6:","COM7:","COM8:","COM9:","COM10:"}

#else   ' Linux

	Dim As Long b=7,p=3  ' Settings for Linux
	Dim As String port(0 to 5) = {"/dev/tty0:","/dev/tty1:","/dev/ttyAMA0:","/dev/ttyUSB0:","/dev/ttyUSB1:"}
	Dim As String dprt(0 to 5) = {"tty0:","tty1:","ttyAMA0:","ttyUSB0:","ttyUSB1:"}	' Display String only!

#EndIf


Open Com port(p) & baud(b) & ",n,8,1,cs0,ds0,cd0,rs" As #1
' Check port status
If Err Then
	Print "Error opening ";port(p);
	Sleep 2000 : Cls : Close : End	' Don't use GoTo !!!
Else
	#Ifdef __FB_WIN32__
		Print "Open Port: " & port(p) & " Baud: " & baud(b)
	#Else	' on LINUX: use dprt(p) Array for Display
		Print "Open Port: " & dprt(p) & " Baud: " & baud(b)
	#EndIf  
End If

Print "SimTerm, a simple Terminal Program. [Esc] to quit Program."	' also X click !

'main serial send/receive routine
Do
	Key = Inkey

	If Key <> "" Then		' [Enter] is also sent ! (so, no check needed !)	
		Print #1,Key;        'Send the char
		Print Key;           'This is the echo
	End If
    While Loc(1) > 0
        buffer = Input(Loc(1),#1)	'Grab a char
        Print buffer; : buffer = ""	'Print the char : clear Buffer
    Wend

	Sleep 10, 1
Loop Until key = Chr(255,107) OrElse key = Chr(27)  ' also: chr(255)+"k"

' close, quit
Close : End
'********************
This explains the use of different Arrays in Linux ...
No more GoTo, etc. ...
Last edited by MrSwiss on Mar 14, 2016 19:31, edited 1 time in total.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: SimTerm, back to basics

Post by dasyar »

It looks like MrSwiss provided a very good and clean revision, Thanks.

As to the "click on X", if you are referring to the 'X' in the actual window, upper right hand on Windows, and upper left hand on Linux, that does not work. I never could figure out why, in a GUI, at least, you would think that would work automatically with out having to add programming code, but even in this case Chr(255,109) is not working as expected.

I guess I will let this stew for a while while I think about how to add the port and BAUD selection, in a simplified manner.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SimTerm, back to basics

Post by MrSwiss »

dasyar wrote:As to the "click on X", if you are referring to the 'X' in the actual window, upper right hand on Windows, and upper left hand on Linux, that does not work. I never could figure out why, in a GUI, at least, you would think that would work automatically with out having to add programming code, but even in this case Chr(255,109) is not working as expected.
Thanks, the Fault is 109 should be 107 = "k".
Corrected the Code in prev. Post. (tested on WIN, works as expected)

The OS returns VKey's (virtual Keys) on certain clicks, so just a check on those needed ...
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: SimTerm, back to basics

Post by dasyar »

Just did the change in the program, and it works as expected for the Linux version. So, now you can use the <Esc> or just click on the 'X' in the window. It is interesting why the window '-' click works without adding any code?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SimTerm, back to basics

Post by MrSwiss »

dasyar wrote:It is interesting why the window '-' click works without adding any code?
This is "built in", handled by OS, no additional Code needed. The "maximize" is different, it tries to go "full screen", which would need additional Code to "re-size" the Content of the Window (dynamic Content, instead of static Content).
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: SimTerm, back to basics

Post by dasyar »

I have been thinking about some different ways of changing the port and BAUD when you have started up the program and the possibility of starting the program with a specified port and BAUD.

I am not sure that you could do something like ./SimTerm /dev/ttyUSB0 115200, from the command line, with a program developed with freeBASIC. But, that scenario would only be good for a command line SimTerm. Need to cover the GUI aspect also.

The other possibility is doing it the way minicom does it, maybe ctrl-S to bring up a small window that allows you to change the Settings. Also, I am not sure that freeBASIC would be able to accomplish something like this, in an efficient manner.

I guess I would need some other feasible suggestions or examples.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SimTerm, back to basics

Post by MrSwiss »

dasyar wrote:I am not sure that you could do something like ./SimTerm /dev/ttyUSB0 115200, from the command line, with a program developed with freeBASIC.
A sort of "semi"-static Behaviour could be realized with a .INI File.
Any Editor could be used, to change the Pre-Sets stored in .INI.

Better to use than Command Line, but less flexible than with "internal" Configuration.

Configuration-Dialog(s), done cross-compatible (WIN/LIN), are more complex to realize.
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: SimTerm, back to basics

Post by dasyar »

Better to use than Command Line, but less flexible than with "internal" Configuration.
Does freeBASIC have a way of doing './SimTerm /dev/ttyUSB0 115200'? I browsed through the documentation, but did not stumble on any particular method. If somebody could point me to the page where this would be described, I could give it a try, as an experiment.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SimTerm, back to basics

Post by MrSwiss »

FB-Manual wrote: Command() - Statement (FB)
Returns command line parameters used to call the program

Syntax
Declare Function Command ( ByVal index As Long = -1 ) As String

Usage
result = Command[$]( [ index ] )

Parameters
index
Zero-based index for a particular command-line argument.

Return Value
Returns the command-line arguments(s).
Not certain, whether this answers your Question ...
It returns the Command, as well as any additionally given Parameters from the Command Line (Terminal/Console).
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: SimTerm, back to basics

Post by dasyar »

I did some more checking in the freeBASIC docs, and I did not find anything about how to start a program, in the command line, with specific requirements. So, the other method is to do the requirements within the running program itself. Not sure if I want to do the mouse and static menu item selection again. Since I am not getting any feed back on this, I can only guess that people are completely satisfied with SimTerm as it is now.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SimTerm, back to basics

Post by MrSwiss »

dasyar wrote:I did not find anything about how to start a program, in the command line, with specific requirements.
This has nothing to do with Programming at all. This is OS know-how.
  • open a Terminal Session (Linux)
    navigate to the correct Location (DIR/Folder, where Program is located)
    type the Command (Parameters, etc.)
    hit [Enter] (also called [Return])
Alternative way: write a Shell-Script ...
Read the Linux-Distro's "man Pages" for more Information on such things.
Last edited by MrSwiss on Mar 16, 2016 13:04, edited 1 time in total.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: SimTerm, back to basics

Post by St_W »

dasyar wrote:I did some more checking in the freeBASIC docs, and I did not find anything about how to start a program, in the command line, with specific requirements. So, the other method is to do the requirements within the running program itself. Not sure if I want to do the mouse and static menu item selection again.
I don't quite get what you mean, but try to answer nevertheless. To start a program you can use the SHELL command for example. You can query for command line parameters using the COMMAND() command e.g. for replacing the statically defined port number. If you want mouse handling there's no other solution than implementing it in your program.

//edit: Ah, after reading MrSwiss's answer, some additional information: I did not think about that as I cannot believe anybody who aims to do some programming does not know how to launch a program with parameters on the command line. Anyway; if you're on windows press Windows-key + R or run cmd.exe
dasyar wrote:Since I am not getting any feed back on this, I can only guess that people are completely satisfied with SimTerm as it is now.
You program is (1) not for a every-day use-case and (2) very minimalistic (few features), thus don't expect feedback from a lot of users. I'd rather say you are quite lucky if you get any users at all. Look at existing terminal programs like:
HTerm http://www.der-hammer.info/terminal/
TeraTerm https://en.osdn.jp/projects/ttssh2/
PuTTy http://www.putty.org/
Why should a user use your program instead?
dasyar
Posts: 372
Joined: Dec 04, 2008 15:31

Re: SimTerm, back to basics

Post by dasyar »

Below is my attempt at doing some command line parameter programming. The program compiles and runs, but I keep getting "Error opening /dev/ttyUSB0", when I do ./cltest1 /dev/ttyUSB0 115200. Do I have to do something else with the arg1 and arg2 variables. They are Dim As String, so that should be the proper form for the Open Com command, I would think.

Code: Select all

'cltest1.bas
' March 16, 2016
' Test for command line paramaters

Screen 12
Width 80,30

Dim As Integer i = 1

Dim As String arg1 = Command(1)
Dim As String arg2 = Command(2)

Dim As String Key

If arg1 = "" Then
	print "Usage : SimTerm port baud"
	Sleep 2000:Cls : Close : End
else
	Open Com arg1 & arg2 & ",n,8,1,cs0,ds0,cd0,rs" As #1
End If
If Err Then
	Print "Error opening ";arg1;
	Sleep 2000 : Cls : Close : End   ' Don't use GoTo !!!
Else
	Print "Open Port: " & arg1 & " Baud: " & arg2
End If

Do
	Key = Inkey

	Sleep 10,1
Loop Until key = Chr(255,107) OrElse key = Chr(27)



Close: End

'Print " :" & arg1
'Print " :" & arg2
'Dim As Integer i = 1
'Do
'    Dim As String arg = Command(i)
'    If Len(arg) = 0 Then
'        Exit Do
'    End If
'    Print "command line argument " & i & " = """ & arg & """"
'    i += 1
'Loop
'If i = 1 Then
'    Print "(no command line arguments)"
'End If'
'Sleep

'End
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: SimTerm, back to basics

Post by MrSwiss »

dasyar wrote:./cltest1 /dev/ttyUSB0 115200
Well, you seem to forget the : ... Colon
./cltest1 /dev/ttyUSB0: 115200 ... on the Command Line.
Post Reply