Files
cms-core-active/CMS_CORE_Library/Utils/Nc_Utils.cs
T
Lucio Maranta 75729a27cd WIP Fanuc RStoredData, read tools from fanuc memory
Added new magazine action to demo
2019-05-10 11:08:32 +00:00

303 lines
10 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using static CMS_CORE_Library.Models.DataStructures;
namespace CMS_CORE_Library.Utils
{
internal static class Nc_Utils
{
//-------------------------------------------------------------------------------------------------------------------------------------------------
// NC <-> .NET Conversions
// INT -> 2 WORD
internal static void IntTOWord(int IntVal, out ushort LowWord, out ushort HighWord)
{
byte[] bytes = BitConverter.GetBytes(IntVal);
//Split into 2 word
LowWord = BitConverter.ToUInt16(bytes, 0);
HighWord = BitConverter.ToUInt16(bytes, 2);
}
// 2 WORD -> INT
internal static void WordTOInt(out int IntVal, ushort LowWord, ushort HighWord)
{
byte[] bytes = new byte[4];
//fill the bytes from 2 word
bytes[0] = BitConverter.GetBytes(LowWord)[0];
bytes[1] = BitConverter.GetBytes(LowWord)[1];
bytes[2] = BitConverter.GetBytes(HighWord)[0];
bytes[3] = BitConverter.GetBytes(HighWord)[1];
//Create the value
IntVal = BitConverter.ToInt32(bytes, 0);
}
// INT -> 2 WORD (LIST)
internal static List<ushort> IntTOWordList(List<int> UintVal)
{
// Initialize the new List
List<ushort> Word = new List<ushort>();
// Fill the List
foreach (int val in UintVal)
{
IntTOWord(val, out ushort low, out ushort high);
Word.Add(low);
Word.Add(high);
}
return Word;
}
// 2 WORD -> INT (LIST)
internal static List<int> WordTOIntList(List<ushort> UintVal)
{
// Initialize the new List
List<int> Int = new List<int>();
// Fill the List
for (int i = 0; i < UintVal.Count; i = i + 2)
{
WordTOInt(out int value, UintVal[i], UintVal[i + 1]);
Int.Add(value);
}
return Int;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
// NC <-> .NET Conversions
// UINT -> 2 WORD
internal static void UIntTOWord(uint UintVal, out ushort LowWord, out ushort HighWord)
{
byte[] bytes = BitConverter.GetBytes(UintVal);
// Split into 2 word
LowWord = BitConverter.ToUInt16(bytes, 0);
HighWord = BitConverter.ToUInt16(bytes, 2);
}
// 2 WORD -> UINT
internal static void WordTOUint(out uint UintVal, ushort LowWord, ushort HighWord)
{
byte[] bytes = new byte[4];
// Fill the bytes from 2 word
bytes[0] = BitConverter.GetBytes(LowWord)[0];
bytes[1] = BitConverter.GetBytes(LowWord)[1];
bytes[2] = BitConverter.GetBytes(HighWord)[0];
bytes[3] = BitConverter.GetBytes(HighWord)[1];
// Create the value
UintVal = BitConverter.ToUInt32(bytes, 0);
}
// UINT -> 2 WORD (LIST)
internal static List<ushort> UIntTOWordList(List<uint> UintVal)
{
// Initialize the new List
List<ushort> Word = new List<ushort>();
// Fill the List
foreach (uint val in UintVal)
{
UIntTOWord(val, out ushort low, out ushort high);
Word.Add(low);
Word.Add(high);
}
return Word;
}
// 2 WORD -> UINT (LIST)
internal static List<uint> WordTOUintList(List<ushort> UintVal)
{
// Initialize the new List
List<uint> uInt = new List<uint>();
// Fill the List
for (int i = 0; i < (UintVal.Count); i = i + 2)
{
WordTOUint(out uint value, UintVal[i], UintVal[i + 1]);
uInt.Add(value);
}
return uInt;
}
internal static void WriteLocalFile(string fileContent, ref FileStream fileReference)
{
// Write Part Program
using (StreamWriter writer = new StreamWriter(fileReference))
writer.Write(fileContent);
}
internal static string ReadLocalFile(FileStream fileReference)
{
// Get File Content
StreamReader reader = new StreamReader(fileReference);
string fileContent = reader.ReadToEnd();
return fileContent;
}
// Get a substring from the beginning until the specified character -> defautl "["
internal static string GetStringUntilOrEmpty(string text, string stopAt = "[")
{
// Get string unti
if (!String.IsNullOrWhiteSpace(text))
{
// Find character index
int charLocation = text.IndexOf(stopAt, StringComparison.Ordinal);
if (charLocation > 0)
return text.Substring(0, charLocation);
}
return String.Empty;
}
// Convert one integer in array of bools
internal static bool[] IntToBits(int val)
{
return IntToBits(new int[] { val });
}
// Convert array of integers in an array of bools
internal static bool[] IntToBits(int[] val)
{
return new BitArray(val.ToArray())
.Cast<bool>()
.ToArray();
}
// Convert a byte into an array of bools
public static bool[] ByteToBits(byte val)
{
return new BitArray(new byte[] { val })
.Cast<bool>()
.ToArray();
}
// Convert a generic number into an array of byte
internal static byte[] NumberToByte(object val)
{
return BitConverter.GetBytes(Convert.ToInt32(val));
}
internal static List<byte> IntsToBytes(List<int> ints)
{
return ints.SelectMany(BitConverter.GetBytes).ToList();
}
internal static List<T> ListOfByteToListOf<T>(byte[] array, Func<byte[], int, T> bitConverter)
{
var size = Marshal.SizeOf(typeof(T));
return Enumerable.Range(0, array.Length / size)
.Select(i => bitConverter(array, i * size))
.ToList();
}
internal static SIEMENS_MAGAZINE_TYPE ConvertToSiemensMagazineType(NC_MAGAZINE_TYPE type)
{
switch (type)
{
case NC_MAGAZINE_TYPE.CHAIN: return SIEMENS_MAGAZINE_TYPE.CHAIN;
case NC_MAGAZINE_TYPE.BOX_MAGAZINE: return SIEMENS_MAGAZINE_TYPE.BOX_MAGAZINE;
case NC_MAGAZINE_TYPE.DISK: return SIEMENS_MAGAZINE_TYPE.REVOLVER;
}
return SIEMENS_MAGAZINE_TYPE.BOX_MAGAZINE;
}
internal static NC_MAGAZINE_TYPE ConvertToNcType(SIEMENS_MAGAZINE_TYPE type)
{
switch (type)
{
case SIEMENS_MAGAZINE_TYPE.CHAIN: return NC_MAGAZINE_TYPE.CHAIN;
case SIEMENS_MAGAZINE_TYPE.BOX_MAGAZINE: return NC_MAGAZINE_TYPE.BOX_MAGAZINE;
case SIEMENS_MAGAZINE_TYPE.REVOLVER: return NC_MAGAZINE_TYPE.DISK;
case SIEMENS_MAGAZINE_TYPE.MAGAZINE_LOADING_STATION: return NC_MAGAZINE_TYPE.MAGAZINE_LOADING_STATION;
case SIEMENS_MAGAZINE_TYPE.MAGAZINE_TOOL_BUFFER: return NC_MAGAZINE_TYPE.MAGAZINE_TOOL_BUFFER;
}
return NC_MAGAZINE_TYPE.BOX_MAGAZINE;
}
internal static string[] ExtractM154ParametersFromNcCodeLine(string codeLine)
{
string[] returnVal = new string[1];
int startPos = 0, endPos = 0;
// Get first position
startPos = codeLine.IndexOf('(');
if (startPos == -1)
startPos = codeLine.IndexOf(';');
// Get second position
endPos = codeLine.IndexOf(')', startPos + 1);
if (startPos != -1 && endPos == -1)
endPos = codeLine.Length;
// if not exists
if (startPos != -1)
{
string tmpString = codeLine;
if (endPos != -1)
tmpString = codeLine.Substring(startPos + 1 , endPos - startPos - 1);
returnVal = tmpString.Trim(';').Split(',');
}
return returnVal;
}
internal static string[] ExtractM155ParametersFromNcCodeLine(string codeLine, char startBrackets = '[', char endBrackets = ']')
{
string[] returnVal = new string[1];
int startPos = 0, endPos = 0;
// Get first position
startPos = codeLine.IndexOf(startBrackets);
if (startPos == -1)
startPos = codeLine.IndexOf(';');
// Get the final parameters index
endPos = codeLine.IndexOf(endBrackets, startPos + 1);
if (startPos != -1 && endPos == -1)
endPos = codeLine.Length;
// If parameters exist
if (startPos != -1)
{
string tmpString = codeLine;
if (endPos != -1)
tmpString = codeLine.Substring(startPos + 1, endPos - startPos - 1); // Remove command string ex: M155
returnVal = tmpString.Split(',');
}
return returnVal;
}
public static bool GetBitValue(int b, int bitNumber)
{
return (b & (1 << bitNumber)) != 0;
}
public static bool GetBitValue(byte b, int bitNumber)
{
return (b & (1 << bitNumber)) != 0;
}
}
}