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
'********************