2020-11-04 06:42 PM
Dear Members,
I have this code :
HAL_UART_Receive_IT (&huart3, Rx_data, 4);
I tested from RealTerm, it worked but I need to click "send" 4 times,
How can I make it only one click for the whole string ?
In STM32 side :
char Rx_data[10];
char buf2 [] = "1234";
char buf3 [] = "5678";
.
.
.
i = strcmp (Rx_data,buf2);
if (i==0)
{
printf("char match RELAY 1 OFF \n");
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14,GPIO_PIN_SET); // RELAY OFF
//HAL_UART_Receive_IT(&huart3, Rx_data, 4);
}
i = strcmp (Rx_data,buf3);
if (i==0)
{
printf("char match RELAY 1 ON\n");
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14,GPIO_PIN_RESET); // RELAY ON
//HAL_UART_Receive_IT(&huart3, Rx_data, 4);
}
Any ideas how ?
Cheers
2020-11-05 03:19 PM
The code I use in PC , I developed with delphi,
procedure TForm1.Button3Click(Sender: TObject);
var
BytesWritten: DWORD;
begin
{SEND COM}
//Relay ON
s :='5678';
s := s + #13;
WriteFile(ComFile, s[1], Length(s), BytesWritten, nil);
end;
Which part is not correct ? STM32 side or PC side ?
Thanks
2020-11-05 04:09 PM
Not going to be able to diagnose from random disconnected fragments of code.
When the buffer gets filled you will get a callback, HAL_UART_Receive_IT(&huart3, Rx_data, 4) returns immediately. it will not zero terminate the buffer as a string, you will need to insure that if using string functions.
Perhaps create a polled loop we you see all 4 or 5 characters, one at a time.
2020-11-05 04:41 PM
Hi Clive, thanks for the reply,
...............
create a polled loop we you see all 4 or 5 characters, one at a time
..........
will it be nearly the same on how to decode GPS message, we spoke before ?
2020-11-05 06:15 PM
Well you could use that as a template.
You might want to check out the UART interrupt examples for your platform.
Generally the issue with requesting 4 characters is that if you get over or under you might not stay synchronized
2020-11-07 05:59 AM
Thanks for the reply,
I did this experiment :
if (Verbose)
{
puts("Decode UART3");
printf("%s\n",s);
}
printf("Check the line synchronization\n");
f=0;
while(1)
{
field[f++] = s;
while((*s != 0x0D) && (*s != 0x0A))
s++; //Line 173 sim900.h
if ((*s == 0x0D) || (*s == 0x0A) || (f == (FIELD_MAX - 1)))
{
*s = 0;
field[f] = NULL;
break;
}
*s++ = 0; // Line 183 sim900.h
}
if (Verbose)
{
printf("Fields %02d\n",f);
for(i=0; i<f; i++)
printf("#%02d : %s\n",i,field[i]);
}
if ((strcmp(&field[0][3],"1234") == 0) && (f > 5)) // is it 1234 ?
{
//Turn on port
printf("1234 Received \n");
}
I gave input 1234+CR from RealTerm,
and got output :
Decode UART3
(
Check the li
ne synchronization
Fields 01
#00 : (
Decode UART3
(
Check the line synchroniza
tion
Fields 01
#00 : (
Decode UART3
(
Check the line synchronization
Fields 01
#00 : (
almost get it, which part should I fix ? thanks