I am reading status data from an instrument via the RS-232 serial port. The instrument sends out 39 packets. Most of the packets contain information in decimal form, so these can be printed right away by Liberty. Some of the packets contain information in binary form. By using the ASC function the information in this packet is converted to an ASCI number and the decimal value of that ASCI number is exactly the expected value, so although I do not really understand yet how that works, the conversion from binary to decimal looks completely ok; they are the correct values.
But some packets contain information in BCD form. By using a serial portmonitor (RealTerm) I can see the data in this packet, but all serial portmonitors I tried are unable to show BCD code. The goal is the convert BCD to decimal within Liberty. Can any one give a hint how to do this ? I think the major problem is that the packets in the serial stream are already in BCD, but of course Liberty does not know this and thinks it is ASCI. (I have no idea why one would build a stream with so many different codings, but that is the way it is)
Last Edit: Oct 24, 2020 14:46:30 GMT -5 by johnking: type mistake
Post by tenochtitlanuk on Oct 24, 2020 16:25:45 GMT -5
If you can receive the byte-stream and save to a file, and post it here for us, we could probably help. Or just the sections you can't at present understand/decode. But there are a whole range of BCD codings- it would help if you know what the binary-coded parts should look like.
eg if you know it is supposed to hold four decimal digits but only two bytes are received, then it has to be 4-bits per digit. Or if the received bytes are between zero and 9 and the upper bits are always zero, it is obviously one decimal value per byte) Once you know the digits you simply add them each multiplied by 1, 10, 100, etc.
I recommend using a hex editor to study the file of the transmitted data stream.
' Here are three ways to save number data... a =123 ' example datum value =123
' if an integer in range 0 to 255... out1byte$ =chr$( 123)
open "sample1byte.fil" for output as #fOut #fOut, out1byte$; close #fOut
open "sample1byte.fil" for input as #fIn i$ =input$( #fIn, 1) close #fIn print "Stored as single byte",, asc( i$)
' if to store in ASCII, or BCD with one byte per digit.. a$ =str$( a)
for i =1 to 3 outASCII$ =outASCII$ +mid$( a$, i, 1) outBCD$ =outBCD$ +chr$( val( mid$( a$, i, 1))) next i
open "sampleASCII.fil" for output as #fOut #fOut, outASCII$; close #fOut
open "sampleASCII.fil" for input as #fIn i$ =input$( #fIn, lof( #fIn)) close #fIn print "Stored as ASCII chars",, i$
open "sampleBCD.fil" for output as #fOut #fOut, outBCD$; close #fOut
open "sampleBCD.fil" for input as #fIn i$ =input$( #fIn, lof( #fIn)) close #fIn
print "Stored as BCD one digit per byte", for j =1 to 3 m$ =mid$( i$, j, 1) v =asc( m$) o$ =o$ +chr$( 48 +v) 'print m$; next j print "" print " . . which is a string with number value "; val( o$)
Thanks, that pointed me in the right direction and after making some conversions by hand in excel and with your code I found that simply using
converted the value on position 27 in the string dataread$ to the correct decimal number, as it did for the values on position 28..32. In this case it is year-month-day and hour-minute-seconds. I still do not understand exactly why the conversions are ok because it is not what the protocol describes, but anyway: it is correct now. It could be that the protocol description is not 100% up-to-date on this as I found two other packets were not in the stream although they should be there according to the protocol.