B4R Question Changing serial port 2 parameters

Gerardo Tenreiro

Active Member
Licensed User
Hello, I need to change the communication speed of serial port 2 while executing the program. There is a team that starts communication at 19200 and then changes to 115200.
De momento tengo el segundo puerto serial funcionando a una velocidad fija de 11500 con el siguiente código:
' Variables Globales de Proceso
Sub Process_Globals

Public Serial1 As Serial ' Puerto Serie 1
Public SerialNative2 As Stream ' Puerto Serie 2
Private PS2 As AsyncStreams ' Entrada de Datos del Puerto Seria

'Private wifi As ESP8266WiFi

Private T_1s As Timer ' Timer Cada Segundo

Public T_Principal As Timer ' Timer Bucle Principal

Public CPS_1 As Int ' Ciclos Por Segundo Rutina Principal

End Sub

' Se ejecuta una vez en el Arranque
Private Sub AppStart

' Inicializa el Puerto Serie 1
Serial1.Initialize(9600) ' Puerto de Comunicacion con el Encoder
RunNative("SerialNative2", null) ' Puerto Serie del VF
' Inicializa el Puerto Serie 2
PS2.Initialize(SerialNative2, "VF_Newdata","VF_Error")


' Lanza Tarea Principal
T_Principal.Initialize("T_Principal_Tick",CONFIG.PLC_TC) ' Sobre 185 Ciclos por Segundo
T_Principal.Enabled = True

' Inicializa Comunicacion con el VF
VF.Inicializa_MODBUS

' Inicia el PLC
PLC.Inicia_PLC

End Sub
'
' Entrada de Datos del VF
'
private Sub VF_Newdata (Buffer() As Byte)

' Si Hay datos en el Buffer, los procesa
If Buffer.Length >0 Then
' Gestiona Telegrama
VF.Entro_Telegrama(Buffer)

Log("Entro PS2 = ",Buffer)
End If
End Sub


'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'
' Tarea Principal que se Ejecuta Ciclicamente
' Al Principio Se procesan los Flancos de las Teclas, Al Final se Borran los Flancos
Private Sub T_Principal_Tick
' ++++++++++++++++++++++++++++++
' Rutina Principal
' Analiza Los Flancos de las Teclas
TECLADO.Flancos_de_Teclado
' Gestion del Menu
TECLADO.Gestion_del__Menu

' Lee y Escribe los Expansores
IO.Lectura_Escritura_Expansores

' Si el PLC esta en RUN Procesa programa Puerta
If CONFIG.PLC_RUN = True Then
PLC.PLC_0 ' Bloque 0 del PLC
End If
' Cuenta el Numero de Ciclos por Segundo
CPS_1 = CPS_1 + 1

End Sub
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


#if C
void SerialNative2(B4R::Object* unused)
{
::Serial2.begin(115200);
b4r_main::_serialnative2->wrappedStream = &::Serial2;
}
#End If

The intention is to pass a speed configuration parameter to the code
#if C
void SerialNative2(B4R::Object* unused)
{
::Serial2.begin(115200);
b4r_main::_serialnative2->wrappedStream = &::Serial2;
}
#EndIf
That instead of always using 115200 to be able to make a call

RunNative("SerialNative2", 19200) or RunNative("SerialNative2", 115200) as needed, including RunNative("SerialNative2", 9600),...

This is possible?
Thank you so much
 

Gerardo Tenreiro

Active Member
Licensed User
Please use [code]code here...[/code] tags when posting code.

See the second example here: https://www.b4x.com/android/forum/threads/inline-c-c.65714/#content
Thanks Erel.
I think I understood you, but this code compiles fine and the result is wrong:
Sub Process_Globals
Public Serial1 As Serial
Public SerialNative2 As Stream ' Puerto Serie 2
Public brate2 As UInt = 115200
End Sub
Private Sub AppStart
Dim TS(8) As Byte
Serial1.Initialize(115200) ' Inicializa el Puerto Serie 1
brate2 = 115200 ' Velocidad ddel Puerto Serie 2
RunNative("SerialNative2",Null) ' Inicializa el Puerto Serie 2
TS(0) = 1
TS(1) = 3
TS(2) = 0
TS(3) = 100
TS(4) = 0
TS(5) = 6
TS(6) = 132
TS(7) = 23
SerialNative2.WriteBytes(TS,0,TS.Length)
End Sub
#if C
void SerialNative2(B4R::Object* unused)
{
::Serial2.begin(b4r_main::_brate2,SERIAL_8N1);
b4r_main::_serialnative2->wrappedStream = &::Serial2;
}
#End If

1684681197251.png



while this other code if it works correctly
Sub Process_Globals
Public Serial1 As Serial
Public SerialNative2 As Stream ' Puerto Serie 2
Public brate2 As UInt = 115200
End Sub
Private Sub AppStart
Dim TS(8) As Byte
Serial1.Initialize(115200) ' Inicializa el Puerto Serie 1
brate2 = 115200 ' Velocidad ddel Puerto Serie 2
RunNative("SerialNative2",Null) ' Inicializa el Puerto Serie 2
TS(0) = 1
TS(1) = 3
TS(2) = 0
TS(3) = 100
TS(4) = 0
TS(5) = 6
TS(6) = 132
TS(7) = 23
SerialNative2.WriteBytes(TS,0,TS.Length)
End Sub
#if C
void SerialNative2(B4R::Object* unused)
{
::Serial2.begin(115200,SERIAL_8N1);
b4r_main::_serialnative2->wrappedStream = &::Serial2;
}
#End If

1684681140666.png


The only difference in the code is:
::Serial2.begin(115200,SERIAL_8N1); --> works OK
::Serial2.begin(b4r_main::_brate2,SERIAL_8N1); --> frame error

Any ideas, does the brate variable have to be an int or another type?
thanks a lot
 
Upvote 0

candide

Active Member
Licensed User
can you check this post :

you can make a search for "Serial port 2" and you will see several threads on similar question
 
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
puedes checar esta publicacion:

puede realizar una búsqueda de "Puerto serie 2" y verá varios hilos sobre una pregunta similar
Hi, I can get serial port 2 to work correctly. I checked the posts and the code I attached works correctly, the problem I have is changing the communication speed of serial port 2 in the program loop so that each time I can have different parameters of the serial port.

If you set the serial port to 115200 with the instruction
::Serial2.begin(115200, SERIAL_8N1);
It works correctly, on the other hand, if instead of the value "115200" it passes a variable, the serial port 2 does not work correctly, review the previous post where I explain determine the problem.


Please read this carefully: https://www.b4x.com/android/forum/threads/changing-serial-port-2-parameters.148023/post-938558

Thank you so much
We are still trying to find the right key to fix the problem. :D:D:D
 
Upvote 0
Top