Programmbeispiele
Beispiele für RS232 und RS485 (Pro II)
RS232:
Empfangen und senden
462
4.5 Beispiele für RS232 und RS485 (Pro II)
Die folgenden Beispiele sind vollständige Programme für das Senden und
Empfangen von Daten und Strings mit RS232 oder RS485.
Benötigt wird ein Modul Pro II-RSxxx mit Moduladresse 1.
Das Programm zeigt die Initialisierung der seriellen RS232-Schnittstelle im
Abschnitt
und das zyklische Lesen und Schreiben von Daten im
Init:
Abschnitt
. Der Prozess ist zeitgesteuert.
Event:
Rem The program initializes the serial interfaces in
Rem the INIT: section.
Rem In the EVENT: section data are exchanged between interfaces
Rem 1 & 2 of the RSxxx module.
Rem With this program both interfaces can be tested.
Rem To do so, connect the interfaces with each other before
Rem starting the program.
#Include
ADwinPro_All.Inc
#Define
num_data
1000
#Define
module
1
Dim
Data_1[num_data]
Dim
Data_2[num_data]
Dim
i
As Long
Init:
P2_RS_Reset(module)
For
i
=
1
To
num_data
Data_1[i] =
i
And
Next
i
Rem Initialize interfaces 1 and 2:
Rem 9600 Baud, no parity bit, 8 data bits, 2 stop bits,
Rem no handshake
P2_RS_Init(module, 1, 9600, 0, 8, 1, 0)
P2_RS_Init(module, 2, 9600, 0, 8, 1, 0)
Par_1
=
1
Par_4
=
1
Event:
Rem read and write a data set
If
(Par_1
<= num_data)
Par_2
= P2_Write_FIFO(module, 1, Data_1[Par_1])
If
(Par_2
= 0)
Then
EndIf
Par_3
= P2_Read_FIFO(module,
If
(Par_3
<> -1)
Then
Data_2[Par_4] =
Par_3
Inc
Par_4
EndIf
If
(Par_4
> num_data)
'number of send and receive data
'Module address
As
Long'send data
As
Long'receive data
'count variable
'initialize send data
0FFh
Then'send data
Inc
Par_1
2)'read data
Then End
'all data are transferred
ADwin-Pro II Software, Handbuch Apr. 2021
ADwin