ClearCore Library
Loading...
Searching...
No Matches
ReadSerialInput.cpp

Return to SDK Examples for Microchip Studio

1/*
2 * Title: ReadSerialInput
3 *
4 * Objective:
5 * This example demonstrates how to read and display incoming data from a
6 * serial port.
7 *
8 * Description:
9 * This example will read one byte per second from the serial input buffer.
10 * During operation, if a byte has been received, it will be printed to the
11 * USB serial port as a character.
12 *
13 * Requirements:
14 * ** A serial input source connected to COM-0.
15 *
16 * Links:
17 * ** ClearCore Documentation: https://teknic-inc.github.io/ClearCore-library/
18 * ** ClearCore Manual: https://www.teknic.com/files/downloads/clearcore_user_manual.pdf
19 *
20 *
21 * Copyright (c) 2020 Teknic Inc. This work is free to use, copy and distribute under the terms of
22 * the standard MIT permissive software license which can be found at https://opensource.org/licenses/MIT
23 */
24
25#include "ClearCore.h"
26
27// Select the baud rate to match the target device.
28#define baudRateSerialPort 115200
29#define baudRateInputPort 115200
30
31// When using COM ports, is the device TTL or RS232?
32#define isTtlInputPort false
33
34// Specify which serial interface to use as output.
35#define SerialPort ConnectorUsb
36
37// Specify which serial interface to use as input.
38#define InputPort ConnectorCOM0
39
40// Container for the byte to be read-in
41int16_t input;
42
43int main() {
44 // Set up serial communication to print out the serial input.
45 SerialPort.Mode(Connector::USB_CDC);
46 SerialPort.Speed(baudRateSerialPort);
47 SerialPort.PortOpen();
48 while (!SerialPort) {
49 continue;
50 }
51
52 // Set up serial communication to send serial input over.
53 if (isTtlInputPort) {
54 InputPort.Mode(Connector::TTL);
55 }
56 else {
57 InputPort.Mode(Connector::RS232);
58 }
59 InputPort.Speed(baudRateInputPort);
60 InputPort.PortOpen();
61 while (!InputPort) {
62 continue;
63 }
64
65 while (true) {
66 // Read the input.
67 input = InputPort.CharGet();
68
69 // If there was a valid byte read-in, print it.
70 if (input != -1) {
71 // Display the input character received.
72 SerialPort.Send("Received: ");
73 SerialPort.SendLine((char)input);
74 }
75 else {
76 SerialPort.SendLine("No data received...");
77 }
78
79 // Wait a second then repeat...
80 Delay_ms(1000);
81 }
82}
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition SysTiming.h:287