2560 e Modbus slave

Sezione dedicata al sistema di sviluppo BASCOM-AVR per i micro AVR
At90s, Attiny, Atmega e Xmega

Re: 2560 e Modbus slave

Postby js-bascom » 22 Apr 2014, 11:46

le correzioni, adesso sono in ross
Grazie signor maestro. :)

suibaf non so se questo ti aiuta on no. Ho postato il mio programma che sembra funzionare benissimo, ci sono un paio di cose orrende pero' non so' per il momento come migliorare le cose. Forse un aiuto verra proprio da qui altrimenti continuero' a scocciare Mark. :mrgreen:

Il problema che avevo io era che no risettavo (??) il serial buffer ogni volta che mandavo un pacchetto, quindi i dati erano sbagliati. Nota
Code: Select all
' Restart buffer
   Clear Serialin1
prima di mandare il pacchetto, dopo aspetto 30ms (il pacchetto di ritorno e solo 7 bytes quindi c'e' abbastanza tempo per riceverlo) dopo ricavo i dati che mi necessitano.


Code: Select all
'-----------------------------------------------------------------------------------------
'name                     : rs485-modbus-master.bas
'copyright                : (c) 1995-2008, MCS Electronics
'purpose                  : demo file for MAKEMODBUS
'micro                    : Mega324p
'suited for demo          : yes
'commercial addon needed  : no
'-----------------------------------------------------------------------------------------

$regfile = "m324pdef.dat"                                   ' specify the used micro

$crystal = 18432000                                         ' 18.432MHz

$baud = 19200                                               ' use baud rate

$hwstack = 42                                               ' default use 42 for the hardware stack
$swstack = 40                                               ' default use 40 for the SW stack
$framesize = 40                                             ' default use 40 for the frame space

$lib "modbus.lbx"                                           ' specify the additional library
'the libray will call a routine for UAR0,UART1,UAR2 and/or UAR3.
'when you get an error message that a label is not found with _SENDCHAR3 or _SENDCHAR4 then add these labels
'when you later use these routines you might get a duplicate label error and then you need to remove them

Config Print1 = Portc.6 , Mode = Set                        ' specify RS-485 and direction pin

Rs485dir Alias Portc.6                                      'make an alias
Config Rs485dir = Output                                    'set direction register to output
Rs485dir = 0                                                'set the pin to 0 for listening


' Output port is B
Ddrb = &HFF

'Ddrc = &HC0

Strobe Alias Portc.7
Config Strobe = Output                                      'set direction register to output
Strobe = 0

'configure the first UART for RS232
Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

'configure the second UAR for RS485/MODBUS. Make sure all slaves/servers use the same settings
Config Com2 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0


'use OPEN/CLOSE for using the second UART
Open "COM2:" For Binary As #1

'dimension some variables
Dim B As Byte

Dim Low_switches As Byte
Dim High_switches As Byte

Dim Switches_16 As Word
Dim W As Word

W = 0000

Enable Interrupts
Config Serialin1 = Buffered , Size = 7

Print "RS-485 MODBUS master"

Do
      Waitms 100                                            ' delay

' Restart buffer
   Clear Serialin1

   Print "Poll slave 2, 16 inputs > ";

   Print #1 , Makemodbus(2 , 3 , 40000 , 2);                ' slave 2, function 3, start address 2B, 4 words is 8 bytes

'Wait for Modbus packet to come in (7 bytes)
   Waitms 30

   Low_switches = Inp(&H010b)
   High_switches = Inp(&H010a)

   Switches_16 = High_switches * 256 : Switches_16 = Switches_16 + Low_switches

'Mimic status of low 8 switches from slave 2 locally
   Portb = Low_switches

'Latch data
   Strobe = 1
   Strobe = 0

' Print switches status
   Print Hex(high_switches) ; " High Sw.  " ; : Print Hex(low_switches) ; " Low Sw.  " ; : Print Hex(switches_16) ; " Word"

' Restart buffer
   Clear Serialin1

   Print #1 , Makemodbus(3 , 6 , 40000 , Switches_16);      ' slave 2, function 6, address 40000  , value of w
'Wait for Modbus packet to come in
   Waitms 30

' Process return packet if needed

' Restart buffer
   Clear Serialin1

   Print #1 , Makemodbus(3 , 6 , 40001 , W);                ' slave 2, function 6, address 40001  , value of w

'Wait for Modbus packet to come in
   Waitms 30

' Process return packet if needed

   W = W + 1

   Loop

End
Le cose orrende sono qui, non so come accedere i dati dal buffer1 quindi ho messo valori ricavati guardando dove il pacchetto e' locato usando Studio e il JTAG Mk2 in debug mode. Poi combino i due bytes in una word... a modo mio!! Se c'e' un modo piu' elegante (senzaltro) fatemelo sapere.
Code: Select all
   Low_switches = Inp(&H010b)
   High_switches = Inp(&H010a)

   Switches_16 = High_switches * 256 : Switches_16 = Switches_16 + Low_switches
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
User avatar
js-bascom
 
Posts: 176
Joined: 23 Mar 2013, 02:53
Location: Sydney, AUSTRALIA Ex Nunziata, Mascali (CT)

Re: 2560 e Modbus slave

Postby deluca » 22 Apr 2014, 13:08

@js-bascom,
Code: Select all
Switches_16 = High_switches * 256 : Switches_16 = Switches_16 + Low_switches

per fare questo esiste makeint:
Code: Select all
Switches_16 = Makeint(low_switches , High_switches)
Ciao
Il mio sito: http://www.delucagiovanni.com ......e la chat: chat/
User avatar
deluca
Site Admin
 
Posts: 1104
Joined: 19 Jun 2011, 10:44
Location: 95123 - Catania (Italy)

Re: 2560 e Modbus slave

Postby suibaf » 22 Apr 2014, 13:35

Grazie John,

a me funziona benino. Avevo il problema di cui parlavo prima. Poi per assurdo se allungo il tempo di chiamata tra un makemodbus ed un'altro non funziona bene nel senso che salta qualche scrittura! Con 2 simulatori.....il che mi sembra strano strano! Ma comunque, appena ho 2 minuti collego un plc e gli faccio fare lo slave e vediamo che succede!
User avatar
suibaf
 
Posts: 122
Joined: 08 Mar 2014, 09:55
Location: Lecce

Re: 2560 e Modbus slave

Postby js-bascom » 22 Apr 2014, 21:54

esiste makeint:
Grazie..sapevo che qualcosa ci doveva essere. :)

Dopo un po' di sonno e un po' di lettura mi sembra che la cosa meno orrenda per ricacare i dati del buffer sarebbe quello di usare una subroutine usando Ischarwaiting() e Waitkey() per copiare il contenuto (incluso i zeri in questo caso) in una stringa e poi usare MID per tirare fuori i dati necessarii, in questo caso forse no ci sara' bisogno di risettare il "circular buffer" prima di ogni pacchetto.

Be forse' questa sera faro' un altro po di indagini.
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
User avatar
js-bascom
 
Posts: 176
Joined: 23 Mar 2013, 02:53
Location: Sydney, AUSTRALIA Ex Nunziata, Mascali (CT)

Re: 2560 e Modbus slave

Postby js-bascom » 25 Apr 2014, 08:35

Per non contaminare questa thread ancora di piu' ho postato il codice sul forum Inglese
http://www.mcselec.com/index2.php?optio ... 4263#64263

Forse si protrebbe iniziare una nuova thread qui con commenti in Italiano se qualcuno la troverrebbe utile. :-)

E naturalmente ci sara' molto spazio per miglioramenti nel mio code perche' ho dovuto leggere molto e a volte non capire il modo migliore per fare le cose. Comunque FUNZIONA!! :mrgreen:
John Samperi
Ampertronics Pty. Ltd.
www.ampertronics.com.au
* Electronic Design * Custom Products * Contract Assembly
User avatar
js-bascom
 
Posts: 176
Joined: 23 Mar 2013, 02:53
Location: Sydney, AUSTRALIA Ex Nunziata, Mascali (CT)

Re: 2560 e Modbus slave

Postby suibaf » 13 Jun 2014, 22:32

Ecco il risultato del lavoro ottenuto grazie al Vs aiuto!

http://www.youtube.com/watch?v=wgpz7jo-1V8
User avatar
suibaf
 
Posts: 122
Joined: 08 Mar 2014, 09:55
Location: Lecce

Previous

Return to BASCOM-AVR

Who is online

Users browsing this forum: No registered users and 10 guests