Skip to content

Instantly share code, notes, and snippets.

@tiagocoutinho
Last active December 19, 2023 07:57
Show Gist options
  • Save tiagocoutinho/de3f8da1bc11943b5ae41fd84aa83ee8 to your computer and use it in GitHub Desktop.
Save tiagocoutinho/de3f8da1bc11943b5ae41fd84aa83ee8 to your computer and use it in GitHub Desktop.

Revisions

  1. tiagocoutinho revised this gist May 13, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ python simulator.py
    #### Run on terminal 3:

    ```
    python client "*IDN?"
    python client.py "*IDN?"
    ```

    ### References
  2. tiagocoutinho revised this gist May 13, 2020. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,26 @@
    # Virtual serial line
    ## Virtual serial line

    Virtual serial line on linux with socat, python server & client

    #### Run on terminal 1:
    ```terminal
    socat -d -d pty,raw,echo=0,link=/tmp/cryocon_simulator pty,raw,echo=0,link=/tmp/cryocon
    ```

    #### Run on terminal 2:

    ```
    python simulator.py
    ```

    #### Run on terminal 3:

    ```
    python client "*IDN?"
    ```

    ### References

    [stack overflow reference](https://stackoverflow.com/questions/52187/virtual-serial-port-for-linux)

    Alternatives:
  3. tiagocoutinho revised this gist May 13, 2020. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    # Virtual serial line

    Virtual serial line on linux with socat, python server & client

    [stack overflow reference](https://stackoverflow.com/questions/52187/virtual-serial-port-for-linux)

    Alternatives:
    * [example](https://github.com/cymait/virtual-serial-port-example)
    * [nullmodem](https://github.com/pitti98/nullmodem)
    * [tty0tty](http://sourceforge.net/projects/tty0tty/)
  4. tiagocoutinho renamed this gist May 13, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. tiagocoutinho created this gist May 13, 2020.
    2 changes: 2 additions & 0 deletions bridge.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    # creates the PTYs
    socat -d -d pty,raw,echo=0,link=/tmp/cryocon_simulator pty,raw,echo=0,link=/tmp/cryocon
    16 changes: 16 additions & 0 deletions client.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    import sys
    import serial
    DEFAULT_ADDR = '/tmp/cryocon'
    DEFAULT_CMD = '*IDN?'
    args = len(sys.argv) - 1
    if args == 0:
    addr, cmd = DEFAULT_ADDR, DEFAULT_CMD
    elif args == 1:
    addr, cmd = DEFAULT_ADDR, sys.argv[1]
    else:
    addr, cmd = sys.argv[1:3]

    cmd += '\n'
    s = serial.serial_for_url(addr)
    s.write(cmd.encode())
    print(s.readline())
    21 changes: 21 additions & 0 deletions simulator.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    import sys
    import logging

    import serial

    DEFAULT_ADDR = '/tmp/cryocon_simulator'

    logging.basicConfig(level=logging.INFO)

    addr = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_ADDR

    conn = serial.serial_for_url(addr)
    logging.info(f'Ready to receive requests on {addr}')
    while True:
    request = conn.readline()
    logging.info('REQ: %r', request)
    request = request.strip().decode().lower()
    reply = 'Cryo-con,24C,305682,1.05A\n' if request == '*idn?' else 'NACK\n'
    reply = reply.encode()
    logging.info('REP: %r', reply)
    conn.write(reply)