Hi,
Hoping someone can help me out here and let me start off by saying I'm not very knowledgeable in C#. Due to limitations of our SCADA system I cannot pull back strings from our RTU so I would like to convert a 10 character ASCII string into 10 different bytes (Tags) in the HMI that I could then send to the RTU.
Thanks in advance!
Converting ASCII String to Bytes
Re: Converting ASCII String to Bytes
You should be able to convert it to a byte array using this code:
and then convert it back using this code:
Code: Select all
byte[] myBytes = Encoding.ASCII.GetBytes(myString);
Code: Select all
string myString= Encoding.ASCII.GetString(myBytes);
Best regards,
Russ
(801) 708-6690
Technical Support
Contact Us
Beijer Electronics AB
http://www.beijerelectronics.us
Russ
(801) 708-6690
Technical Support
Contact Us
Beijer Electronics AB
http://www.beijerelectronics.us
Re: Converting ASCII String to Bytes
So I have the following on a button right now, will change it to a second tag once I get it to work.
using System.Text;
public partial class Home
{
void Button_Click(System.Object sender, System.EventArgs e)
{
string input = Globals.Tags.String_ROC.Value;
byte[]array = Encoding.ASCII.GetBytes(input);
}
}
How do I assign Byte 1 of this array to a Globals.Tags.XX.Value that is a single UINT16 tag in the tag list? Then Byte 2, etc. I have the UINT16 tag already set up as a byte to my controller using the B:XX,XX,XX call out.
using System.Text;
public partial class Home
{
void Button_Click(System.Object sender, System.EventArgs e)
{
string input = Globals.Tags.String_ROC.Value;
byte[]array = Encoding.ASCII.GetBytes(input);
}
}
How do I assign Byte 1 of this array to a Globals.Tags.XX.Value that is a single UINT16 tag in the tag list? Then Byte 2, etc. I have the UINT16 tag already set up as a byte to my controller using the B:XX,XX,XX call out.
-
- Posts: 137
- Joined: Mon Jun 11, 2012 2:10 pm
Re: Converting ASCII String to Bytes
Assuming the string always has 10 characters you could use the following
Code: Select all
public partial class Home
{
void Button_Click(System.Object sender, System.EventArgs e)
{
string input = Globals.Tags.String_ROC.Value;
byte[] array = Encoding.ASCII.GetBytes(input);
Globals.Tags.Byte0.Value = array[0];
Globals.Tags.Byte1.Value = array[1];
Globals.Tags.Byte2.Value = array[2];
Globals.Tags.Byte3.Value = array[3];
Globals.Tags.Byte4.Value = array[4];
Globals.Tags.Byte5.Value = array[5];
Globals.Tags.Byte6.Value = array[6];
Globals.Tags.Byte7.Value = array[7];
Globals.Tags.Byte8.Value = array[8];
Globals.Tags.Byte9.Value = array[9];
}
}
Adam M.
Controls Engineer
FlexEnergy
Controls Engineer
FlexEnergy
Re: Converting ASCII String to Bytes
Tried incorporating this piece of code but am getting the errors that are attached. I then tried to create a set of internal tags with default data types which I could link to the external tags with the correct ones and got the same errors. Any idea's on corrections?AMitchneck wrote:Assuming the string always has 10 characters you could use the following
Code: Select all
public partial class Home { void Button_Click(System.Object sender, System.EventArgs e) { string input = Globals.Tags.String_ROC.Value; byte[] array = Encoding.ASCII.GetBytes(input); Globals.Tags.Byte0.Value = array[0]; Globals.Tags.Byte1.Value = array[1]; Globals.Tags.Byte2.Value = array[2]; Globals.Tags.Byte3.Value = array[3]; Globals.Tags.Byte4.Value = array[4]; Globals.Tags.Byte5.Value = array[5]; Globals.Tags.Byte6.Value = array[6]; Globals.Tags.Byte7.Value = array[7]; Globals.Tags.Byte8.Value = array[8]; Globals.Tags.Byte9.Value = array[9]; } }
- Attachments
-
- Script_Errors.PNG (111.2 KiB) Viewed 9072 times
-
- Posts: 137
- Joined: Mon Jun 11, 2012 2:10 pm
Re: Converting ASCII String to Bytes
It seems the tag type does not directly support byte values so it's trying to type cast it. The error indicates C# can't implicitly determine which type to cast it to. Try the following. This explicitly type casts the byte values to ushort.
Code: Select all
public partial class Home
{
void Button_Click(System.Object sender, System.EventArgs e)
{
string input = Globals.Tags.String_ROC.Value;
byte[] array = Encoding.ASCII.GetBytes(input);
Globals.Tags.Byte0.Value = (ushort)array[0];
Globals.Tags.Byte1.Value = (ushort)array[1];
Globals.Tags.Byte2.Value = (ushort)array[2];
Globals.Tags.Byte3.Value = (ushort)array[3];
Globals.Tags.Byte4.Value = (ushort)array[4];
Globals.Tags.Byte5.Value = (ushort)array[5];
Globals.Tags.Byte6.Value = (ushort)array[6];
Globals.Tags.Byte7.Value = (ushort)array[7];
Globals.Tags.Byte8.Value = (ushort)array[8];
Globals.Tags.Byte9.Value = (ushort)array[9];
}
}
Adam M.
Controls Engineer
FlexEnergy
Controls Engineer
FlexEnergy