How to read characters (string) in PSoC from Serial

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi:

   

For a new project I need to send and read data, ASCII char numbers, from the serial port to the PSoC. I've been trying to send and receive strings from Serial Port to the PSoC 4,  sending strings is easy with UART_PutString(); , but I cant read them yet. I was looking in the UART Datasheet to find help in this matter,  but what I can understand so far, is that you can only receive or get one char or byte at a time. So I was wondering if there is a function such as gets() or maybe ReadLine. And if there is no function, how do you think that it can be done?

   

Thank you for your help

   

Martin

13 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Use an Rx interrupt, or poll, and fill an array byte by byte as they

   

are received. You would use a pointer into the array and inc it each

   

time you write a byte. There are example projects in Creator, on start

   

page "Find Example Projects" link.

   

 

                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

S No

Date

Project Title

1

01-May-2013

Simple Blinking LED

2

02-May-2013

CapSense Slider Example

3

03-May-2013

PSoC 4 Pioneer Kit Annotation Library

4

06-May-2013

USB-UART utility

5

07-May-2013

USB-I2C Utility

6

08-May-2013

Danger Shield with Light Sensor Control

7

09-May-2013

Danger Shield with 7-segment display

8

10-May-2013

Danger Shield Buzzer of Doom

9

13-May-2013

Graphics LCD Display

10

14-May-2013

LCD - Rotating Cube Demo

11

15-May-2013

GLCD Paddle Game

12

16-May-2013

Pioneer Board Oscilloscope

13

17-May-2013

CapSense Button & LED Control with Bridge Control Panel

14

20-May-2013

What was the value? - LED Memory Example

15

21-May-2013

CapSense Proximity Detection

16

22-May-2013

Proximity Theremin

17

23-May-2013

2-Channel OScope with GraphicsLCD

18

24-May-2013

'Catch the Shells' Game with ColorLCD Shield

19

28-May-2013

LED Memory! Part Deux. Just Deux it!

20

29-May-2013

Starter Designs

21

30-May-2013

Fractional Frequency Synthesizer

22

31-May-2013

“Kill The Ghost” Game

23

03-Jun-2013

tinyprintf Example

24

04-Jun-2013

XBee Router Loopback Example

25

05-Jun-2013

XBee Direct IO

26

06-Jun-2013

XBee API RxTx Example

27

07-Jun-2013

XBee RGB CapSense Control

28

10-Jun-2013

“WiFi? Why Not!” Arduino WiFi Shield Example

29

11-Jun-2013

Bluetooth Home Automation System

30

12-Jun-2013

Bluetooth with GLCD

31

13-Jun-2013

Ethernet Shield

32

14-Jun-2013

More Relay!

33

17-Jun-2013

Sweet Music Everywhere! MIDI Shield

34

18-Jun-2013

Resistive Touch Example

35

19-Jun-2013

UART, I2C, and SPI Joystick Example

36

20-Jun-2013

What? I can use the PSoC 5LP too?

37

21-Jun-2013

PSoC With Friends! Word Scramble Game

38

24-Jun-2013

Ultrasonic Distance Measurement

39

25-Jun-2013

Two PWMs for the price of one

40

26-Jun-2013

Pioneer PnP Sensors

41

27-Jun-2013

Thermistor Example

42

28-Jun-2013

Hangman Game

43

01-Jul-2013

Rise of the Machines (Rolling Robot)

44

02-Jul-2013

Run Away Run Away! (Proximity Robot)

45

03-Jul-2013

Stepper Motor Example

46

05-Jul-2013

“Paint The Night” Accelerometer Example

47

08-Jul-2013

Obstacle Avoider Robot

48

09-Jul-2013

Walk The Line! Robot Example

49

11-Jul-2013

Android Device Controlled Robot

50

12-Jul-2013

SD Card Example

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

S No

Date

Project Title

51

15-Jul-2013

Music Player

52

16-Jul-2013

Strike A Pose! Digital Camera

53

17-Jul-2013

PSoC Eye

54

18-Jul-2013

Custom Component 4x4 Keypad

55

19-Jul-2013

Parallel to Serial UDB Component

56

22-Jul-2013

Accelerometer Level

57

23-Jul-2013

Digital Audio From A Single Pin

58

24-Jul-2013

An Introduction to Processing

59

25-Jul-2013

But Wait There’s More! Single PWM with 3 Outputs

60

26-Jul-2013

PSoC 4 Mini-Billboard

61

29-Jul-2013

Nokia 5110 LCD Interface

62

30-Jul-2013

Ready…Set…GO! PSoC Stopwatch

63

31-Jul-2013

Some Like it Hot! PSoC Thermostat

64

01-Aug-2013

Workspace Organization using PSoC Theromostat

65

02-Aug-2013

New to PSoC or the Pioneer Kit? START HERE!

66

05-Aug-2013

LED Blinky Revisited

67

06-Aug-2013

PSoC 4 Getting Started Lab 1 (LED Blinky)

68

07-Aug-2013

PSoC 4 Getting Started Lab 2 (PWM LED)

69

08-Aug-2013

PSoC 4 Getting Started Lab 3 (CapSense UART)

70

09-Aug-2013

PSoC 4 Getting Started Lab 4 (ADC)

71

12-Aug-2013

Get Your Motor Runnin’!

72

13-Aug-2013

TFT Touchscreen Shield

73

14-Aug-2013

TFT Touchscreen Tic-Tac-Toe Game

74

15-Aug-2013

PSoC Creator Training 110

75

16-Aug-2013

PSoC Creator Training 111                                          

76

19-Aug-2013

PSoC Creator Training 112

77

20-Aug-2013

PSoC Creator Training 113

78

21-Aug-2013

PSoC Creator Training 210

79

22-Aug-2013

PSoC Creator Training 211

80

23-Aug-2013

PSoC Creator Training 212/213/214

81

26-Aug-2013

NFC/RFID Shield

82

27-Aug-2013

RFID Reader and Writer Project #2

83

28-Aug-2013

Raspberry Pi Integration

84

29-Aug-2013

State Machines in PSoC 4

85

30-Aug-2013

UART Bootloader in PSoC 4

86

03-Sep-2013

GPS Algorithm

87

04-Sep-2013

Gas Sensor Example

88

05-Sep-2013

Simulation of Gas Sensor Example

89

06-Sep-2013

GPS Example

90

09-Sep-2013

Comparator Example

91

10-Sep-2013

Infrared Remote Control

92

11-Sep-2013

Serial Communications Examples

93

12-Sep-2013

4 Channel Multiplexed Comparator

94

13-Sep-2013

Opamp Dynamic Gain

95

16-Sep-2013

Hibernate and Stop Power Modes

96

17-Sep-2013

P4 Light Sensor Project Design

97

18-Sep-2013

Rice Cooker

98

19-Sep-2013

Thermal Printer

99

20-Sep-2013

I2C Pass Though 'Smart Wire'

100

23-Sep-2013

PSoC 4 Time Square Billboard

   

 

   

Regards, Dana.

0 Likes
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted
        martin, Take a look on this thread: http://www.cypress.com/forum/psoc-4-architecture/psoc4-uart-receiving-string There is an example of receiving UART strings and simple commands to control RGB LED on PSOC4 Pioneer Board: UART4_RGB_01-000.cywrk.Archive01.zip   
0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi again:

   

Sorry for not answer soon enough, I am trying to do this program to get four characters, and then save the values in an array. In order to do that I'm using an Rx Interrupt on byte received (just like @danaaknight said). I developed the following code and then program my PSoC CYCKIT-049-42xx, then using puTTY, I tried to send this characters from my PC to the PSoC, but I am not getting any answer from the PSoC to my PC.

   

I would be very grateful if you could give some help or advice about what I am doing wrong.

   

Martin

   

P.S. I am attaching the project to this comment.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Are you trying to use the UART bridge that connects the USB interface to a (virtual) Com-port on your PC? Then you need to use the SCB - based component and connect Tx and Rx to Port4_0 and _1

   

 

   

When using a direct connection from your Tx and Rx pins P0_5 and _4 you will need a level shifter to convert the 5V signal to a +-12v RS232 level and back.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I am using the USB-Serial Bridge (CY7C65211) that is included with the prototyping kit, and just wiring P0.4 and P0.5 to the Serial Bridge. But I've never heard that it's necessary to use the SCB component, in other projects I used the UART v2.5 component, and it worked just fine, What is the difference? . However, I am going to try the SCB component.

   

Thanks

   

Martin 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Maybe I misunderstood you, you are talking about " just wiring P0.4 and P0.5 to the Serial Bridge" which pins do you use?.

   

Did you confirm that the baud-rate for USB-chip is same as for your UART? Did you check using Cypress USB Configuration Utility?

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I am connecting P0.4 and P0.5 of the PSoC4200 to SCB.4  and SCB.0  of the Serial Bridge (CY7C65211) respectively, I am attaching a diagram of my connections.

   

By the way, yes.  I'm using puTTY as virtual serial monitor, with 115200 Bauds. And I have set the baud rate from the USB Configuration Utility (I will attach an image of this too).

   

Martin

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Your variable "send" is defined too short, so sprintf() will overwrire data.

   

Declaring a local variable as "volatile" is of no use, it cannot get changed from the outside, but your variable "i" can.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

´Concerning the "wiring": Sorry I was darn wrong with that. For the CY8CKIT-049-42xx prototyping kit P4_0 and P4_1 are correct.

   

No external wire needed, both pins are already connected to the USB-bridge. Just built and tested a project - attached.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
cadi_1014291
Level 6
Level 6
25 likes received 10 likes received 10 likes given

I have a project that use "commands" to turn on the corresponding led on the RGB Led of the Pioneer Kit, i haven´t seen yours, but maybe it can help you, find it attached.

   

 

   

PD: en ese proyecto estaba tratando de entender las funciones de string.h asi que trae varias cosas que ni al caso xD

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hey:

   

First of all, thank you for all your help. I really tried to do this program with the SCB UART, I don't know why it didn't work with this component; nevertheless, with the UART v2.5 worked perfectly, and now I can read strings inputs with a length of 4. The thing is that now, when I try to convert the string to an integer using a C function, sometimes it works, but sometimes the conversion is totally wrong. I proved this function in the compiler in my PC and I am pretty sure that it worked.

   

What could be happening here?

   

Martin

   

P.S. I am attaching the project.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are overdoing too much.

   

First the UART send:

   

Do not use delay functions, do not use UART_ClearTxBuffer() except in case of an error.

   

Instead use UART_ReadTxStatus() and check for completed transfer- Since UART_PutString is a blocking function not even that is needed.

   

Next the UART receive:

   

You do not need to use an interrupt, you may increase the Rx buffer in the UART component and look wit UART_GetRxBufferSize() for data received. Only difficulty is here to find out when a transmission is finished. Easiest is when a delimiter as \r or \n is received.

   

When using the interrupt the first thing in the handler is to clear the interrupt source using UART_ReadRxStatus().

   

Look into the UART's datasheet for explanations of the functions and the status registers.

   

What if you receive more than 5 bytes? Then you are destroying data!!!

   

You may, but you do not need to write your own conversion routines. There is the scanf() library routine which will do, have a look here.

   

 

   

Happy coding

   

Bob

0 Likes
Anonymous
Not applicable

You can also get the standard C puts, gets, scanf, printf stuff working with some pain

https://iotexpert.com/2017/05/10/implementing-psoc-printf/
Note: be sure to increase the heap size to at least 400 under Design Wide Resources, System, Configuration, Heap Size (bytes)

_read generally stolen from here. Assumes that an incoming 0 is nothing recieved (e.g. can't support binary data).

https://mcuoneclipse.com/2014/07/11/printf-and-scanf-with-gnu-arm-libraries/

#include "project.h"

#include "stdio.h"

//Replace the gcc default connectors for stdin and stdout

int _write(int file, char *ptr, int len) {

    int i;

    file = file;

    for (i = 0; i < len; i++) {

        UART_UartPutChar(*ptr++);

        }

    return len;

    }

int _read (int fd, const void *buf, size_t count) {

  size_t CharCnt = 0x00;

  (void)fd;                            /* Parameter is not used, suppress unused argument warning */

  for (;count > 0x00; --count) {

    /* Save character received by UARTx device into the receive buffer */

    while(!(*(uint8_t*)buf = (unsigned char)UART_UartGetChar()));

    CharCnt++;                         /* Increase char counter */

    /* Stop reading if CR (Ox0D) character is received */

    if (*(uint8_t*)buf == 0x0DU) {     /* New line character (CR) received ? */

      *(uint8_t*)buf = '\n';           /* Yes, convert LF to '\n' char. */

      break;                           /* Stop loop and return received char(s) */

    }

    buf++;                   /* Increase buffer pointer */

  }

  //return 1; /* WRONG! */

  return CharCnt;

}

//and now all the standard c io stuff works.

int main(void) {

    char buf[100]="";

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start();

    printf("Hello World\n\r");

    for(;;) {

//        char c = UART_UartGetChar();

//        UART_UartPutChar(toupper(c));

        //gets(buf); //bad idea. No size limit, no\r on input

        fgets(buf, sizeof(buf), stdin);

        for (int i=0;i<sizeof(buf) && buf;i++) {

            buf = toupper(buf);

            }

        printf(buf); //not secure

        }

}

0 Likes