cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC 5, 3 & 1 MCU

New Contributor II

I am new to this embedded programming. I am a .net developer and planing to read data from Psoc. Can you please guide me on this.

0 Likes
Reply
1 Solution
Esteemed Contributor

Dear Abinash-san,

I googled "C# Serial" and found the following URL

SerialPort クラス (System.IO.Ports) | Microsoft Docs

It was a sample chat program, so I modified it to work with the PSoC Program,

namely, I change

(1) line 54

From

                _serialPort.WriteLine(

                   String.Format("<{0}>: {1}", name, message));

To

                _serialPort.WriteLine(

                    message);

(2) commented line 39 and 40, as PSoC will not care my name

//       Console.Write("Name: ");

//       name = Console.ReadLine();

The total source is now

========================

// Use this code inside a project created with the Visual C# > Windows Desktop > Console Application template.

// Replace the code in Program.cs with this code.

using System;

using System.IO.Ports;

using System.Threading;

public class PortChat

{

    static bool _continue;

    static SerialPort _serialPort;

    public static void Main()

    {

        string name;

        string message;

        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.

        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.

        _serialPort.PortName = SetPortName(_serialPort.PortName);

        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);

        _serialPort.Parity = SetPortParity(_serialPort.Parity);

        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);

        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);

        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

        // Set the read/write timeouts

        _serialPort.ReadTimeout = 500;

        _serialPort.WriteTimeout = 500;

        _serialPort.Open();

        _continue = true;

        readThread.Start();

//       Console.Write("Name: ");

//       name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

        while (_continue)

        {

            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))

            {

                _continue = false;

            }

            else

            {

                _serialPort.WriteLine(

                    // String.Format("<{0}>: {1}", name, message));

                    message);

            }

        }

        readThread.Join();

        _serialPort.Close();

    }

    public static void Read()

    {

        while (_continue)

        {

            try

            {

                string message = _serialPort.ReadLine();

                Console.WriteLine(message);

            }

            catch (TimeoutException) { }

        }

    }

    // Display Port values and prompt user to enter a port.

    public static string SetPortName(string defaultPortName)

    {

        string portName;

        Console.WriteLine("Available Ports:");

        foreach (string s in SerialPort.GetPortNames())

        {

            Console.WriteLine("   {0}", s);

        }

        Console.Write("Enter COM port value (Default: {0}): ", defaultPortName);

        portName = Console.ReadLine();

        if (portName == "" || !(portName.ToLower()).StartsWith("com"))

        {

            portName = defaultPortName;

        }

        return portName;

    }

    // Display BaudRate values and prompt user to enter a value.

    public static int SetPortBaudRate(int defaultPortBaudRate)

    {

        string baudRate;

        Console.Write("Baud Rate(default:{0}): ", defaultPortBaudRate);

        baudRate = Console.ReadLine();

        if (baudRate == "")

        {

            baudRate = defaultPortBaudRate.ToString();

        }

        return int.Parse(baudRate);

    }

    // Display PortParity values and prompt user to enter a value.

    public static Parity SetPortParity(Parity defaultPortParity)

    {

        string parity;

        Console.WriteLine("Available Parity options:");

        foreach (string s in Enum.GetNames(typeof(Parity)))

        {

            Console.WriteLine("   {0}", s);

        }

        Console.Write("Enter Parity value (Default: {0}):", defaultPortParity.ToString(), true);

        parity = Console.ReadLine();

        if (parity == "")

        {

            parity = defaultPortParity.ToString();

        }

        return (Parity)Enum.Parse(typeof(Parity), parity, true);

    }

    // Display DataBits values and prompt user to enter a value.

    public static int SetPortDataBits(int defaultPortDataBits)

    {

        string dataBits;

        Console.Write("Enter DataBits value (Default: {0}): ", defaultPortDataBits);

        dataBits = Console.ReadLine();

        if (dataBits == "")

        {

            dataBits = defaultPortDataBits.ToString();

        }

        return int.Parse(dataBits.ToUpperInvariant());

    }

    // Display StopBits values and prompt user to enter a value.

    public static StopBits SetPortStopBits(StopBits defaultPortStopBits)

    {

        string stopBits;

        Console.WriteLine("Available StopBits options:");

        foreach (string s in Enum.GetNames(typeof(StopBits)))

        {

            Console.WriteLine("   {0}", s);

        }

        Console.Write("Enter StopBits value (None is not supported and \n" +

         "raises an ArgumentOutOfRangeException. \n (Default: {0}):", defaultPortStopBits.ToString());

        stopBits = Console.ReadLine();

        if (stopBits == "")

        {

            stopBits = defaultPortStopBits.ToString();

        }

        return (StopBits)Enum.Parse(typeof(StopBits), stopBits, true);

    }

    public static Handshake SetPortHandshake(Handshake defaultPortHandshake)

    {

        string handshake;

        Console.WriteLine("Available Handshake options:");

        foreach (string s in Enum.GetNames(typeof(Handshake)))

        {

            Console.WriteLine("   {0}", s);

        }

        Console.Write("Enter Handshake value (Default: {0}):", defaultPortHandshake.ToString());

        handshake = Console.ReadLine();

        if (handshake == "")

        {

            handshake = defaultPortHandshake.ToString();

        }

        return (Handshake)Enum.Parse(typeof(Handshake), handshake, true);

    }

}

========================

Then I modified PSoC's program as below

main.c

========================

#include "project.h"

#define LED_ON 1u

#define LED_OFF 0u

#define USBFS_DEVICE    (0u)

char detorecibido ;

int main(void)

{

    uint16 count;

    char c ;

    int i ;

   

    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Start USBFS operation with 5-V operation. */

    USBUART_Start(USBFS_DEVICE, USBUART_5V_OPERATION);

    for(;;) {

       /* Host can send double SET_INTERFACE request. */

        if (0u != USBUART_IsConfigurationChanged())

        {

            /* Initialize IN endpoints when device is configured. */

            if (0u != USBUART_GetConfiguration())

            {

                /* Enumeration is done, enable OUT endpoint to receive data

                 * from host. */

                USBUART_CDC_Init();

            }

        }

        /* Service USB CDC when device is configured. */

        if (0u != USBUART_GetConfiguration())

        {

            /* Check for input data from host. */

            if (0u != USBUART_DataIsReady())

            {

                count = USBUART_GetCount() ;

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

                     c = USBUART_GetChar() ;

                    switch(c) {

                    case 'd':

                    case 'D':

                        LED_Write(LED_OFF) ;

                        USBUART_PutString("_\n\r") ;

                        break ;

                    case 'l':

                    case 'L':

                        LED_Write(LED_ON) ;

                        USBUART_PutString("*\n\r") ;    

                        break ;

                    default:

                        break ;

                    }

                }

            }

        }

    }

}

========================

After compiling both of the project,

(1) Start PSoC's program

(2) Start C# program

The result output of C# program was

when I typed "l" and enter, the LED turns on and "*" was written

when I typed "d" and enter, the LED turns off and "_"  was written

000-program.JPG

UartTest200727 is for .NET/C#

uart_test_050_200727C is for PSoC 5LP

Best Regards,

27-Jul-2020

Motoo Tanaka

View solution in original post

20 Replies
Moderator
Moderator

We have a C Sharp example project for programming a PSoC5 device. Please find the example at this path: "C:\Program Files (x86)\Cypress\Programmer\Examples\Programming\PSoC3_5\SWD\C_Sharp"

Note that you need PSoC Programmer software installed to be able to view this path. If you don't have it already, then you can download it from here: https://www.cypress.com/products/psoc-programming-solutions

Regards,
Dheeraj

0 Likes
Reply
Honored Contributor

abho,

Can you be more specific about what you what the PC host to do?  

For example, in the past I created a C# program that opens a COM port to provide UART-style communication between the PSoC and the PC.  I then parse the incoming Rx PSoC communication data.  As needed, I command the PSoC via the Tx to change modes and the such.

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
Reply
New Contributor II

Hi Len,

How you connect this psoc program with visual studio and how you read the psoc data in visual studio?

Please help me to provide the steps and sample application which will help me a lot.

Thanks,

Abinash

0 Likes
Reply
Esteemed Contributor

Hi,

In my understanding, a PC application and a firmware are very different species.

A PC application is built using a tool such as Visual Studio and runs on an OS such as Windows.

On the other hand a firmware is built using a tool such as PSoC Creator and runs without an OS or on a Real Time OS.

We could arrange both programs to communicate via interface(s) exist on both hardware (PC and PSoC Board),

such as UART, USB, ..

000-vs-creator.JPG

But from the last response in the discussion below,

choose Pin and port for Cypress CY8CKIT-050 5LP

I have a feeling that what Abinash-san wants to do is

Building both PSoC firmware and PC application with visual studio, especially in C#.

001-vs-only.JPG

So I think that the question is if we can generate PSoC firmware using visual studio and C#?

Considering that Cypress is not supporting even C++ with PSoC Creator,

expecting C# to generate PSoC Native binary and program/debug from visual studio may not be the easiest thing to do.

May be possible, if Visual Studio allows to use a GNU tool chain and if the GNU tool chain contains C# and a cross compiler for Cortex-M3 (or for PSoC 5LP).

Any suggestion(s) and/or opinions?

moto

0 Likes
Reply
Esteemed Contributor

Hi,

 You can use VSCODE with Modus Toolbox2.1, but is there any good way?

Regards,

Kenshow

0 Likes
Reply
New Contributor II

Hi Team,

Below is my requirement.

We are planing to do a PH Meter project where the sensor read PH value from the water and send to Micro-controller. Wanted to read that data from my Window application and display.

1.Can you please suggest how can i achieve it?

2.Will PH Sensor send value to microcontroller automatically or we need to write any firmware for that?

Thanks and Regards,

Abinash

0 Likes
Reply
Esteemed Contributor

Hi,

Usually connection would look like

002-block-diagram.JPG

(1) Connect the sensor to the PSoC EVB via the interface which the sensor supports.

(2) PSoC's firmware reads (and may be, calculate) the sensor value and output it to the interface connected to the PC

(3) PC reads the data from PSoC and displays it (in your way.)

This case unless there is/are ready made sample code for the sensor and the PSoC board,

most likely you need to write a program (firmware) to do that.

Note: There are many sample code in the community code example area

Community Code Examples

Then you need to send data from the PSoC board to PC.

IMHO, probably the easiest way is using UART and UART_PutString() or printf type functions.

Finally, PC's application needs to read the data from the COM port and display it "in your way".

Again, the easiest would be using a serial terminal program such as Tera Term to display the numbers.

Or using SerialPlot to show it as a graph of lines.

The following USL is my sample of using CCS811 air sensor and displayed the data as number or graph.

How I recycle the data (aka Return of CCS811)

Or you can write as fancy program as you like with your windows development environment.

moto

0 Likes
Reply
Honored Contributor II

abho,

you can display data using plot charting application (SerialPlot), no coding required (on PC side)

SerialPlot: interface to real-time data charts

/odissey1

New Contributor II

Hi,

For my dev kit(CY8CKIT-050 PSOC 5LP ) any kitprog USB-UART driver required?

0 Likes
Reply
Honored Contributor

abho,

For the CY8CKIT-050 PSOC 5LP if you are using the USBFS_UART component with the board's J2 connector to connect to the PC, you will need the USBUART_cdc.inf during the driver install phase.

It should be on your system when you installed either the PSoC Creator or PSoC Programmer.   However, I attached it here just in case.

Once successfully installed AND you have your CY8CKIT-050 programmed with code to use the USBFS_UART AND you connect your kit to the PC with the appropriate USB cable, you should see a new COM port in the Device Manager.

Here is a another reference discussion link:

CY8CKIT-50 PSOC5 development Kit USBUART driver not working on windows 10

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
Reply
Esteemed Contributor

Hi,

Please refer to my response at the discussion below.

Re: Not able to see serial port enabled in teraterm for (CY8CKIT-050)PSOC 5LP

moto

P.S. Yes, In case you use USBFS-UART component the inf file Len-san attached is required.

0 Likes
Reply
New Contributor II

Hi Motoo,

Can you please help me to send very simple c# application of USBUART style which send a character(Let say L) then LED will ON. When i send lets say(D) then LED will off.

Thanks and Regards,

Abinash

0 Likes
Reply
Esteemed Contributor

Dear Abinash-san,

Don't specify my name, there are many experts here.

And I am NOT an expert of C#/.net so I can not help you about C#/.net.

Having written above, in the discussion below, Len-san has already provided a sample C# source code.

How can i integrate Psoc with c#.net?

PC Terminal Program with C# Source Code

Meantime, in the discussion below, I have already provided you a couple of projects

which turn on LED when '0' is typed, and turn off LED when '1' is typed.

So just change '0' to 'D' and '1' to 'L' will do what you are asking.

Re: Not able to see serial port enabled in teraterm for (CY8CKIT-050)PSOC 5LP

Use the sample code "uart_test_050_200721B".

Best Regards,

26-Jul-2020

Motoo Tanaka

0 Likes
Reply
New Contributor II

Hi Team,

The program u shared is not getting any clue as there is baudrate is coming which is not possible to configure in PSOC in USBUART Style. Please send a very simple c# application of USBUART style which send a character(Let say L) then LED will ON. When i send lets say(D) then LED will off.

Note:-I am able to execute through TeraTerm, but need an application.I am using CYC8CKIT-050 PSOC 5LP

Atleast tell me the simple code how can i send a character and receive data from the PSOC Device in c# USBUART style?

Thanks,

Abinash

0 Likes
Reply
Honored Contributor

Abinash,

I'm confused.  Are you looking for a C# style program to run on the PC or to run on the PSoC?

Len

Len
"Engineering is an Art. The Art of Compromise."
0 Likes
Reply
New Contributor II

Let me clear things...

I have already a PSOC program which i am using in USBUART style.

My question is how can i send command from my C# Application to psoc, lets say any character "L" which ll turn on the LED. If i send a command "D" it should turned off the LED. So i need a very simple program in C# which will do this.

Assume i am using Window Application in C#.

Thanks,

Abinash

0 Likes
Reply
Esteemed Contributor

Dear Abinash-san,

I googled "C# Serial" and found the following URL

SerialPort クラス (System.IO.Ports) | Microsoft Docs

It was a sample chat program, so I modified it to work with the PSoC Program,

namely, I change

(1) line 54

From

                _serialPort.WriteLine(

                   String.Format("<{0}>: {1}", name, message));

To

                _serialPort.WriteLine(

                    message);

(2) commented line 39 and 40, as PSoC will not care my name

//       Console.Write("Name: ");

//       name = Console.ReadLine();

The total source is now

========================

// Use this code inside a project created with the Visual C# > Windows Desktop > Console Application template.

// Replace the code in Program.cs with this code.

using System;

using System.IO.Ports;

using System.Threading;

public class PortChat

{

    static bool _continue;

    static SerialPort _serialPort;

    public static void Main()

    {

        string name;

        string message;

        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;

        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.

        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.

        _serialPort.PortName = SetPortName(_serialPort.PortName);

        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);

        _serialPort.Parity = SetPortParity(_serialPort.Parity);

        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);

        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);

        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

        // Set the read/write timeouts

        _serialPort.ReadTimeout = 500;

        _serialPort.WriteTimeout = 500;

        _serialPort.Open();

        _continue = true;

        readThread.Start();

//       Console.Write("Name: ");

//       name = Console.ReadLine();

        Console.WriteLine("Type QUIT to exit");

        while (_continue)

        {

            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))

            {

                _continue = false;

            }

            else

            {

                _serialPort.WriteLine(

                    // String.Format("<{0}>: {1}", name, message));

                    message);

            }

        }

        readThread.Join();

        _serialPort.Close();

    }

    public static void Read()

    {

        while (_continue)

        {

            try

            {

                string message = _serialPort.ReadLine();

                Console.WriteLine(message);

            }

            catch (TimeoutException) { }

        }

    }

    // Display Port values and prompt user to enter a port.

    public static string SetPortName(string defaultPortName)

    {

        string portName;

        Console.WriteLine("Available Ports:");

        foreach (string s in SerialPort.GetPortNames())

        {

            Console.WriteLine("   {0}", s);

        }

        Console.Write("Enter COM port value (Default: {0}): ", defaultPortName);

        portName = Console.ReadLine();

        if (portName == "" || !(portName.ToLower()).StartsWith("com"))

        {

            portName = defaultPortName;

        }

        return portName;

    }

    // Display BaudRate values and prompt user to enter a value.

    public static int SetPortBaudRate(int defaultPortBaudRate)

    {

        string baudRate;

        Console.Write("Baud Rate(default:{0}): ", defaultPortBaudRate);

        baudRate = Console.ReadLine();

        if (baudRate == "")

        {

            baudRate = defaultPortBaudRate.ToString();

        }

        return int.Parse(baudRate);

    }

    // Display PortParity values and prompt user to enter a value.

    public static Parity SetPortParity(Parity defaultPortParity)

    {

        string parity;

        Console.WriteLine("Available Parity options:");

        foreach (string s in Enum.GetNames(typeof(Parity)))

        {

            Console.WriteLine("   {0}", s);

        }

        Console.Write("Enter Parity value (Default: {0}):", defaultPortParity.ToString(), true);

        parity = Console.ReadLine();

        if (parity == "")

        {

            parity = defaultPortParity.ToString();

        }

        return (Parity)Enum.Parse(typeof(Parity), parity, true);

    }

    // Display DataBits values and prompt user to enter a value.

    public static int SetPortDataBits(int defaultPortDataBits)

    {

        string dataBits;

        Console.Write("Enter DataBits value (Default: {0}): ", defaultPortDataBits);

        dataBits = Console.ReadLine();

        if (dataBits == "")

        {

            dataBits = defaultPortDataBits.ToString();

        }

        return int.Parse(dataBits.ToUpperInvariant());

    }

    // Display StopBits values and prompt user to enter a value.

    public static StopBits SetPortStopBits(StopBits defaultPortStopBits)

    {

        string stopBits;

        Console.WriteLine("Available StopBits options:");

        foreach (string s in Enum.GetNames(typeof(StopBits)))

        {

            Console.WriteLine("   {0}", s);

        }

        Console.Write("Enter StopBits value (None is not supported and \n" +

         "raises an ArgumentOutOfRangeException. \n (Default: {0}):", defaultPortStopBits.ToString());

        stopBits = Console.ReadLine();

        if (stopBits == "")

        {

            stopBits = defaultPortStopBits.ToString();

        }

        return (StopBits)Enum.Parse(typeof(StopBits), stopBits, true);

    }

    public static Handshake SetPortHandshake(Handshake defaultPortHandshake)

    {

        string handshake;

        Console.WriteLine("Available Handshake options:");

        foreach (string s in Enum.GetNames(typeof(Handshake)))

        {

            Console.WriteLine("   {0}", s);

        }

        Console.Write("Enter Handshake value (Default: {0}):", defaultPortHandshake.ToString());

        handshake = Console.ReadLine();

        if (handshake == "")

        {

            handshake = defaultPortHandshake.ToString();

        }

        return (Handshake)Enum.Parse(typeof(Handshake), handshake, true);

    }

}

========================

Then I modified PSoC's program as below

main.c

========================

#include "project.h"

#define LED_ON 1u

#define LED_OFF 0u

#define USBFS_DEVICE    (0u)

char detorecibido ;

int main(void)

{

    uint16 count;

    char c ;

    int i ;

   

    CyGlobalIntEnable; /* Enable global interrupts. */

    /* Start USBFS operation with 5-V operation. */

    USBUART_Start(USBFS_DEVICE, USBUART_5V_OPERATION);

    for(;;) {

       /* Host can send double SET_INTERFACE request. */

        if (0u != USBUART_IsConfigurationChanged())

        {

            /* Initialize IN endpoints when device is configured. */

            if (0u != USBUART_GetConfiguration())

            {

                /* Enumeration is done, enable OUT endpoint to receive data

                 * from host. */

                USBUART_CDC_Init();

            }

        }

        /* Service USB CDC when device is configured. */

        if (0u != USBUART_GetConfiguration())

        {

            /* Check for input data from host. */

            if (0u != USBUART_DataIsReady())

            {

                count = USBUART_GetCount() ;

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

                     c = USBUART_GetChar() ;

                    switch(c) {

                    case 'd':

                    case 'D':

                        LED_Write(LED_OFF) ;

                        USBUART_PutString("_\n\r") ;

                        break ;

                    case 'l':

                    case 'L':

                        LED_Write(LED_ON) ;

                        USBUART_PutString("*\n\r") ;    

                        break ;

                    default:

                        break ;

                    }

                }

            }

        }

    }

}

========================

After compiling both of the project,

(1) Start PSoC's program

(2) Start C# program

The result output of C# program was

when I typed "l" and enter, the LED turns on and "*" was written

when I typed "d" and enter, the LED turns off and "_"  was written

000-program.JPG

UartTest200727 is for .NET/C#

uart_test_050_200727C is for PSoC 5LP

Best Regards,

27-Jul-2020

Motoo Tanaka

View solution in original post

New Contributor II

Hi Len,

Can you send a c# sample application where you implemented UART-style communication and parseing the incoming Rx PSoC communication data?

Can you please help me to tell the steps you follow to do this?

0 Likes
Reply
Honored Contributor

Abinash,

Here is an answer to your question:

PC Terminal Program with C# Source Code

Len

Len
"Engineering is an Art. The Art of Compromise."
Contributor II

Hi,

if the intention is to really have the PSoC application written in C#, you might want to look at the nanoFramework project:

nanoFramework – Making it easy to write C# code for embedded systems.

However, there's currently no PSoC ported version available. And because of the PSoC device nature you'll need extensive support from Cypress as well if you want to port it.

Regards

0 Likes
Reply