View previous topic :: View next topic |
Author |
Message |
unzeitig
Joined: 31 Jan 2008 Posts: 25

|
Posted: Mon Jul 29, 2019 4:00 pm Post subject: MODBUS master on XMEGA |
|
|
Can somebody explain me why it does not work - it prints ABC only and stay running on Print #1 , Makemodbus(2 , 3 , B , 8); row.
When I try it with MEGA128 - it works properly.
[code]
$regfile = "xm128a3udef.dat"
$hwstack = 64
$swstack = 40
$framesize = 40
$lib "xmega.lib" : $external _xmegafix_clear : $external _xmegafix_rol_r1014
$lib "modbus.lbx"
'---------------------------------------------------------------
Const Crystalfrekq = 32000000
$crystal = Crystalfrekq
Config Osc = Disabled , 32mhzosc = Enabled , 32khzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1 'CPU Clock = 32MHz
Bitwait Osc_status.1 , Set 'Check if 32MHz Oscillator is ready
Bitwait Osc_status.2 , Set 'Check if internal 32.768 KHz Oscillator is ready
Osc_dfllctrl.0 = 0
Dfllrc32m_ctrl.0 = 1 'enable
Config Com1 = 9600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Open "COM1:" For Binary As #1
'dimension some variables
Dim B As Byte
B = &H26
Do
Print #1 , "ABC"
Print #1 , Makemodbus(2 , 3 , B , 8);
Loop
End
[/code]
[b][color=red](BASCOM-AVR version : 2.0.8.1 )[/b][/color] |
|
Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6197 Location: Holland

|
Posted: Tue Jul 30, 2019 11:17 am Post subject: |
|
|
it seems the xmega.lib and the modbus.lib got out of sync.
I attached a corrected version. _________________ Mark |
|
Back to top |
|
 |
unzeitig
Joined: 31 Jan 2008 Posts: 25

|
Posted: Tue Jul 30, 2019 11:38 am Post subject: |
|
|
Now it works, thank you for fast reply. |
|
Back to top |
|
 |
matjazs
Joined: 08 Nov 2016 Posts: 104
|
Posted: Fri Oct 04, 2024 4:36 pm Post subject: |
|
|
I would like to read the data from the energy meter - Energy meter TAC1101 Tariff
[url]admin: ads for ali are not allowed. instead attach the PDF[/url]
ModBUS is used for communication.
I'm using ATXMEGA32 ("xm32a4udef.dat") test board.
Attached is my Bascom example made from Samples file "rs485-modbus-master.bas" and the answer from the PC terminal.
I don't understand how makemodbus really works. I have read the documentation from e-meter and also bascom help,
but somehow I can't connect this information.
Maybe someone can help me and suggest what I should do to be able to read the data from the energy meter.
I would like to read the information:
Power
Voltage
Current
Power factor
Frequency
Wh out
Wh in
...
Code: | '-----------------------------------------------------------------------------------------
'name : rs485-modbus-master.bas
'copyright : (c) 1995-2020, MCS Electronics
'purpose : demo file for MAKEMODBUS
'micro : XMega32
'-----------------------------------------------------------------------------------------
'*******************************************************************************
'ModBUS test program
'ATXMEGA32
'TestBoard-MS
'2024-10
'*******************************************************************************
'Energy meter TAC1101 Tariff
'-From Dokumentation-----------------------
'Usage: TAC1100 [-a address] [-d] [-x] [-p] [-v] [-c] [-e] [-i] [-t] [-f] [-g] [-T] [[-m]|[-q]] [-b baud_rate] [-P parity] [-S bit] [-z num_retries] [-j seconds] [-w seconds] device
'TAC1100 [-a address] [-d] [-b baud_rate] [-P parity] [-S bit] -s new_address device
'TAC1100 [-a address] [-d] [-b baud_rate] [-P parity] [-S bit] -r baud_rate device
'TAC1100 [-a address] [-d] [-b baud_rate] [-P parity] [-S bit] -R new_time device
'where
'-a address Meter number (between 1 and 247). Default: 1
'-s new_address Set new meter number (between 1 and 247)
'-p Get power (W)
'-v Get voltage (V)
'-c Get current (A)
'-f Get frequency (Hz)
'-g Get power factor
'-e Get exported energy (Wh)
'-i Get imported energy (Wh)
'-t Get total energy (Wh)
'-T Get Time for rotating display values (0 = no rotation)
'-d Debug
'-x Trace (libmodbus debug on)
'-b baud_rate Use baud_rate serial port speed (1200, 2400, 4800, 9600) Default: 2400
'-P parity Use parity (E, N, O)
'-S bit Use stop bits (1, 2). Default: 1
'-r baud_rate Set baud_rate meter speed (1200, 2400, 4800, 9600)
'-R new_time Change rotation time for displaying values (0 - 30s) (0 = no totation)
'-m Output values in IEC 62056 format ID(VALUE*UNIT)
'-q Output values in compact mode
'-z num_retries Try to read max num_retries times on bus before exiting with error. Default: 1 (no retry)
'-j 1/10 secs Response timeout. Default: 2=0.2s
'-D 1/1000 secs Delay before sending commands (wait line set). Default: 30=0.03s
'-w seconds Time to wait to lock serial port. (1-30s) Default: 0s device Serial device, i.e. /dev/ttyUSB0
'*******************************************************************************
$regfile = "xm32a4udef.dat"
$crystal = 32000000
$hwstack = 50
$swstack = 50
$framesize = 50
$lib "modbus.lbx"
Config Print1 = Portb.3 , Mode = Set
Rs485dir Alias Portb.3 'make an alias
Config Rs485dir = Output 'set direction register to output
Rs485dir = 0 ' set the pin to 0 for listening
'Config Modbus = Default
'
'-Oscilator ----------------------------------------------------------------------
'
Config Osc = Enabled , Extosc = Enabled , Pllosc = Enabled , Range = 12mhz_16mhz , Startup = Xtal_16kclk , Pllsource = Extclock , Plldiv2 = Disabled , Pllmul = 2 , 32khzosc = Disabled
Config Sysclock = Pll , Prescalea = 1 , Prescalebc = 1_1
Config Priority = Static , Vector = Application , Lo = Enabled , Med = Enabled
'Config Priority = Static , Vector = Application , Lo = Enabled
'-------------------------------------------------------------------------------
'Config Submode = New
'-EEPROM XMEGA-----------------------------------------------------------------
'Config Eeprom = Mapped
'------------------------------------------------------------------------------
'-COM Ports ---------------------------------------------------------------------------------
'Serial Interface to PC
'COM----UART----TX pin--RX pin-
'COM1 - UART_C0 PORTC.3 PORTC.2 RS485- ModBUS Energy meter
'COM3 - UART_D0 PORTD.3 PORTD.2 PC-Terminal
'COM5 - UART_E0 PORTE.3 PORTE.2 WiFi ESP8266
Config Com1 = 9600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Config Com3 = 9600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
'Config Com5 = 57600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
'--Open all UARTS----------------------------------------------------------------------------------
Open "COM1:" For Binary As #1 'RS485- ModBUS Energy meter
Open "COM3:" For Binary As #3 'PC-Terminal
'Open "COM5:" For Binary As #5 'WiFi ESP8266
Config Serialin = Buffered , Size = 50
Config Serialin2 = Buffered , Size = 50
Enable Interrupts
'-----------------------------------------------------------------------------------
Print #3 , "*****Energy meter 2024-10*****"
Dim B As Byte
Dim W As Word
Dim L As Long
Dim Str_mbus As String * 20
W = &H4567 'assign a value
L = &H12345678 'assign a value
Main:
Do
Wait 2 ' delay
'Print "send request to slave/server"
' Send one of the following three messages
Print #1 , Makemodbus(1 , 3 , &H2B , 24); ' slave 2, function 3, start address 2B, 4 words is 8 bytes
W = &HDD :
'Print #1 , Makemodbus(1 , 3 , &H64 , W); ' slave 2, function 6, address 8 , value of w
'Print #1 , Makemodbus(2 , 16 , 8 , L); ' slave 2, function 16, address 8 , send a long
While Ischarwaiting() <> 0
B = Waitkey(#1)
Str_mbus = Str_mbus + Hex(b)
Wend
Print #3 , Str_mbus
Str_mbus = ""
'If Ischarwaiting(#1) <> 0 Then 'was something returned?
' B = Waitkey(#1) 'then get it
' Print#3 , Hex(b) ; ","; 'print the info
'End If
Loop
End |
Response from the terminal on the computer.
The data looks like random where some information is missing.
Quote: | *****Energy meter 2024-10*****
4025
4020
D009
0C0300
4025
0C2000
012B00
000D00
A025
002000
022B00
000300
012B00
000300
002B00
000300
4025
0C1003
102B00
002003
082B00
002003
... |
|
|
Back to top |
|
 |
matjazs
Joined: 08 Nov 2016 Posts: 104
|
Posted: Sun Oct 06, 2024 9:14 pm Post subject: |
|
|
I fixed some troubles on my hardware.
Now I connected TCA1101 directly to PC and terminal via USB/RS484 converter.
Reading datasheet of TCA1101 I send HEX value:
Quote: | Example: Read “Phase 1 line to neutral volts”
Request: 01 04 00 00 00 02 71 CB
Where, 01 = Meter address
04 = Function code
00 = High byte of registers starting address
00 = Low byte of registers starting address
00 = High byte of registers number
02 = Low byte of registers number
71 = CRC Low
CB = CRC High |
I got response:
Quote: | Response: 01 04 04 43 5C D8 49 B5 E4
Where, 01 = Meter address
04 = Function code
04= Byte count
43 = Data, (High Word, High Byte)
5C = Data, (High Word, Low Byte)
D8 = Data, (Low Word, High Byte)
49 = Data, (Low Word, Low Byte)
B5 = CRC Low
E4 = CRC High |
... which means, that Voltage is:
Quote: | 43 5C D8 49(Hex) = 220.845 (Floating point) |
What is exactly what I need.
Now I read serial output on my board - Sending information to TCA1101:
Code: | Print #1 , Makemodbus(&H01 , &H04 , &H00000002 , &H04); |
and on serial interface appear data:
Quote: | HEX 01 04 00 02 00 02 D0 0B |
.. here must be
Quote: | HEX 01 04 00 00 00 02 71 CB |
Why is Low byte of registers starting address &H02 instead &H00? |
|
Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6197 Location: Holland

|
Posted: Mon Oct 07, 2024 2:24 pm Post subject: |
|
|
you send &H00000002
that is not the same as the 0000 you sent with the terminal
 _________________ Mark |
|
Back to top |
|
 |
matjazs
Joined: 08 Nov 2016 Posts: 104
|
Posted: Mon Oct 07, 2024 2:47 pm Post subject: |
|
|
Quote: | you send &H00000002 |
Serial port sends &H 00 02 00 02 (&H 01 04 00 02 00 02 D0 0B)
I don't understand.
Does that mean I'm sending this in two parts:
&H0000 and &H0002 ?
How is this done with makemodbus?
Maybe:
Code: | Print #1, Makemodbus(&H01, &H04, &H0000, &H0002, &H04); |
|
|
Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6197 Location: Holland

|
Posted: Mon Oct 07, 2024 3:16 pm Post subject: |
|
|
you send too many parameters.
please check makemodbus in the help. only 4 are allowed. and you specify the start address.
i think that your device is not compatible with modbus. only the crc is. _________________ Mark |
|
Back to top |
|
 |
matjazs
Joined: 08 Nov 2016 Posts: 104
|
Posted: Mon Oct 07, 2024 7:19 pm Post subject: |
|
|
Mark. Thank you very much.
One question more:
On response data I get value
How to calculate a floating point value from a hexadecimal value in Bascom
In this case, Quote: | &H43618A75 is 225,541 |
I used this:
https://gregstoll.com/~gregstoll/floattohex/
Maybe some advice. |
|
Back to top |
|
 |
albertsm
Joined: 09 Apr 2004 Posts: 6197 Location: Holland

|
Posted: Mon Oct 07, 2024 7:52 pm Post subject: |
|
|
that has been asked before. it is simple :
Code: | Dim L As Long
Dim Sn As Single At L Overlay
L = &H43618A75
print sn
|
_________________ Mark |
|
Back to top |
|
 |
matjazs
Joined: 08 Nov 2016 Posts: 104
|
Posted: Mon Oct 07, 2024 9:26 pm Post subject: |
|
|
Thank you very much again.
It's really simple .
The best solutions are always simple. |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|