7 people like it.

Serial Port Reader

Code to drive a serial port. Trivial application to read a paper tape to a file from a GNT4604 paper tape reader attached to serial port COM3:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
#light

// Program to read a paper tape to a file from a GNT4604 paper tape reader
// attached to serial port COM3:

module ReadTape
    
    open System.IO

    exception NoTape  // used to force exit when no further tape to read

    // Open serial port 
    let OpenPort () =
        let port = new System.IO.Ports.SerialPort ("COM3", 4800, Ports.Parity.None, 8, Ports.StopBits.One)
        port.Open ()
        port.DtrEnable   <- true
        port.RtsEnable   <- true
        port.ReadTimeout <- 1000
        port 

    let port = OpenPort ()                   

    let ReadRaw f =
        let buffer: byte[] = Array.zeroCreate (250*60*30) // 15 minutes of paper tape reading 
        let rec GetBytes i = 
            try 
                    buffer.[i] <- byte (port.ReadByte ()) 
                    GetBytes (i+1)
            with 
            _   ->  i // finish on timeout    
        printfn "Reading tape to %s" f
        printf "Load tape and type hit any key..."
        System.Console.ReadLine () |> ignore
        let count = (GetBytes 30)+30 // force 3 inches of leader and trailer)
        printfn "%d bytes input" (count-60)
        let rec TrimLeft i =
            if   i = count
            then printfn "Empty tape"
                 0
            elif buffer.[i] = 0uy 
            then TrimLeft (i+1) 
            else i
        let rec TrimRight i =
            if   i = 0
            then printfn "Empty Tape"
                 count
            elif buffer.[i] = 0uy 
            then TrimRight (i-1) 
            else i
        let start  = max 30 ((TrimLeft 0)-30)
        let finish = min (count-30) ((TrimRight count)+30)       
        let trimmed = buffer.[start..finish]
        printfn "%d bytes output" trimmed.Length
        try File.WriteAllBytes (f, trimmed) with
        e   -> System.Console.WriteLine f


    let  cmdLine = System.Environment.GetCommandLineArgs()
    if   cmdLine.Length < 2
    then System.Console.WriteLine "Error: READTAPE file"
    else ReadRaw cmdLine.[1]
module ReadTape
namespace System
namespace System.IO
exception NoTape

Full name: ReadTape.NoTape
val OpenPort : unit -> Ports.SerialPort

Full name: ReadTape.OpenPort
val port : Ports.SerialPort
namespace System.IO.Ports
Multiple items
type SerialPort =
  inherit Component
  new : unit -> SerialPort + 6 overloads
  member BaseStream : Stream
  member BaudRate : int with get, set
  member BreakState : bool with get, set
  member BytesToRead : int
  member BytesToWrite : int
  member CDHolding : bool
  member Close : unit -> unit
  member CtsHolding : bool
  member DataBits : int with get, set
  ...

Full name: System.IO.Ports.SerialPort

--------------------
Ports.SerialPort() : unit
Ports.SerialPort(container: System.ComponentModel.IContainer) : unit
Ports.SerialPort(portName: string) : unit
Ports.SerialPort(portName: string, baudRate: int) : unit
Ports.SerialPort(portName: string, baudRate: int, parity: Ports.Parity) : unit
Ports.SerialPort(portName: string, baudRate: int, parity: Ports.Parity, dataBits: int) : unit
Ports.SerialPort(portName: string, baudRate: int, parity: Ports.Parity, dataBits: int, stopBits: Ports.StopBits) : unit
type Parity =
  | None = 0
  | Odd = 1
  | Even = 2
  | Mark = 3
  | Space = 4

Full name: System.IO.Ports.Parity
field Ports.Parity.None = 0
type StopBits =
  | None = 0
  | One = 1
  | Two = 2
  | OnePointFive = 3

Full name: System.IO.Ports.StopBits
field Ports.StopBits.One = 1
Ports.SerialPort.Open() : unit
property Ports.SerialPort.DtrEnable: bool
property Ports.SerialPort.RtsEnable: bool
property Ports.SerialPort.ReadTimeout: int
val port : Ports.SerialPort

Full name: ReadTape.port
val ReadRaw : f:string -> unit

Full name: ReadTape.ReadRaw
val f : string
val buffer : byte []
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = System.Byte

Full name: Microsoft.FSharp.Core.byte
module Array

from Microsoft.FSharp.Collections
val zeroCreate : count:int -> 'T []

Full name: Microsoft.FSharp.Collections.Array.zeroCreate
val GetBytes : (int -> int)
val i : int
Ports.SerialPort.ReadByte() : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
System.Console.ReadLine() : string
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val count : int
val TrimLeft : (int -> int)
val TrimRight : (int -> int)
val start : int
val max : e1:'T -> e2:'T -> 'T (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.max
val finish : int
val min : e1:'T -> e2:'T -> 'T (requires comparison)

Full name: Microsoft.FSharp.Core.Operators.min
val trimmed : byte []
property System.Array.Length: int
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.WriteAllBytes(path: string, bytes: byte []) : unit
val e : exn
System.Console.WriteLine() : unit
   (+0 other overloads)
System.Console.WriteLine(value: string) : unit
   (+0 other overloads)
System.Console.WriteLine(value: obj) : unit
   (+0 other overloads)
System.Console.WriteLine(value: uint64) : unit
   (+0 other overloads)
System.Console.WriteLine(value: int64) : unit
   (+0 other overloads)
System.Console.WriteLine(value: uint32) : unit
   (+0 other overloads)
System.Console.WriteLine(value: int) : unit
   (+0 other overloads)
System.Console.WriteLine(value: float32) : unit
   (+0 other overloads)
System.Console.WriteLine(value: float) : unit
   (+0 other overloads)
System.Console.WriteLine(value: decimal) : unit
   (+0 other overloads)
val cmdLine : string []

Full name: ReadTape.cmdLine
type Environment =
  static member CommandLine : string
  static member CurrentDirectory : string with get, set
  static member Exit : exitCode:int -> unit
  static member ExitCode : int with get, set
  static member ExpandEnvironmentVariables : name:string -> string
  static member FailFast : message:string -> unit + 1 overload
  static member GetCommandLineArgs : unit -> string[]
  static member GetEnvironmentVariable : variable:string -> string + 1 overload
  static member GetEnvironmentVariables : unit -> IDictionary + 1 overload
  static member GetFolderPath : folder:SpecialFolder -> string + 1 overload
  ...
  nested type SpecialFolder
  nested type SpecialFolderOption

Full name: System.Environment
System.Environment.GetCommandLineArgs() : string []
Raw view New version

More information

Link:http://fssnip.net/9s
Posted:7 years ago
Author:Andrew Herbert
Tags: ports , serial , paper tape