- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
|
|
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
}
}