; Serial I/O snippets for VM2 To init Make serial2 SerialPort(115200, 2, 1) End To main Print "demonstrating term. Press Ctrl/C to exit" term End ; talkthrough between PC terminal on serial 1 and ; device connected to serial 2 ; (useful for checking if a modem is working, etc.) To term Forever [ If serial2.Queue serial.Put(serial2.Get) If serial.Queue serial2.Put(serial.Get) ] End ; term ; get a line of text from user on terminal, terminated by Enter (CR) ; returns in a text buffer (supplied) ; characters are echoed ; simple version To readline(buf) Local c := 0 buf.Empty While c <> 13 [ c := serial.Get serial.Put(c) ; Echo input char If c = 13 serial.Put(10) ; Also echo LF after CR Else buf.Put(c) ; all other chars to buffer ] End ; readline ; more sophisticated readline ; buf is a string variable, or could also be a text buffer variable ; this version filters out control characters To readline_printable(buf) Local c := 0 buf.Empty While c <> 13 [ c := serial.Get If c = 13 [ serial.Put(13) ; Echo CR serial.Put(10) ; Also echo LF after CR ] Else If c >= ' ' ; non-control chars only [ serial.Put(c) ; Echo input char buf.Put(c) ; ...and add to buffer ] ] End ; readline ; example of sending a command to a device like a modem ; on serial 2 and ; collecting 1 line of response ; parameters are ; cmd: string or text buffer ; response: text buffer To cmd_response(cmd, response) Local c := 0 response.Empty Print To serial2, cmd, Chr 13 ; not CR as that sends CRLF While c <> 10 c := serial2.Get ; get echo of command c := 0 While c <> 10 [ c := serial2.Get If (c <> 13) And (c <> 10) response.Put(c) ] End