Grazie signor maestro.le correzioni, adesso sono in ross
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.
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
- 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
- Code: Select all
Low_switches = Inp(&H010b)
High_switches = Inp(&H010a)
Switches_16 = High_switches * 256 : Switches_16 = Switches_16 + Low_switches