From 698864041c7f9fbb9c7722defe52a54bfdb2d361 Mon Sep 17 00:00:00 2001 From: Matt Moening Date: Mon, 18 May 2015 18:51:06 -0700 Subject: [PATCH] X3G Driver is now in a separate plugin. --- MatterControl.csproj | 4 + MatterControl.sln | 31 ++++ .../PrinterConnectionAndCommunication.cs | 13 +- PrinterCommunication/X3GCrc.cs | 45 ----- PrinterCommunication/X3GReader.cs | 99 ----------- PrinterCommunication/X3GWriter.cs | 156 ------------------ 6 files changed, 44 insertions(+), 304 deletions(-) delete mode 100644 PrinterCommunication/X3GCrc.cs delete mode 100644 PrinterCommunication/X3GReader.cs delete mode 100644 PrinterCommunication/X3GWriter.cs diff --git a/MatterControl.csproj b/MatterControl.csproj index 1a83e6cda..d5814e81b 100644 --- a/MatterControl.csproj +++ b/MatterControl.csproj @@ -394,6 +394,10 @@ + + {26164cc3-29ad-4384-861c-181440934b00} + X3GDriver + {9B062971-A88E-4A3D-B3C9-12B78D15FA66} clipper_library diff --git a/MatterControl.sln b/MatterControl.sln index 2691a98ef..9d51aaccf 100644 --- a/MatterControl.sln +++ b/MatterControl.sln @@ -120,6 +120,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TextCreator", "TextCreator\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataConverters3D", "Submodules\agg-sharp\DataConverters3D\DataConverters3D.csproj", "{04667764-DC7B-4B95-AEF6-B4E6C87A54E9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "X3GDriver", "..\X3GDriver\X3GDriver\X3GDriver.csproj", "{26164CC3-29AD-4384-861C-181440934B00}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1070,6 +1072,34 @@ Global {04667764-DC7B-4B95-AEF6-B4E6C87A54E9}.Release64|x64.ActiveCfg = Release64|x64 {04667764-DC7B-4B95-AEF6-B4E6C87A54E9}.Release64|x64.Build.0 = Release64|x64 {04667764-DC7B-4B95-AEF6-B4E6C87A54E9}.Release64|x86.ActiveCfg = Release64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug|Any CPU.Build.0 = Debug|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug|x64.ActiveCfg = Debug|x64 + {26164CC3-29AD-4384-861C-181440934B00}.Debug|x64.Build.0 = Debug|x64 + {26164CC3-29AD-4384-861C-181440934B00}.Debug|x86.ActiveCfg = Debug|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug64|Any CPU.ActiveCfg = Debug64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug64|Any CPU.Build.0 = Debug64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug64|Mixed Platforms.ActiveCfg = Debug64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug64|Mixed Platforms.Build.0 = Debug64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Debug64|x64.ActiveCfg = Debug64|x64 + {26164CC3-29AD-4384-861C-181440934B00}.Debug64|x64.Build.0 = Debug64|x64 + {26164CC3-29AD-4384-861C-181440934B00}.Debug64|x86.ActiveCfg = Debug64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release|Any CPU.ActiveCfg = Release|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release|Any CPU.Build.0 = Release|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release|x64.ActiveCfg = Release|x64 + {26164CC3-29AD-4384-861C-181440934B00}.Release|x64.Build.0 = Release|x64 + {26164CC3-29AD-4384-861C-181440934B00}.Release|x86.ActiveCfg = Release|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release64|Any CPU.ActiveCfg = Release64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release64|Any CPU.Build.0 = Release64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release64|Mixed Platforms.ActiveCfg = Release64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release64|Mixed Platforms.Build.0 = Release64|Any CPU + {26164CC3-29AD-4384-861C-181440934B00}.Release64|x64.ActiveCfg = Release64|x64 + {26164CC3-29AD-4384-861C-181440934B00}.Release64|x64.Build.0 = Release64|x64 + {26164CC3-29AD-4384-861C-181440934B00}.Release64|x86.ActiveCfg = Release64|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -1106,6 +1136,7 @@ Global {002B1D12-4DBB-4ECF-B481-5EBECBC1666E} = {8F938919-F383-4BAE-BB95-13AB2A94088E} {F49EC1DD-D645-4709-8667-B57318AF67B0} = {32E5B656-D6DF-415E-9E7E-D03F1D8C3A8A} {04667764-DC7B-4B95-AEF6-B4E6C87A54E9} = {2AB9B589-5C98-4C05-BBEA-F97DAE168EAB} + {26164CC3-29AD-4384-861C-181440934B00} = {4E79BE4E-275E-4901-9173-E9096B7318F0} EndGlobalSection GlobalSection(MonoDevelopProperties) = preSolution StartupItem = MatterControl.csproj diff --git a/PrinterCommunication/PrinterConnectionAndCommunication.cs b/PrinterCommunication/PrinterConnectionAndCommunication.cs index 96cc20629..4f46a145c 100644 --- a/PrinterCommunication/PrinterConnectionAndCommunication.cs +++ b/PrinterCommunication/PrinterConnectionAndCommunication.cs @@ -39,6 +39,7 @@ using MatterHackers.MatterControl.SlicerConfiguration; using MatterHackers.SerialPortCommunication; using MatterHackers.SerialPortCommunication.FrostedSerial; using MatterHackers.VectorMath; +using MatterHackers.MatterControl.Plugins.X3GDriver; using Microsoft.Win32.SafeHandles; using System; using System.Collections.Generic; @@ -2825,12 +2826,16 @@ namespace MatterHackers.MatterControl.PrinterCommunication try { - timeSinceLastWrite.Restart(); - timeHaveBeenWaitingForOK.Restart(); using (TimedLock.Lock(this, "serialPort.Write")) { - byte[] x3gCommand = X3GWriter.translate(lineToWrite); - serialPort.Write(x3gCommand, 0, x3gCommand.Length); + bool sendToPrinter; + byte[] x3gCommand = X3GWriter.translate(lineToWrite, out sendToPrinter); + if (sendToPrinter) + { + serialPort.Write(x3gCommand, 0, x3gCommand.Length); + timeSinceLastWrite.Restart(); + timeHaveBeenWaitingForOK.Restart(); + } } //Debug.Write("w: " + lineToWrite); } diff --git a/PrinterCommunication/X3GCrc.cs b/PrinterCommunication/X3GCrc.cs deleted file mode 100644 index 364e35188..000000000 --- a/PrinterCommunication/X3GCrc.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MatterHackers.MatterControl.PrinterCommunication -{ - class X3GCrc - { - private uint crc; - - public X3GCrc() - { - crc = 0; - } - - public void update(byte command) - { - crc = (crc ^ command) & 0xff; - for (int i = 0; i < 8; i++) - { - if ((crc & 0x01) != 0) // C# is strictly typed so you must make a comparison - { - crc = ((crc >> 1) ^ 0x8c) & 0xff; - } - else - { - crc = (crc >> 1) & 0xff; - } - } - }//end generateNewCRC - - public byte getCrc() - { - return (byte)crc; - } - - public void clear() - { - crc = 0; - } - } - -} diff --git a/PrinterCommunication/X3GReader.cs b/PrinterCommunication/X3GReader.cs deleted file mode 100644 index 1f2fbed73..000000000 --- a/PrinterCommunication/X3GReader.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MatterHackers.MatterControl.PrinterCommunication -{ - /******************************************************** - * S3G Response Packet Structure: - * Index 0:StartBit - * Index 1:packet length - * Index 2+: Payload- - * PayLoad Index 0: Response Code(values 0x80 - 0x8C - * PayLoad Index 1+: Optional Response Arguments - * Index (2+N): crc - *******************************************************/ - - - class X3GReader - { - public static string translate(byte[] x3gResponse) - { - - X3GPacketAnalyzer analyzer = new X3GPacketAnalyzer(x3gResponse); - - - return analyzer.analyze(); - } - - private class X3GPacketAnalyzer - { - private byte[] response; - private X3GCrc crc; - - - public X3GPacketAnalyzer(byte[] x3gResponse) - { - response = x3gResponse; - crc = new X3GCrc(); - } - - public string analyze() - { - string gCodeResponse = ""; - int payloadLength; - - if (response[0] == 0xD5) - { - payloadLength = response[1]; - gCodeResponse += analyzePayload(payloadLength); - checkCrc(payloadLength + 2); - } - - gCodeResponse += "\r\n"; - - return gCodeResponse; - } - - private string analyzePayload(int payloadLength) - { - string payloadStr = ""; - for (int i = 2; i < payloadLength; i++) - { - if (i == 2) - { - switch(response[i]) - { - case 0x81: - payloadStr += "ok"; - break; - case 0x80: - case 0x83: - case 0x88: - case 0x89: - case 0x8C: - payloadStr += "rs"; - break; - } - - } - - //TODO - analyze payload - crc.update(response[i]); - } - - return payloadStr; - } - - private bool checkCrc(int crcIndex) - { - return crc.getCrc() == response[crcIndex]; - } - } - - - } - -} diff --git a/PrinterCommunication/X3GWriter.cs b/PrinterCommunication/X3GWriter.cs deleted file mode 100644 index 73da14a84..000000000 --- a/PrinterCommunication/X3GWriter.cs +++ /dev/null @@ -1,156 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace MatterHackers.MatterControl.PrinterCommunication -{ - class X3GWriter - { - //Axes bitfeilds - byte xAxis = 0x01; - byte yAxis = 0x02; - byte zAxis = 0x04; - byte xyAxes = 0x03; - byte aAxis; //Unknown functionality - byte bAxis; //Unknown functionality - - public static byte[] translate(string writemessage) - { - byte[] convertedMessage; - X3GPacketFactory binaryPacket; - char commandC = writemessage[0]; - - //Convert Connect message to X3G - switch(commandC) - { - case 'M': - - if (writemessage == "M115\r\n") - { - - binaryPacket = new X3GPacketFactory(0x00); - binaryPacket.add16bits(0x28); - //Convert M115 to 0x00 (host query call for X3G) - convertedMessage = binaryPacket.getX3GPacket(); - } - else if (writemessage == "M105\r\n") - { - binaryPacket = new X3GPacketFactory(0x0A); - binaryPacket.addByte(0x00); - binaryPacket.addByte(0x02); - convertedMessage = binaryPacket.getX3GPacket(); - } - else - { - convertedMessage = new byte[] { 0 }; - } - break; - - case 'G': //TODO - convertedMessage = new byte[] { 0 }; - break; - - case 'X': - writemessage = writemessage.Substring(1); - string[] commands = writemessage.Split(' '); - int arraySize = commands.Length; - - binaryPacket = new X3GPacketFactory(Byte.Parse(commands[0])); - - for (int i = 1; i < commands.Length; i++ ) - { - char c = commands[i][0]; - commands[i] = commands[i].Substring(1); - switch(c) - { - case 'b': - case 'B': - binaryPacket.addByte(Byte.Parse(commands[i])); - break; - case 'i': - case 'I': - binaryPacket.add16bits(int.Parse(commands[i])); - break; - case 'l': - case 'L': - binaryPacket.add32bits(long.Parse(commands[i])); - break; - } - - } - - convertedMessage = binaryPacket.getX3GPacket(); - break; - default: - convertedMessage = new byte[] { 0 }; - break; - } - - - - - return convertedMessage; - } - - - private class X3GPacketFactory - { - private const int MAX_PACKET_SIZE = 256; - - private byte[] packetOutline; - - private int index; - private X3GCrc crc; - - public X3GPacketFactory(byte printerCommand) - { - packetOutline = new byte[MAX_PACKET_SIZE]; - index = 2; - crc = new X3GCrc(); - addByte(printerCommand); - } - - public void addByte(int command) - { - packetOutline[index] = (byte)command; - index++; - crc.update((byte)command); - } - - public void add16bits(int command) - { - addByte((byte)(command & 0xff)); - addByte((byte)(command >> 8) & 0xff); - } - - public void add32bits(long command) - { - add16bits((int)command & 0xffff); - add16bits((int)(command >> 16) & 0xffff); - } - - public byte[] getX3GPacket() - { - byte packetLength = (byte)(index +1); - - byte[] packetArray = new byte[packetLength]; - packetArray[0] = 0xD5; - packetArray[1] = (byte)(packetLength - (byte)3); //Length does not count packet header or crc - - for (int i = 2; i < packetLength-1; i++ ) - { - packetArray[i] = packetOutline[i]; - } - - packetArray[packetLength - 1] = crc.getCrc(); - - return packetArray; - } - - } - - - } -}