Skip to content

Commit a84aea1

Browse files
Merge pull request robincornelius#235 from Janez22/xdd
String data type updates
2 parents c9b0843 + 60e4e91 commit a84aea1

File tree

4 files changed

+52
-32
lines changed

4 files changed

+52
-32
lines changed

EDSEditorGUI/DeviceODView.Designer.cs

Lines changed: 17 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EDSEditorGUI/DeviceODView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ private void Button_saveChanges_Click(object sender, EventArgs e)
439439
od.LowLimit = textBox_lowLimit.Text;
440440

441441
// CO_stringLengthMin
442-
if (od.datatype == DataType.VISIBLE_STRING || od.datatype == DataType.OCTET_STRING)
442+
if (od.datatype == DataType.VISIBLE_STRING || od.datatype == DataType.UNICODE_STRING || od.datatype == DataType.OCTET_STRING)
443443
{
444444
try
445445
{

libEDSsharp/CanOpenNodeExporter_V4.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private void Prepare(EDSsharp eds)
197197
private int Prepare_var(ODentry od, string indexH, string varName, string group)
198198
{
199199
DataProperties data = Get_dataProperties(od.datatype, od.defaultvalue, od.prop.CO_stringLengthMin, indexH);
200-
string attr = Get_attributes(od, data.cTypeMultibyte);
200+
string attr = Get_attributes(od, data.cTypeMultibyte, data.cTypeString);
201201

202202
// data storage
203203
string dataPtr = "NULL";
@@ -253,7 +253,7 @@ private int Prepare_arr(ODentry od, string indexH, string varName, string group)
253253
DataType dataType = (sub.datatype != DataType.UNKNOWN) ? sub.datatype : od.datatype;
254254

255255
DataProperties data = Get_dataProperties(dataType, sub.defaultvalue, sub.prop.CO_stringLengthMin, indexH);
256-
string attr = Get_attributes(sub, data.cTypeMultibyte);
256+
string attr = Get_attributes(sub, data.cTypeMultibyte, data.cTypeString);
257257

258258
if (sub.Subindex != i)
259259
Warnings.AddWarning($"Error in 0x{indexH}: SubIndexes in ARRAY must be in sequence!", Warnings.warning_class.WARNING_BUILD);
@@ -350,7 +350,7 @@ private int Prepare_rec(ODentry od, string indexH, string varName, string group)
350350
foreach (ODentry sub in od.subobjects.Values)
351351
{
352352
DataProperties data = Get_dataProperties(sub.datatype, sub.defaultvalue, sub.prop.CO_stringLengthMin, indexH);
353-
string attr = Get_attributes(sub, data.cTypeMultibyte);
353+
string attr = Get_attributes(sub, data.cTypeMultibyte, data.cTypeString);
354354

355355
if (sub.Subindex == 0 && (data.cType != "uint8_t" || data.length != 1))
356356
Warnings.AddWarning($"Error in 0x{indexH}: Data type in RECORD, subIndex 0 must be UNSIGNED8, not {sub.datatype}!", Warnings.warning_class.WARNING_BUILD);
@@ -661,6 +661,7 @@ private class DataProperties
661661
public string cTypeArray = "";
662662
public string cTypeArray0 = "";
663663
public bool cTypeMultibyte = false;
664+
public bool cTypeString = false;
664665
public UInt32 length = 0;
665666
public string cValue = null;
666667
}
@@ -824,29 +825,39 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
824825
break;
825826

826827
case DataType.VISIBLE_STRING:
828+
data.cTypeString = true;
827829
if (valueDefined || stringLength > 0)
828830
{
829831
List<string> chars = new List<string>();
830832
UInt32 len = 0;
831833

832834
if (valueDefined)
833835
{
834-
Encoding ascii = Encoding.ASCII;
835-
Byte[] encodedBytes = ascii.GetBytes(defaultvalue);
836+
UTF8Encoding utf8 = new UTF8Encoding();
837+
Byte[] encodedBytes = utf8.GetBytes(defaultvalue);
836838
foreach (Byte b in encodedBytes)
837839
{
838-
chars.Add($"'{StringUnescape.Escape((char)b)}'");
840+
if ((char)b == '\'')
841+
chars.Add("'\\''");
842+
else if (b >= 0x20 && b < 0x7F)
843+
chars.Add($"'{(char)b}'");
844+
else
845+
chars.Add($"0x{b:X2}");
839846
len++;
840847
}
841848
}
849+
/* fill unused bytes with nulls */
842850
for (; len < stringLength; len++)
843851
{
844-
chars.Add("'\\0'");
852+
chars.Add("0");
845853
}
846854

855+
// extra string terminator
856+
chars.Add("0");
857+
847858
data.length = len;
848859
data.cType = "char";
849-
data.cTypeArray = $"[{len}]";
860+
data.cTypeArray = $"[{len + 1}]";
850861
data.cTypeArray0 = "[0]";
851862
data.cValue = $"{{{string.Join(", ", chars)}}}";
852863
}
@@ -866,7 +877,7 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
866877
string[] strBytes = defaultvalue.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
867878
foreach (string s in strBytes)
868879
{
869-
bytes.Add(String.Format("0x{0:X2}", Convert.ToByte(s, nobase)));
880+
bytes.Add(String.Format("0x{0:X2}", Convert.ToByte(s, 16)));
870881
len++;
871882
}
872883
}
@@ -883,6 +894,8 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
883894
}
884895
break;
885896
case DataType.UNICODE_STRING:
897+
data.cTypeString = true;
898+
data.cTypeMultibyte = true;
886899
if (valueDefined || stringLength > 0)
887900
{
888901
List<string> words = new List<string>();
@@ -904,9 +917,12 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
904917
words.Add("0x0000");
905918
}
906919

920+
// extra string terminator
921+
words.Add("0x0000");
922+
907923
data.length = len * 2;
908924
data.cType = "uint16_t";
909-
data.cTypeArray = $"[{len}]";
925+
data.cTypeArray = $"[{len + 1}]";
910926
data.cTypeArray0 = "[0]";
911927
data.cValue = $"{{{string.Join(", ", words)}}}";
912928
}
@@ -986,9 +1002,10 @@ private DataProperties Get_dataProperties(DataType dataType, string defaultvalue
9861002
/// Get attributes from OD entry or sub-entry
9871003
/// </summary>
9881004
/// <param name="od"></param>
989-
/// <param name="multibyte"></param>
1005+
/// <param name="cTypeMultibyte"></param>
1006+
/// <param name="cTypeString"></param>
9901007
/// <returns></returns>
991-
private string Get_attributes(ODentry od, bool multibyte)
1008+
private string Get_attributes(ODentry od, bool cTypeMultibyte, bool cTypeString)
9921009
{
9931010
List<string> attributes = new List<string>();
9941011

@@ -1031,9 +1048,12 @@ private string Get_attributes(ODentry od, bool multibyte)
10311048
break;
10321049
}
10331050

1034-
if (multibyte)
1051+
if (cTypeMultibyte)
10351052
attributes.Add("ODA_MB");
10361053

1054+
if (cTypeString)
1055+
attributes.Add("ODA_STR");
1056+
10371057
return string.Join(" | ", attributes);
10381058
}
10391059
#endregion

libEDSsharp/CanOpenXDD_1_1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ private ISO15745ProfileContainer Convert(EDSsharp eds, string fileName, bool dev
477477
devSubPar.Items = new object[] { new vendorTextLabel { lang = "en", Value = subod.parameter_name } };
478478
}
479479
if (!stripped)
480-
devSubPar.property = od.prop.SubOdeXdd();
480+
devSubPar.property = subod.prop.SubOdeXdd();
481481
if (deviceCommissioning && subod.denotation != null && subod.denotation != "")
482482
{
483483
devPar.denotation = new denotation

0 commit comments

Comments
 (0)