Link to home
Start Free TrialLog in
Avatar of cmdolcet
cmdolcetFlag for United States of America

asked on

Shifting and Trunkating decimal places

I am having a problem when after I read my COM port I get a decimal shift of values from a result of .1515 to 1515.0. Not sure why it will display 1515.0 I see the correct value in the following code.

'Reads the Value inside the COM Port.
 strData = serialPort.ReadExisting
'Parse out any unwanted cahracters
 strData = strData.Replace(vbCrLf, vbTab).Replace(Chr(26), "").Replace(Chr(12), "").Replace(Chr(13), "")
                        'Parses out the information gathered from the COM port to a temp location
                        tempArray = Split(strData, ",")
                        'Parses out Wireless ID that is sent over when a Data Send is picked up on the COM port.
                        'This will Add the Wireless ID to all LMI 200, LMI 241, LMI TP
                        If tempArray.Length = 2 Then
                            WirelessGageID.Add(tempArray(1))
                            DSIValues.Add(tempArray(0))
                         End if
'This will place the value received in the DSIValue Array into the Reading box.
 CType(newGraphics.readingLabelArrayList(intloop), Label).Text = Format(muxClass.DSIValues(0) + gage.Offset, howToDiplayDecimalPlaces)
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi cmdolcet;

If you were to place a break point on this line in your code,
CType(newGraphics.readingLabelArrayList(intloop), Label).Text = Format(muxClass.DSIValues(0) + gage.Offset, howToDiplayDecimalPlaces)

Open in new window

What are the values of these variables at that time, muxClass.DSIValues(0), gage.Offset, and howToDiplayDecimalPlaces?
Avatar of cmdolcet

ASKER

It happens so infrequently that when I do place a breakpoint to step through the code it never happens. With the current code above can you see anything I may be doing wrong?
Then I would say to open a file with append, write the values to a file then flush and close the file. do this just before that line of code. Then when it crashes the last line in the file are the values just before the crash.
Im curious about how things can get shifted around.
That makes to of us.
The code I posted there should be no shifting correct. We did run a COM port sniffer and the values where coming over correctly.
And therefore must be changing in code and the reason to inspect the lines of code that could cause that.
OK I think the variable howtoDisplayDecimalPlaces is causing the issue right now it is setup like this "#####,0.0000"
Could this setup be causing the shift?
Hi cmdolcet;

Try modifying the following two lines of code in your program.
// Where you set up the format specifier for a numeric value
string howToDiplayDecimalPlaces = "###,##0.0000";

// Where you modify the text value of the Label control
CType(newGraphics.readingLabelArrayList(intloop), Label).Text = Format(CType(muxClass.DSIValues(0) + gage.Offset, Double), howToDiplayDecimalPlaces)

Open in new window

OK tried that code above and still got the same decimal shift result as before.
Well I can not think of anything other then a coding issue as long as the input coming from the comm port is correct. Is it possible to create a small project hard coding the data from the comm port and then using your current code to that will show the issue and upload that project to the web somewhere where I can download it, a web site like OneDrive?
ASKER CERTIFIED SOLUTION
Avatar of cmdolcet
cmdolcet
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If that will show the problem then that will do.
OK I tried narrow it down by comparing the original value in the COM port to what I should be expecting. What I think is happening is when I use variable "HowtoDisplayDecimalPlaces". The numbers get shifted.

CType(newGraphics.readingLabelArrayList(intloop), Label).Text = Format(muxClass.DSIValues(0) + gage.Offset, howToDiplayDecimalPlaces) "#####0.0000"

Is there any other way to display this? or another function
This solution worked!