Covert int to string

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
jydepower
Posts: 26
Joined: Thu Aug 16, 2012 9:33 am

Covert int to string

Post by jydepower »

Hallo.

Im using IX 2.0

Im new to C# so have a friend helping with the code.

I have an int tag comming from the plc which is showed in an anolognumeric box. What i want to do is convent the tag to a string if a specifig number is in the tag. Lets say int = 88 will be showen as string = fail1 or int = 99 as string = fail2. Anything else will be show normal

My friend made a convert function. But we have problems figuring out what aproach to take. Convert the analognumric box or the tag itself. What type should the tag or the box be original?
None of the code below works 100%. I end up with an emty box if the tag is either 88 or 99. I think its because the tag is int to begin with.

private string convert_number(string number)
{

string return_value = "";
switch (number)
{
case "88":
return_value = "fail1";
break;

case "99":
return_value = "fail2";
break;
}
return return_value;

}
// use this
void Screen1_Opened(System.Object sender, System.EventArgs e)
{
AnalogNumeric1.Text = convert_number(AnalogNumeric1.Text);
}
// or this
void Screen1_Opened(System.Object sender, System.EventArgs e)
{
Globals.Tags.Tag1.Value = convert_number(Globals.Tags.Tag1.Value);
}

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Covert int to string

Post by mark.monroe »

iX has a built in way to show text based on the value of a tag. Put a Text object on the screen and go to Configure Texts.

Use the start and end values to define what text to display when the tag is between those values. If the start and end value are the same, then the text will only be displayed when the tag equals that number.

You can also look at the Text Library feature in iX, which does a similar thing.
Snap 2012-10-31 at 11.27.47.png
Snap 2012-10-31 at 11.27.47.png (106.36 KiB) Viewed 17078 times
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

jydepower
Posts: 26
Joined: Thu Aug 16, 2012 9:33 am

Re: Covert int to string

Post by jydepower »

Thats nice.

Maybe i should have mentioned that im using the tag for logging also. So i need the variable to change.

Im thinking i should do it in the plc instead.

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Covert int to string

Post by mark.monroe »

You specify what type of value is being saved in a tag. You also specify what type of value an AnalogNumeric box is going to display. An AnalogNumeric set to display ints will display nothing if you send it a string.

You need to make sure that your AnalogNumeric box is set to display strings. Normally you would have the tag value set as an int32 and then use the case statement to convert the int to a string.

Code: Select all

private string convert_number(string number)
{

string return_value = "";
switch (number)
{
case 88:
return_value = "fail1";
break;

case 99:
return_value = "fail2";
break;
}
return return_value;

}
void Screen1_Opened(System.Object sender, System.EventArgs e)
{
//this.AnalogNumeric1.Text is set to display strings
//Globals.Tags.Tag1.Value is set as a int32
this.AnalogNumeric1.Text = convert_number(Globals.Tags.Tag1.Value);
}

Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

jydepower
Posts: 26
Joined: Thu Aug 16, 2012 9:33 am

Re: Covert int to string

Post by jydepower »

On the tag page i have two Data types one for plc variabel and one for panel tag.
If the plc variabel is INT16, can i then tell the tag type to be string, and it converts straight away?

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Covert int to string

Post by mark.monroe »

Yes. The best thing to do is try it and see if it works.
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

jydepower
Posts: 26
Joined: Thu Aug 16, 2012 9:33 am

Re: Covert int to string

Post by jydepower »

I get this error on the lines marker red.

Cannot implicitly convert type 'int' to 'string'

public partial class Maling
{
private string convert_number(string number)
{

string return_value = "";
switch (number)
{
case 88:
return_value = "fail1";
break;

case 99:
return_value = "fail2";
break;
}
return return_value;

}
void Maling_Opened(System.Object sender, System.EventArgs e)
{
//this.AnalogNumeric1.Text is set to display strings
//Globals.Tags.Tag1.Value is set as a int32

this.AnalogNumeric19.Text = convert_number(Globals.Tags.OldBoxNr1.Value);
}
}

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Covert int to string

Post by mark.monroe »

Put quotes around the case numbers.

case "88"
case "99"
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

jydepower
Posts: 26
Joined: Thu Aug 16, 2012 9:33 am

Re: Covert int to string

Post by jydepower »

Great that worked.

But gave me new problem.
Instead of changing the value of the AnalogNumeric, i need the tag value to change. Because i need the new tag value, for loggin even though i am not on the screen with the AnalogNumeric. Maybe its not possible since the PLC write to the tag all the time?


I tried this on the script page of the tag list. But made the run-time crash.

void TestString_ValueChange(System.Object sender,Neo.ApplicationFramework.Interfaces.Events.ValueChangedEventArgs e)
{
Globals.Tags.TestString.Value = convert_number(Globals.Tags.TestString.Value);
}

I also tried the ValueChanged event on the AnalogNumeric but i dident work. For test i tried a MouseDown, and that worked perfect.

mark.monroe
Posts: 824
Joined: Tue Mar 13, 2012 9:53 am

Re: Covert int to string

Post by mark.monroe »

You need to make sure that your tag value is set to the correct data type. If the value from the PLC is int, then you can not simply copy a string to that same tag. You need to make a new tag that is a string, and convert the tag from the PLC and save it into that string tag.

Pseudocode:

void TestString_ValueChange
{
Globals.Tags.MyStringTag.Value = Convert_To_String(Globals.Tags.FromPLCInt.Value)
}
Best Regards,
Mark Monroe

Beijer Electronics, Inc. | Applications Engineer

Post Reply