MyTerminal prog

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

MyTerminal prog

Postby dasyar » Dec 06, 2008 18:11

I am trying to create a small terminal program that I will be running on a dos system, so that is why I am here. I have two computers hooked up with a null modem cable, and on one end I have Hyperterminal on COM 1, and on the other end I have MyTerminal prog on the dos system.

The problem that I am having with the prog below, I can not figure out how to capture the keypress on the other end. With the prog below, on the dos side, I get a 0 every half a second, even when I press a key on the other end. But I do get an "A" on the Hyperterminal side, so I have communication in one direction. I tried using a 'wait &h3f8, ByteIn', to see if I could get the prog to wait for the keypress from the other side, that did not work out.

I guess I need some ideas as to how this should be approached.

Thanks

Code: Select all

' Test2.bas
' MyTerminal.exe

' Grabs the exiting values of the command prompt window
locate ,,0
  dim as integer oldcolor = color
  dim as integer oldwidth = width

'New code goes here

  dim ByteIn as byte

  Open Com "COM1:9600,N,8,1" As 1

  if Err <> 0 then
    print "Error opening COM1:"
    sleep
    end
  end if

  do
    input #1, ByteIn             '
      print ByteIn               'Print the char it received
    print #1, "A"                'Test to see if the other end got it
      sleep 500,0                'Slow the do loop down
'  loop until Inkey$ = Chr$(27)
  loop until inkey = "'"         'This char " ' " stops the program

  close #1

' Restore the command prompt window to the old settings
  width oldwidth and &hFFFF, oldwidth shr 16
  color oldcolor and &hFFFF, oldcolor shr 16
  view print 1 to oldwidth shr 16
'  cls
phishguy
Posts: 1200
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Postby phishguy » Dec 06, 2008 18:21

Try something like this.

Code: Select all

dim as string key,buffer
Open Com "COM1:9600,n,8,1,cs0,ds0,cd0,rs" As #1

'main serial send/receive routine
Do

       Key = Inkey$

if key = chr$(27) then
exit do
end if

    If Key <> "" Then
               Print #1,Key;
    End If

    While Loc(1) > 0
        buffer = Input$(Loc(1),#1)
        Print buffer;
    Wend

Sleep 1,0
Loop   
Last edited by phishguy on Dec 06, 2008 20:22, edited 2 times in total.
dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Postby dasyar » Dec 06, 2008 19:24

Thanks phishguy, the code snippet worked as expected. Now I have the concept, I can start to add some features, after I figure what is going on with the while loop.
phishguy
Posts: 1200
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Postby phishguy » Dec 06, 2008 20:24

There was a small error in my program that would allow the escape character that ends the program to be sent out the serial port. I edited my previos post to correct that problem.
dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Postby dasyar » Dec 09, 2008 11:54

Below is my latest version of FBterm.bas, added some minor details; on to bigger concepts.

Since I plan on using this prog with my iRobot Create, I have to figure out some way of sending pure numbers, as opposed to ASCII numbers to the robot, but I still want to keep the prog as simple as possible. And eventually I will have to deal with the info that the robot sends me, which is in a byte format.

I have used RealTerm, that prog deals with the problem by having a check box for which one you want to use, 'numbers or ASCII', but I do not want to develop a graphics prog just yet. So, I am open to some suggestions as to how to accomplish this. Just a reminder, this will be used in a DOS system.

Thanks


Code: Select all

' FBterm.bas
' FBterm.exe
' December 6, 2008
' Ver .03

' Grabs the exiting values of the command prompt window
'1 = cursor on, and 0 = cursor off
locate ,,1                         'This locates the cursor at 0,0 position
  dim as integer oldcolor = color
  dim as integer oldwidth = width
cls

'New code goes here


dim as string Key,buffer         'Define for a string value

'This is example for a modem connection
'open com "COM1:9600,n,8,1,cs0,ds0,cd0,rs" as #1

'Open the Com port 
open com "COM1:9600,n,8,1" as #1

'Check if port was opened
if Err <> 0 then
    print "Error opening COM1:";
    sleep
    end
end if

'Titles
print "FBterm, A terminal program"
print "Press the ESC key to QUIT"


do
    key = inkey$

'Check for the esc key to quit the program
    if Key = chr$(27) then         'Is chr$(27) the esc Key, yes it is.
    exit do
    end if

'Send the keypress
    if Key <> "" then
       if Key = chr$(13) then      'Check if CR has been pressed         
          print #1, chr$(13)       'Send the CR
          print ""                 'Do a CR
       end if
        print #1,Key;               'This sends a char, print with ; = no new line
        print Key;                  'Echo the keypress
    end if

'Receive and display the char
    while Loc(1) > 0
        buffer = input$(Loc(1),#1)  'This receives a char
        print buffer;
    wend

sleep 1,0
loop


' Restore the command prompt window to the old settings
  width oldwidth and &hFFFF, oldwidth shr 16
  color oldcolor and &hFFFF, oldcolor shr 16
  view print 1 to oldwidth shr 16
'  cls

phishguy
Posts: 1200
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Postby phishguy » Dec 09, 2008 14:23

Note that -

Code: Select all

Print #1, Chr$(13)

will send 2 carriage returns.

You would want to use -

Code: Select all

Print #1,


or

Code: Select all

Print #1,chr$(13);


To send the 'number' and not the ASCII representation of the number, you would do this -

Code: Select all

dim numtosend as ubyte
numtosend = 128 ' or &h80
print #1,chr$(numtosend);
 
' or prompt for input of number
input "Enter number to send (0 - 255) ";numtosend
print #1,numtosend;


You could also trap a specific key in your inkey loop that would input the number.

Code: Select all

' FBterm.bas
' FBterm.exe
' December 6, 2008
' Ver .03

' Grabs the exiting values of the command prompt window
'1 = cursor on, and 0 = cursor off
Locate ,,1                         'This locates the cursor at 0,0 position
Dim As Integer oldcolor = Color
Dim As Integer oldwidth = Width
Cls

'New code goes here


Dim As String Key,buffer          'Define for a string value
Dim As Ubyte num
'This is example for a modem connection
'open com "COM1:9600,n,8,1,cs0,ds0,cd0,rs" as #1

'Open the Com port 
Open Com "COM1:9600,n,8,1" As #1

'Check if port was opened
If Err <> 0 Then
    Print "Error opening COM1:";
    Sleep
    End
End If

'Titles
Print "FBterm, A terminal program"
Print "Press the ESC key to QUIT"


Do
    Key = Inkey$
   
    'Check for the esc key to quit the program
    If Key = Chr$(27) Then         'Is chr$(27) the esc Key, yes it is.
        Exit Do
    End If
   
   
    'Send the keypress
    If Key <> "" Then
        If Key = Chr$(13) Then      'Check if CR has been pressed         
            Print #1, Chr$(13);      'Send the CR
            Print                  'Do a CR
        End If
       
        'Enter number to send if # pressed
        If key = "#" Then
            Input "Enter Number to send (0 - 255) ";num
            Print #1,num;
        Else
           
            Print #1,Key;               'This sends a char, print with ; = no new line
            Print Key;                  'Echo the keypress
        End If
       
    End If
   
    'Receive and display the char
    While Loc(1) > 0
        buffer = Input$(Loc(1),#1)  'This receives a char
        Print buffer;
    Wend
   
    Sleep 1,0
Loop


' Restore the command prompt window to the old settings
Width oldwidth And &hFFFF, oldwidth Shr 16
Color oldcolor And &hFFFF, oldcolor Shr 16
View Print 1 To oldwidth Shr 16
'  cls



In the following commented code, your statement isn't exactly true. The extra switches are to allow proper communications with a three wire connection (TXD, RXD, and GND). I find that I use that configuration most of the time as it will ignore the handshaking signals.

Code: Select all

'This is example for a modem connection
'open com "COM1:9600,n,8,1,cs0,ds0,cd0,rs" as #1

dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Postby dasyar » Dec 09, 2008 21:45

I added a trap the F1 key to provide a way of sending out numbers, instead of ASCII. In that bit of code I placed 'print "Enter a number to send (0 - 255)" ', for some reason this is not getting out to the console, all I see is an ' ; '. I tried using a 'write command', and the same thing happens, no text. I am wondering if it has something to do with 'input' statement directly after it, the documentation does not mention any kind of side affect.

Thanks

Code: Select all

' FBterm.bas
' FBterm.exe
' December 6, 2008
' Ver .04

' Grabs the exiting values of the command prompt window
'1 = cursor on, and 0 = cursor off
locate ,,1                         'This locates the cursor at 0,0 position
  dim as integer oldcolor = color
  dim as integer oldwidth = width
cls

'New code goes here


dim as string Key,buffer         'Define for a string value
dim numtosend1 as Ubyte
dim numtosend2 as Ubyte
dim numtosend3 as Ubyte

'Open the Com port
open com "COM1:9600,n,8,1,cs0,ds0,cd0,rs" as #1


'Check if port was opened
if Err <> 0 then
    print "Error opening COM1:";
    sleep
    end
end if

'Titles
print "FBterm, A terminal program"
print "Press the ESC key to QUIT"
print "Press F1 to send a number"


do
    key = inkey$

'Check for the esc key to quit the program
    if Key = chr$(27) then         'Is chr$(27) the esc Key, yes it is.
    exit do
    end if

'Check for F1
    if Key = chr$(59) then         'the F1 key
    print "Enter a number to send (0 - 255)"    'Why isn't this printing to the console?
        while numtosend1 > 1
            input numtosend1
            print,numtosend1
                if numtosend1 = 13 then
                exit do
                end if
            input numtosend2
            print,numtosend2
                if numtosend2 = 13 then
                exit do
                end if
            input numtosend3
            print,numtosend3
                if numtosend3 = 13 then
                exit do
                end if
            exit do
        wend
        print #1, numtosend1,numtosend2,numtosend3
    end if

'Send the keypress
    if Key <> "" then
       if Key = chr$(13) then      'Check if CR has been pressed         
          print #1, chr$(13)       'Send the CR and do a CR
          print chr$(10);          'Send a linefeed
       end if
        print #1,Key;               'This sends a char, print with ; = no new line
        print Key;                  'Echo the keypress
    end if

'Receive and display the char
    while Loc(1) > 0
        buffer = input$(Loc(1),#1)  'This receives a char
        print buffer;
    wend

sleep 1,0
loop


' Restore the command prompt window to the old settings
  width oldwidth and &hFFFF, oldwidth shr 16
  color oldcolor and &hFFFF, oldcolor shr 16
  view print 1 to oldwidth shr 16
'  cls

[quote][/quote]
phishguy
Posts: 1200
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Postby phishguy » Dec 09, 2008 21:53

F1 is chr$(255) + chr$(59). It sends out 2 characters.

Something like this would work. Enter numbers like this 11 123 45 0.

Code: Select all

' FBterm.bas
' FBterm.exe
' December 6, 2008
' Ver .04

' Grabs the exiting values of the command prompt window
'1 = cursor on, and 0 = cursor off
Locate ,,1                         'This locates the cursor at 0,0 position
Dim As Integer oldcolor = Color
Dim As Integer oldwidth = Width
Cls

'New code goes here

Dim As Integer pos1,pos2
Dim As String Key,buffer         'Define for a string value
Dim numtosend As String
Dim numtosend2 As Ubyte


'Open the Com port
Open Com "COM1:9600,n,8,1,cs0,ds0,cd0,rs" As #1


'Check if port was opened
If Err <> 0 Then
    Print "Error opening COM1:";
    Sleep
    End
End If

'Titles
Print "FBterm, A terminal program"
Print "Press the ESC key to QUIT"
Print "Press F1 to send a number"


Do
    Key = Inkey$
   
    'Check for the esc key to quit the program
    If Key = Chr$(27) Then         'Is chr$(27) the esc Key, yes it is.
        Exit Do
    End If
   
    'Check for F1
    If Key = Chr$(255)+Chr$(59) Then         'the F1 key
       
        Input "Enter numbers to send (0 - 255) space delimited";numtosend   
        pos1 = Instr(numtosend," ")
        numtosend2 = Val(Left$(numtosend,pos1))
        Print #1,chr$(numtosend2);
       
        Do While pos1 > 0
           
            pos2 = pos1
            pos1 = Instr(pos1+1 ,numtosend," ")
            numtosend2 = Val(Mid$(numtosend,pos2+1 ,pos1-pos2))
       
            Print #1, chr$(numtosend2);
        Loop
       
    Else
       
        'Send the keypress
        If Key <> "" Then
            If Key = Chr$(13) Then      'Check if CR has been pressed         
                Print #1, Chr$(13)       'Send the CR and do a CR
                Print Chr$(10);          'Send a linefeed
            End If
            Print #1,Key;               'This sends a char, print with ; = no new line
            Print Key;                  'Echo the keypress
        End If
    End If
    'Receive and display the char
    While Loc(1) > 0
        buffer = Input$(Loc(1),#1)  'This receives a char
        Print buffer;
    Wend
   
    Sleep 1,0
Loop


' Restore the command prompt window to the old settings
Width oldwidth And &hFFFF, oldwidth Shr 16
Color oldcolor And &hFFFF, oldcolor Shr 16
View Print 1 To oldwidth Shr 16
'  cls

dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Postby dasyar » Dec 10, 2008 21:46

I just noticed the revisions to my horrendous do ... while loop, I always start out with some logical steps, then I try to work the code. I appreciate your efforts phishguy. I will take some time to digest your code, and see how it makes sense to me.

I was thinking about how to use F2 to select the port and baud rate that I would like to use. I tried:

Code: Select all

dim as string portbaud
portbaud = "COM1:9600"

open com "portbaud, ..." as #1


The above code compiled without an error, but when I ran the program I got my "error opening COM1:". I guess what I might have to try is something like portbaud = " COM1:9600 ... " , and use the statement 'open com portbaud as #1'. I get the feeling that may not work. The only other option is to set up a bunch of subroutines, and call the one that is selected. I guess I need some more hints.

Thanks
phishguy
Posts: 1200
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Postby phishguy » Dec 10, 2008 22:55

This should work.


Code: Select all

Dim As String portbaud
portbaud = "COM1:9600"

Open Com portbaud & ",n,8,1,cs0,ds0,cd0,rs" As #1
 



Or this

Code: Select all

Dim as string port,baud,settings
port = "COM1:"
Baud = "9600"
settings = ",n,8,1,cs0,cd0,ds0,rs"
open com port & baud & settings as #1


[/code]
dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Postby dasyar » Dec 10, 2008 23:14

I just tried your previous example code for the F1 key trap. After the input, you type in your digits, and press CR, it drops down one line and shows a couple spaces, and a ' ; '. Where is this ' ; ' coming from? Also on the Hyperterminal side it also shows a couple of spaces, and a ' ; '. I was expecting to see some kind of chars, even if Hyperterminal is expecting ASCII, I thought the byte values would show up as some kind of chicken scratchings on the screen. So far so good.

Thanks
phishguy
Posts: 1200
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Postby phishguy » Dec 11, 2008 0:00

Oops! I had a slight bug where if you only tried to send one number it wouldn't work. That's what I get for not testing. Here's the corrected code.

Code: Select all


' FBterm.bas
' FBterm.exe
' December 6, 2008
' Ver .04

' Grabs the exiting values of the command prompt window
'1 = cursor on, and 0 = cursor off
Locate ,,1                         'This locates the cursor at 0,0 position
Dim As Integer oldcolor = Color
Dim As Integer oldwidth = Width
Cls

'New code goes here

Dim As Integer pos1,pos2
Dim As String Key,buffer         'Define for a string value
Dim numtosend As String
Dim numtosend2 As Ubyte


'Open the Com port
Open Com "COM1:9600,n,8,1,cs0,ds0,cd0,rs" As #1


'Check if port was opened
If Err <> 0 Then
    Print "Error opening COM1:";
    Sleep
    End
End If

'Titles
Print "FBterm, A terminal program"
Print "Press the ESC key to QUIT"
Print "Press F1 to send a number"


Do
    Key = Inkey$
   
    'Check for the esc key to quit the program
    If Key = Chr$(27) Then         'Is chr$(27) the esc Key, yes it is.
        Exit Do
    End If
   
    'Check for F1
    If Key = Chr$(255)+Chr$(59) Then         'the F1 key
       
        Input "Enter numbers to send (0 - 255) space delimited";numtosend    'Why isn't this printing to the console?
       
        pos1 = Instr(numtosend," ")
        If pos1 = 0 And Len(numtosend) > 0 Then
            Print #1, Chr$(Val(numtosend));
            Print Chr$(Val(numtosend));
        Else
            numtosend2 = Val(Left$(numtosend,pos1))
            Print #1,  Chr$(numtosend2);
            Print Chr$(numtosend2);
            Do While pos1 > 0
               
                pos2 = pos1
                pos1 = Instr(pos1+1 ,numtosend," ")
                numtosend2 = Val(Mid$(numtosend,pos2+1 ,pos1-pos2))
               
                Print #1,Chr$(numtosend2);
                Print Chr$(numtosend2);
            Loop
           
        End If
    Else
       
        'Send the keypress
        If Key <> "" Then
            If Key = Chr$(13) Then      'Check if CR has been pressed         
                Print #1, Chr$(13);       'Send the CR and do a CR
                Print Chr$(10);          'Send a linefeed
            End If
            Print #1,Key;               'This sends a char, print with ; = no new line
            Print Key;                  'Echo the keypress
        End If
    End If
    'Receive and display the char
    While Loc(1) > 0
        buffer = Input$(Loc(1),#1)  'This receives a char
        Print buffer;
    Wend
   
    Sleep 1,0
Loop


' Restore the command prompt window to the old settings
Width oldwidth And &hFFFF, oldwidth Shr 16
Color oldcolor And &hFFFF, oldcolor Shr 16
View Print 1 To oldwidth Shr 16
'  cls





If you press F1 and then type 72 69 76 76 79<cr> you should see and send HELLO.


BTW - Just curious as to why you're using DOS? If your interested, I made a miniterm program for windows that's in the Projects subforum. It won't work for DOS of course. But, it may give you some ideas.
phishguy
Posts: 1200
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Postby phishguy » Dec 11, 2008 2:18

I added a little bit of port and baud rate selection for an example.

Code: Select all

' FBterm.bas
' FBterm.exe
' December 6, 2008
' Ver .04

' Grabs the exiting values of the command prompt window
'1 = cursor on, and 0 = cursor off
Locate ,,1                         'This locates the cursor at 0,0 position
Dim As Integer oldcolor = Color
Dim As Integer oldwidth = Width
Cls

'New code goes here

Dim As Integer pos1,pos2,p,b
Dim As String Key,buffer         'Define for a string value
Dim numtosend As String
Dim numtosend2 As Ubyte
Dim As String port(1 To 4) = {"COM1:","COM2:","COM3:","COM4:"}
Dim As String baud(1 To 6) = {"110","300","1200","2400","4800","9600"}
Locate 1,1
p = 1
b = 1
begin:
View Print 1 To Hiword(Width)
Print "F1 - ";baud(b),"F2 - ";port(p),"F3 - Connect",,"F10 - Quit"
Do
    key = Inkey$
    If key = Chr$(255) & Chr$(68) Then 'F10
        End
    End If
   
    If key = Chr$(255) & Chr$(59) Then 'F1
        b +=1
        If b = 7 Then b = 1
        Locate 1,6
        Print "      ";
        Locate 1,6
        Print baud(b);
    End If
    If key = Chr$(255) & Chr$(60) Then 'F2
        P +=1
        If p = 5 Then p = 1
        Locate 1,20
        Print port(p);
    End If
    If key = Chr$(255) & Chr$(61) Then 'F3
        Exit Do
    End If
   
    Sleep 1
Loop
Locate 1,1
Print "Baud";
Locate 1,15
Print "Port";
Locate 1,34
Print "Disconnect";


View Print 2 To Hiword(Width)

'Open the Com port
Open Com port(p) & baud(b) &",n,8,1,cs0,ds0,cd0,rs" As #1


'Check if port was opened
If Err <> 0 Then
    Print "Error opening COM1:";
    Sleep
    End
End If

'Titles
'Print "FBterm, A terminal program"
'Print "Press the ESC key to QUIT"
Print "Press F4 to send a number"

Do
    Key = Inkey$
   
    'Check for the esc key to quit the program
    If Key = Chr$(255) & Chr$(68) Then 'F10 to exit
        Exit Do
    End If
    If key = Chr$(255) & Chr$(61) Then 'F3 to disconnect
        Close #1
        Goto begin
    End If
   
   
    'Check for F4
    If Key = Chr$(255) & Chr$(62) Then         'the F4 key
       
        Input "Enter numbers to send (0 - 255) space delimited";numtosend    'Why isn't this printing to the console?
       
        pos1 = Instr(numtosend," ")
        If pos1 = 0 And Len(numtosend) > 0 Then
            Print #1, Chr$(Val(numtosend));
            Print Chr$(Val(numtosend));
        Else
            numtosend2 = Val(Left$(numtosend,pos1))
            Print #1,  Chr$(numtosend2);
            Print Chr$(numtosend2);
            Do While pos1 > 0
               
                pos2 = pos1
                pos1 = Instr(pos1+1 ,numtosend," ")
                numtosend2 = Val(Mid$(numtosend,pos2+1 ,pos1-pos2))
               
                Print #1,Chr$(numtosend2);
                Print Chr$(numtosend2);
            Loop
           
        End If
    Else
       
        'Send the keypress
        If Key <> "" Then
            If Key = Chr$(13) Then      'Check if CR has been pressed         
                Print #1, Chr$(13);       'Send the CR and do a CR
                Print Chr$(10);          'Send a linefeed
            End If
            Print #1,Key;               'This sends a char, print with ; = no new line
            Print Key;                  'Echo the keypress
        End If
    End If
    'Receive and display the char
    While Loc(1) > 0
        buffer = Input$(Loc(1),#1)  'This receives a char
        Print buffer;
    Wend
   
    Sleep 1,0
Loop


' Restore the command prompt window to the old settings
Width oldwidth And &hFFFF, oldwidth Shr 16
Color oldcolor And &hFFFF, oldcolor Shr 16
View Print 1 To oldwidth Shr 16
'  cls

dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Postby dasyar » Dec 11, 2008 11:52

"BTW - Just curious as to why you're using DOS? If your interested, I made a miniterm program for windows that's in the Projects subforum. It won't work for DOS of course. But, it may give you some ideas."

Short answer - I wanted to try out FB, and what a better way to do it with, is a small project, like a terminal program. Why DOS?, I wanted to try out the DOS-C binaries, to find out if the FB programs would have any problem running on DOS-C. So far my little program has run without any problems using DOS-C. I am going to hold off looking at your miniterm program for a bit, I want to see what my brain can conjure up.

I think I will wait a little bit before I discuss the "Long answer".

All ideas are welcome, it shortens the learning curve, but does not replace the idea of learning.

Thanks
dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Postby dasyar » Dec 11, 2008 23:00

Some preliminary tests with the DOS-C setup. I was getting some triple fault activity when I hit some of the ' F ' keys at the wrong time. For instance when I was in the terminal mode I would hit the F1 key, then the F10 key, which at one time, did a triple fault. I have had it triple fault three times, but I cannot remember the other key sequences that caused it. I had high hopes for DOS-C.

On the EDR-DOS setup I could not reproduce the triple fault, so far. I also found out that the RS-232 ports on the computers that I am using are only good up to 115200 baud. So if I ever figure out how implement a USB-RS232 dongle on a DOS machine, I will have to settle for 115200 BAUD. The dongle that I am using did not come with any DOS drivers, in fact I have not seen any devices that would come with DOS drivers, these days.

I guess now I am thinking about improvements to the program. Is there an easy way of adding color to the program?, without going into graphics mode. I am thinking about the ' F ' key selection bar, maybe getting some color.

Thanks

Return to “Hardware Interfaces / Communication”

Who is online

Users browsing this forum: No registered users and 2 guests