TIP: Click on subject to list as thread! ANSI
echo: rberrypi
to: ALL
from: =?UTF-8?Q?BJ=C3=B6RN_LUND
date: 2021-01-02 13:24:00
subject: Re: How to realize a `ser

Den 2021-01-02 kl. 11:14, skrev RobertoA:
> Il 01/01/2021 15:30, Björn Lundin ha scritto:
>> Den 2020-12-31 kl. 21:38, skrev RobertoA:
>
> I understand the substance of the written code, what I can't do at the
> moment is how to use it on Raspberry

save it to a file and run it with
tclsh filename


> What documents / tutorials to follow to understand how to use the sent
> code?

There is none - since I wrote it for me - unless you mean tcl as a
language and in particular file events





I just saw it wasn't complete - this should work (though not tested
since I got no serial device)

save it to a file, install tcl if not alredy there
(sudo apt install tcl)

and set - below
set my_com "/dev/ttyWhatever"
set port 6789
to whatever values you need.

Then - when something connects to the tcp/ip port of your choise
it is forwared byte by byte to the serial device.
and vice versa. whenever the soceket ot serail port becomes readable, it
invokes proc pass_through that reads a byte and forwards it unless its a
carriage return. those are swallowed - change the if-statement 'if
{[string equal "\r" $tmp]}' - in pass_through if it does not apply to you.


run it with tclsh whatever/name/you/gave/it

#####
#Associate a tcp/ip port 'port' with a tty 'my_com'
# run with tclsh scriptname
#####
set done 0
set my_sock 0
set my_com "/dev/ttyWhatever"
set port 6789
set ss 0
set debug 1
set config_line {}
###########################

proc gettime {} {
   return [clock format [clock seconds] -format "%Y-%b-%d %H:%M:%S"]
}

###########################

proc dbg {what} {
   if {$::debug} {
      puts  $what
      # puts $::error_file_pointer $what
      # catch {flush $::error_file_pointer}
   }
}

###########################

proc pass_through {from to} {
   set tmp [read $from 1] ;# read 1 byte at a time
   if {[eof $from]} {     ;# client gone or finished
      set ::done 1        ;# signal to main loop to let go
   } else {
   #do things
     if {[string equal "\r" $tmp]} {
       catch {puts "\n[gettime] -> CR received, terminating chars above" 1}
     } else {
       catch {puts -nonewline $tmp}
       catch {puts -nonewline $to $tmp} ;# put it out
     }
   }
}
###########################

proc accept {sock addr port} {
   dbg "[gettime] -> start accept from $addr"

   set ::my_sock $sock
   set ::my_com [open $::tty {RDWR}]

   # Read channels w/o buffering and in binary mode
   fconfigure $::my_sock -buffering none -translation binary
   fconfigure $::my_com -mode $::mode -buffering none -translation binary

   # Setup handler for communication on the channels
   fileevent $::my_sock readable [list pass_through $::my_sock $::my_com]
   fileevent $::my_com  readable [list pass_through $::my_com $::my_sock]

   # make sure noone else connects while a session in running
   catch {close $::ss}
   dbg "[gettime] -> stop accept"
}

###########################

# loop to reopen the listening socket when connection down
while {1} {
   dbg "[gettime] -> Start listening"
   set ss [socket -server accept $port]
   vwait done ; # wait until the connection is closed
   # clean up after session
   dbg "[gettime] -> Close this session"
   catch {close $::my_com}
   catch {close $::my_sock}
}





--
Björn

--- SoupGate-Win32 v1.05
* Origin: Agency HUB, Dunedin - New Zealand | FidoUsenet Gateway (3:770/3)

SOURCE: echomail via QWK@docsplace.org

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.