10-17-2024 06:19 AM
when we execute the code, then i send data from labview , then I have to relaunch the code to see the data in debug .
i tried for so may times.
it shows the data in format like this
index 0 ascii value - character
index 1 ascii value - character.
HELP!
10-17-2024 06:32 AM - edited 10-17-2024 06:33 AM
Hi Anup,
@Anup13 wrote:
when we execute the code, then i send data from labview , then I have to relaunch the code to see the data in debug .
HELP!
Place a loop in your STM code to have it response to messages as long as you like! No need to restart any code and initialize the COM port again and again…
Did you watch the video recommended in message #2? The same rules apply to your STM code…
10-17-2024 06:46 AM
i used while loop but still , i have to relaunch the code to check if data is receiving or not!
i have send screenshot of labVIEW block diagram.
this is the stm code:-
include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "stm32f4xx.h"
#include "system_stm32f4xx.h"
#include "uart.h"
#include "led.h"
#include "i2c.h"
#include "i2c_lcd.h"
#if !defined(_SOFT_FP) && defined(_ARM_FP)
#warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#endif
#define BUFFER_SIZE 32
void ConvertAsciiToLetters(char* buffer)
{
char result[BUFFER_SIZE]; // Array to hold the converted letters
int j = 0; // Index for the result array
// Iterate through the received string and convert numeric ASCII values to characters
for (int i = 0; i < strlen(buffer); i++)
{
if (buffer[i] >= '0' && buffer[i] <= '9') // If the character is a digit
{
char numStr[4]; // To store the number string (e.g., "65")
int k = 0;
// Extract the number from the string
while (buffer[i] >= '0' && buffer[i] <= '9' && k < 3) // Consider max 3 digits
{
numStr[k++] = buffer[i++];
}
numStr[k] = '\0'; // Null-terminate the number string
int asciiValue = atoi(numStr); // Convert the number string to an integer (ASCII value)
result[j++] = (char)asciiValue; // Convert the ASCII value to a character and store it in the result array
}
}
result[j] = '\0'; // Null-terminate the result string
// Print the converted result
UartPuts("Converted Letters: ");
UartPuts(result); // Send the converted string back through UART
UartPuts("\r\n");
}
// Define the buffer size
int main()
{
SystemInit();
UartInit(BAUD_115200);
LedInit(LED_ORANGE);
char pqr[BUFFER_SIZE];
char abc[BUFFER_SIZE];
char xyz[BUFFER_SIZE];
// Declare buffer using the defined size
UartGets(abc);
while(1)
{
UartPuts("Received data: "); // Debug statement
UartPuts(abc); // Print received data
UartPuts("\r\n");
// Check if a string has been received and stored in the buffer
if (abc[0] != '\0' && abc[0] != '\r' && abc[0] != '\n') // Check if the buffer is not empty
{
LedBlink(LED_ORANGE, 1000); // Blink the LED
UartPuts("Data received: ");
UartPuts(pqr); // Send the string back
UartPuts("\r\n");
copyString(abc,xyz);
// Convert ASCII values in the buffer to letters
ConvertAsciiToLetters(abc);
}
else
{
// Buffer is empty, no string received
UartPuts("No data received.\r\n");
}
}
return 0;
}
void copyString(char src[], char dest[]) {
int i = 0;
// Manually copy characters one by one from src to dest
while (src[i] != '\0') {
dest[i] = src[i]; // Copy the character
i++;
}
dest[i] = '\0'; // Null-terminate the destination string
}
10-17-2024 06:53 AM - edited 10-17-2024 06:54 AM
Hi Anup,
I'm no expert in C code, but it seems to me there is no UARTGET inside the while loop: even here you have to THINK DATAFLOW!
What is the effect of the "return(0)" inside the while loop? (Again, I'm no C expert…)
You should also cleanup your LabVIEW code and use a loop in there too to send multiple messages with a fixed delay…(Helps for debugging!)
10-17-2024 11:24 AM
Well, you do one UartGets() and after that go into a loop where you do pots and lots of UartPuts() to send back quite a bit of data but never ever do another UartGets(). To add insult to injury you never try to read in LabVIEW any of the information you keep putting on the serial line.
10-17-2024 11:24 PM
I'm taking response from STM32 like message "data received" in string indicator named as acknowledgement in labview
10-18-2024 09:04 AM
@Anup13 wrote:
I'm using VISA write function to write data to STM32 via UART, the data is showing in debug of stmcubeide, but its not processing further like blink an led or giving back response to labVIEW like " data received" or like printing in lcd display which is connected to stm32 via I2C.
help!
If you see the data in debug mode, then the data you submitted transferred successfully and there is no problem in your LabVIEW code...at least to where you write the data.
Your entire thread reads like your problem is in your C code and need to go to a C forum and ask why the incoming data is not being processed properly.
10-18-2024 02:43 PM - edited 10-18-2024 02:47 PM
@Eric1977 wrote:
Your entire thread reads like your problem is in your C code and need to go to a C forum and ask why the incoming data is not being processed properly.
Definitely. This is not really the right forum to ask about that code. Most of it is pretty much nonsense anyways. I did put a copy of it with some remarks properly formatted here:
void ConvertAsciiToLetters(char* buffer)
{
char result[BUFFER_SIZE]; // Array to hold the converted letters
int j = 0; // Index for the result array
// Iterate through the received string and convert numeric ASCII values to characters
for (int i = 0; i < strlen(buffer); i++)
{
if (buffer[i] >= '0' && buffer[i] <= '9') // If the character is a digit
{
char numStr[4]; // To store the number string (e.g., "65")
int k = 0;
// Extract the number from the string
while (buffer[i] >= '0' && buffer[i] <= '9' && k < 3) // Consider max 3 digits
{
numStr[k++] = buffer[i++];
}
numStr[k] = '\0'; // Null-terminate the number string
int asciiValue = atoi(numStr); // Convert the number string to an integer (ASCII value)
result[j++] = (char)asciiValue; // Convert the ASCII value to a character and store it in the result array
}
}
result[j] = '\0'; // Null-terminate the result string
// Print the converted result
UartPuts("Converted Letters: ");
UartPuts(result); // Send the converted string back through UART
UartPuts("\r\n");
}
// Define the buffer size
int main()
{
SystemInit();
UartInit(BAUD_115200);
LedInit(LED_ORANGE);
char pqr[BUFFER_SIZE];
char abc[BUFFER_SIZE];
char xyz[BUFFER_SIZE];
// Declare buffer using the defined size
// >>> reading once a string into abc from the uart and then never again, are you sure that is what you want??
// >>> I never used STM32 and your UartGets() function doesn't seem to be the standard function for the STM32 SDK.
// >>> No idea what library and software stack you use and can't read anywhere what UartGets() exactly does
// >>> in terms of termination character ending. It might or might not wait for a new line before returning.
// >>> If it doesn't you will have whatever it received if anything, and that will never ever change again since
// >>> you do not try to read new data into abc inside the loop.
UartGets(abc);
while(1)
{
// >>> Most likely you want to put the UartGets() function call here so it will update. However as it is
// >>> most likely blocking until it receives some data, it will block the loop execution here. You would
// >>> need a UartGets() function call with a timeout and then check that it successfully read some data
UartPuts("Received data: "); // Debug statement
UartPuts(abc); // Print received data
UartPuts("\r\n");
// Check if a string has been received and stored in the buffer
if (abc[0] != '\0' && abc[0] != '\r' && abc[0] != '\n') // Check if the buffer is not empty
{
// >>> The function says blink, but are you sure it switches state every loop iteration?
LedBlink(LED_ORANGE, 1000); // Blink the LED
UartPuts("Data received: ");
// >>> pqr never was assigned anything to, what are you trying to do???
UartPuts(pqr); // Send the string back
// >>> Since you explicitly append \r\n here, I assume UartPuts() doesn't do that itself?
// >>> if it doesn't, it's highly likely that UartGets() above doesn't really wait on such
// >>> a termination character (sequence) either before returning!
UartPuts("\r\n");
// >>> copying everything into xyz, and then, xyz is never used anywhere???
// >>> not sure if your C environment has a standard C library, but strcpy() from that
// >>> library does the same
copyString(abc, xyz);
// Convert ASCII values in the buffer to letters
// >>> so you send some decimal numbers in text form and then convert them to binary data
// >>> and then send that back, any specific reason for this? You already sent back the
// >>> received string above! The name is also wrong, it does convert to binary!!
ConvertAsciiToLetters(abc);
// >>> Possibly exit the loop if you receive a "Q" from the remote side?
if (abc[0] == 'Q')
break;
// >>> There is no pacing of the loop, it will execute as fast as the CPU can execute above
// >>> commands and then speed immediatly back to the start of the loop. Most likely so fast
// >>> that your LED will blink but with something like 100 or more Hertz. You need very super
// >>> fast eyes to see that! If you manage to get the UartGets() call with timeout at the
// >>> start of the loop, that timeout will pace the loop speed. Otherwise you really need
// >>> to do some delay in here.
}
else
{
// Buffer is empty, no string received
UartPuts("No data received.\r\n");
}
}
// >>> This never can be reached since the loop above is never terminated without the conditional break; I added above
return 0;
}
void copyString(char src[], char dest[])
{
// Manually copy characters one by one from src to dest
// >>> this does the same as your old code, but if you have standard C library available,
// >>> use strcpy(dest, src); instead of creating your own function
if (src && dest)
while (*dest++ = *src++);
}
Check the comments starting with // >>> to get some ideas what all is wrong or highly questionable. One of the main problems is that nobody here really knows your microprocessor board. Yes I have worked with STM32 board but they did not have any UartGets() or UartPuts() library functions, so I can't really be sure how they should work and it seems impossible to find any manual for these functions online. Other variants with slightly different names and more parameters but that doesn't help.