Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:14 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Reading Modbus Energy Meter.

Author Message
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 06:40am 07 Apr 2018
Copy link to clipboard 
Print this post

Thought I'd start a new thread, not really appropriate to tag it on the end of Modbus CRC.

Ok, So I now have this mess of workings and it is returning a result.

Just using a hard coded string to request a voltage reading from the meter on address 002.

Just not sure if I'm going around in circles converting data from one form to another un-necessarily.

Any feedback would be greatly appreciated.

  Quote   volts$ = CHR$(02)+CHR$(04)+CHR$(0)+CHR$(0)+CHR$(0)+CHR$(02)+CHR$(&h71)+CHR$(&hF8)
Dim String DataStr
print volts$

Open "Com1:9600,,RecComs,8" as #1
Do

print #1, volts$;

Do While Inkey$="": Loop
' Pause 1000

Loop

Sub RecComs

print "In Coms Sub"
Local Integer BufLen
'BufLen=LOC(#1)
print BufLen 'Local String DataStr
'If LOC(#1)>8 then 'If COM1 serial port buffer is NOT empty.... Was 100

Do While BufLen<> LOC(#1)
BufLen=
LOC(#1)
PAUSE 50
Loop
'End if


DataStr=
INPUT$(LOC(#1),#1) 'then suck everything in the buffer out and stick it in D$....

print "Response ";DataStr
print "Voltage "; Mid$(DataStr,4,4)
print "Length "; BufLen

Response$=
""

For n=1 to BufLen
'Print Hex$(Asc(Mid$(DataStr,n,1)),2)
Response$=Response$+Hex$(Asc(Mid$(DataStr,n,1)),2)
Next n

print Response$

IEEE754

End Sub

Sub IEEE754


ReadingStr$=
"&H"+Mid$(Response$, 7,8)
print ReadingStr$

Reading%=
Val(ReadingStr$)
Sign%=Reading% >>
31 'Read the Sign Bit
Expo%=((Reading% >> 23) And &hff) - 127 'Extract Exponent, Remove sign, Subtract Offset
Ans!=((Reading% Or &h800000) And &hffffff)/2^(23-Expo%)*(-1)^Sign% 'Extract Mantissa & Add implied 24th bit, Shift Point in Binary & apply sign

print "Text Input String: ";ReadingStr$
print "Hex Value Reading: ";Hex$(Reading%,8) 'Reading in Hex
print "Binary Value: ";Bin$(Reading%,32) 'Reading in Binary
print "Sign Bit: ";Hex$(Sign%)
print "Exponent & -offset: ";Bin$(Expo%+127,8), Str$(Expo%)
print "Mantissa Value: ";Bin$((Reading% Or &h800000) And &hffffff,32)
print
print "Returned Value: ";Str$(Ans!,10,1)
print


End Sub






Bit I have trouble getting my head around is that the "String" that comes in the com port is really a series of bytes & how I should treat them.

Phil.


 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 07:11am 07 Apr 2018
Copy link to clipboard 
Print this post

A 'string' is only a collection of bytes. In MMBasic, strings can contain any byte value from zero to 255. Internally MMBasic doesn't care.
The only difficulty is PRINTing the string that contains non-printable characters.
You have solved that by converting to HEX for viewing but that's only for your benefit, not the program.

Internally you can stick to strings for storage without any concern.

Jim
VK7JH
MMedit
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 08:58am 07 Apr 2018
Copy link to clipboard 
Print this post

  TassyJim said  You have solved that by converting to HEX for viewing but that's only for your benefit, not the program.


Rather Excited that I can now read Volts!

Next step is to read the whole damn meter.
(And the other two).


Phil.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4044
Posted: 10:02am 07 Apr 2018
Copy link to clipboard 
Print this post

Small point and would only go wrong rarely, but after you suck in the chars you could have more than BufLen.

Best to set BufLen = len(DataStr)
to be sure.

---

On a style point, using globals for Response$ and so on will hurt you sooner or later. Instead, pass as a param (to IEEE754 in this case).

Also, you may as well get the byte direct from DataStr rather then re-convert from hex.

It looks odd to use a $ with Response$ but Str instead with DataStr.

I'll leave off anything about explicit typing of variables...

John
 
Phil23
Guru

Joined: 27/03/2016
Location: Australia
Posts: 1667
Posted: 08:53pm 07 Apr 2018
Copy link to clipboard 
Print this post

All totally correct John.

What I posted above is at this stage raw workings with trial & error bits commented out.

Once I've got all the working parts doing their job I'll tidy them up into correctly written functions with better variable usage.

Phil.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025