RS485 example

Use this section to discuss your embedded Flowcode projects.
Carmelo
Posts: 42
http://meble-kuchenne.info.pl
Joined: Thu Oct 14, 2021 10:04 am
Has thanked: 14 times

RS485 example

Post by Carmelo »

Hello everyone,
I am starting to use communications with pics and Flowcode and I would appreciate if you could show me some basic example of how to use RS485 communication.
At the output of the pic there would be a MAX487 driver placed and I don't know how to configure which block I should use and how to configure it to establish RS485 communication.

Thank you very much in advance for your help

BenR
Matrix Staff
Posts: 1739
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 440 times
Been thanked: 603 times

Re: RS485 example

Post by BenR »

Hello,

You would just need a UART component and a digital output to drive the data direction pin of the RS485 transciever. You can get transcievers that do not need the data direction pin but it looks like the one you have chosed MAX487 does need this. You can drive the DDR pin using say an output->LED component.

You will need to connect DE and RE pins together and connect this to your DDR output pin. Set the DDR pin to low to receive data and to high to send data.

Carmelo
Posts: 42
Joined: Thu Oct 14, 2021 10:04 am
Has thanked: 14 times

Re: RS485 example

Post by Carmelo »

Ya realicé mi primera experiencia enviando solo un dato numérico y pude comprobar que transceptor envía bien el dato (numérico) y que el receptor también lo recibe correctamente.

Ahora quisiera enviar una trama de 3-4 bytes de forma consecutiva para que el pic receptor los reciba y pueda decodificar la información enviada y de acuerdo a esos 3 bytes realice unas acciones determinadas. Pero en el los bloques de enviar por UART no sé cómo poder hacer esa operación de envío de los 3 bytes seguidos.

¿Me pueden indicar como realizar ese proceso y si es con un ejemplo simple estaría mejor para poder estudiar de como se realiza esa función?
F

chipfryer27
Valued Contributor
Posts: 1146
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 284 times
Been thanked: 412 times

Re: RS485 example

Post by chipfryer27 »

I already carried out my first experience sending only numerical data and I was able to verify that the transceiver sends the data (numeric) correctly and that the receiver also receives it correctly.

Now I would like to send a frame of 3-4 bytes consecutively so that the receiving pic receives them and can decode the information sent and according to those 3 bytes, perform certain actions. But in the blocks of sending via UART I don't know how to do this operation of sending the 3 bytes in a row.

Can you tell me how to carry out this process and if it is with a simple example, it would be better to be able to study how that function is carried out?
All hail Google translate......

I haven't any access to hardware to create / test an example, but there are a few ways to do what you want. Probably an easy way would be to use the SendByteArray and ReceiveByteArray component macros.

No tengo acceso al hardware para crear/probar un ejemplo, pero hay algunas formas de hacer lo que quieras. Probablemente una forma sencilla sería utilizar las macros de los componentes SendByteArray y ReceiverByteArray.

Regards

Carmelo
Posts: 42
Joined: Thu Oct 14, 2021 10:04 am
Has thanked: 14 times

Re: RS485 example

Post by Carmelo »

Thanks for the reply.
I can test the hardware, the problem is that I don't know how to configure the SendByte Array block. The 3 bytes to be sent are in three different registers (variables).
Can you tell me how to do it or any other way to send those bytes.

chipfryer27
Valued Contributor
Posts: 1146
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 284 times
Been thanked: 412 times

Re: RS485 example

Post by chipfryer27 »

Hi

From memory (as no access at present) so forgive any error.

Create a variable called (for example) Send and when you create it add in a dimension e.g for three bytes Send[3]. You now have an array in which you can store and retrieve three separate bytes.

You can assign values using a calculation. Note that for three bytes, the array is Send[0] / Send[1] / Send[2]

Send[0] = 1
Send[1] = 2
Send[2] = 3

(obviously you can also assign other variables such as Send[2] = x etc)

So you now have an array called Send containing three values and if you looked at the array in Simulation Debugger you would see Send [1,2,3]. You can access any of the variables directly. For example x = Send[2] would return the value of 3.

Now you can create an array containing the values you wish to transmit.

In the RS232 component you can use the SendByteArray and (I think) you then set the name of the array (e.g. Send) and the number of bytes to send (any value up to total length of array. A value of 1 would send the first byte by itself and a value of 3 would send first three bytes consecutively).

In the receiver, perhaps create another array called Receive and again assign a dimension of three ( Receive[3] ). Set up an interrupt on RxInt and in the ISR use the ReceiveByteArray component macro. In there you can set the number of bytes to attempt to receive and a timeout to call a halt to things.

Now if you sent the array Send, with three bytes, in the receiver your Receive array would contain [1,2,3].

There are other ways to do it, such as using a Circular Buffer, but the above may suit.

From Google ->

De memoria (ya que no hay acceso en este momento), así que perdonen cualquier error.

Cree una variable llamada (por ejemplo) Enviar y cuando la cree agregue una dimensión, por ejemplo, para tres bytes Enviar [3]. Ahora tiene una matriz en la que puede almacenar y recuperar tres bytes separados.

Puede asignar valores mediante un cálculo. Tenga en cuenta que para tres bytes, la matriz es Enviar[0] / Enviar[1] / Enviar[2]

Enviar[0] = 1
Enviar[1] = 2
Enviar[2] = 3

(obviamente también puedes asignar otras variables como Enviar[2] = x etc)

Entonces ahora tiene una matriz llamada Enviar que contiene tres valores y si observa la matriz en el Depurador de simulación verá Enviar [1,2,3]. Puede acceder a cualquiera de las variables directamente. Por ejemplo x = Enviar[2] devolvería el valor de 3.

Ahora puede crear una matriz que contenga los valores que desea transmitir.

En el componente RS232 puede usar SendByteArray y (creo) luego establece el nombre de la matriz (por ejemplo, Enviar) y la cantidad de bytes a enviar (cualquier valor hasta la longitud total de la matriz. Un valor de 1 enviaría el primer byte por sí solo y un valor de 3 enviaría los primeros tres bytes consecutivamente).

En el receptor, quizás cree otra matriz llamada Recibir y nuevamente asigne una dimensión de tres ( Recibir[3] ). Configure una interrupción en RxInt y en ISR use la macro del componente ReceiverByteArray. Allí puede establecer la cantidad de bytes que intentará recibir y un tiempo de espera para detener las cosas.

Ahora si enviaste el arreglo Enviar, con tres bytes, en el receptor tu arreglo Recibir contendría [1,2,3].

Hay otras formas de hacerlo, como utilizar un búfer circular, pero lo anterior puede ser adecuado.

Regards

Carmelo
Posts: 42
Joined: Thu Oct 14, 2021 10:04 am
Has thanked: 14 times

Re: RS485 example

Post by Carmelo »

Muchas gracias por la respuesta.
Creo entender lo que me ha indicado.
Mañana lo pondré en práctica y probaré.
Una cuestion más:
¿Me imagino que a cualquiera de las posiciones del array se le puede asignar cualquier valor, incluso el valor de una tecla pulsada?

chipfryer27
Valued Contributor
Posts: 1146
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 284 times
Been thanked: 412 times

Re: RS485 example

Post by chipfryer27 »

Hi

Prácticamente cualquier valor que desees. Si crea una matriz de bytes, entonces se puede almacenar cualquier valor de byte, si es una matriz de enteros, entonces cualquier valor entero, etc.

Capture el valor del teclado y asígnelo a cualquier posición en la matriz que desee (por ejemplo, matriz [3] = pulsación de tecla).

También puedes enviar una cadena de caracteres.

¿Qué es lo que realmente quieres enviar?

- - - -

Pretty much any value you want. If you create a Byte array then any byte value can be stored, if an Integer array then any integer value etc.

Capture the keyboard value and assign it to whatever position in the array you want (e.g. array[3] = keypress).

You could also send a String of characters.

What is it you actually want to send?

Regards

Carmelo
Posts: 42
Joined: Thu Oct 14, 2021 10:04 am
Has thanked: 14 times

Re: RS485 example

Post by Carmelo »

De nuevo muchas gracias por las respuestas.
Primeramente lo que quiero hacer es conocer Flowcode y no solo realizar cosas como activar o leer un pin, o escribir un texto en LCD.

En realidad con esto de la comunicación 485 es tener un maestro y un esclavo de forma que cada uno de ellos lea su propio teclado conectado a el. Que el valor de la tecla pulsada se envié al otro y que el valor recibido active/ desactive una salida.

Pero voy con calma, por qué quiero ir entendiendo bien cada paso que vaya realizando.

De momento voy a probar esto que me ha indicado, enviar y recibir esos datos.

Carmelo
Posts: 42
Joined: Thu Oct 14, 2021 10:04 am
Has thanked: 14 times

Re: RS485 example

Post by Carmelo »

Well, I carried out the test according to the instructions described.
If I set fixed values ​​to the array indices, such as 14 and 75, the values ​​that are sent and received are always correct, at least in the "virtual terminal".
However, if you change the value of index 1 and assign it the value of the key pressed, the transmitted value is no longer correct. Index 0 always transmits it well but index 1 does not (key pressed value)
It always transmits a 04 that only changes to 07 if the keys pressed are 5,8,0.
I attach a screenshot of the transmission and reception and a small example that I am implementing to see if it tells me what I may be doing wrong.
Thanks in advance for the possible answers.
Attachments
captura1.png
captura1.png (272.68 KiB) Viewed 239 times
COM485.fcfx
(44.33 KiB) Downloaded 11 times

Post Reply