/// <summary> /// Sample : Parse printer response for getting scale displayed weight. /// </summary> public static Communication.CommunicationResult ParseDoNotCheckCondition(IPeripheralCommandParser parser, IPort port) { byte[] requestWeightToScaleCommand = parser.SendCommands; byte[] receiveWeightFromPrinterCommand = parser.ReceiveCommands; Communication.CommunicationResult result = Communication.CommunicationResult.ErrorUnknown; try { uint startDate = (uint)Environment.TickCount; byte[] readBuffer = new byte[1024]; uint totalReceiveSize = 0; int requestCount = 0; while (true) { if ((UInt32)Environment.TickCount - startDate >= 1000) // Timeout { throw new PortException("ReadPort timeout."); } result = Communication.CommunicationResult.ErrorWritePort; if ((UInt32)Environment.TickCount - startDate >= 250 * requestCount) // Send request displayed weight commands every 250ms. { port.WritePort(requestWeightToScaleCommand, 0, (uint)requestWeightToScaleCommand.Length); requestCount++; Thread.Sleep(100); } // Send read weight from printer commands. port.WritePort(receiveWeightFromPrinterCommand, 0, (uint)receiveWeightFromPrinterCommand.Length); Thread.Sleep(100); result = Communication.CommunicationResult.ErrorReadPort; uint receiveSize = port.ReadPort(ref readBuffer, totalReceiveSize, (uint)(readBuffer.Length - totalReceiveSize)); if (receiveSize > 0) { totalReceiveSize += receiveSize; } byte[] receiveData = new byte[totalReceiveSize]; Array.Copy(readBuffer, 0, receiveData, 0, totalReceiveSize); if (parser.Parse(receiveData, (int)totalReceiveSize) == ParseResult.Success) { result = Communication.CommunicationResult.Success; break; } } } catch (PortException) { } return(result); }
public static CommunicationResult ParseDoNotCheckCondition(IPeripheralCommandParser parser, IPort port) { Result result = Result.ErrorUnknown; int code; try { result = Result.ErrorOpenPort; if (port == null) { throw new PortException("port is null."); } result = Result.ErrorWritePort; StarPrinterStatus printerStatus = port.GetParsedStatus(); byte[] commands = parser.SendCommands; port.WritePort(commands, 0, (uint)commands.Length); result = Result.ErrorReadPort; byte[] readBuffer = new byte[1024]; List <byte> allReceiveData = new List <byte>(); uint startDate = (uint)Environment.TickCount; while (true) { if ((UInt32)Environment.TickCount - startDate >= 1000) // Timeout { throw new PortException("ReadPort timeout."); } Thread.Sleep(10); uint receiveSize = port.ReadPort(ref readBuffer, 0, (uint)readBuffer.Length); if (receiveSize == 0) { continue; } byte[] receiveData = new byte[receiveSize]; Array.Copy(readBuffer, 0, receiveData, 0, receiveSize); allReceiveData.AddRange(receiveData); if (parser.Parse(allReceiveData.ToArray(), allReceiveData.Count) == ParseResult.Success) { result = Result.Success; code = StarResultCode.Succeeded; break; } } } catch (PortException ex) { code = ex.ErrorCode; } return(new CommunicationResult() { Result = result, Code = code }); }
/// <summary> /// Sample : Parse printer response. /// </summary> public static CommunicationResult ParseDoNotCheckCondition(IPeripheralCommandParser parser, IPort port) { CommunicationResult result = CommunicationResult.ErrorUnknown; try { result = CommunicationResult.ErrorOpenPort; if (port == null) { return(CommunicationResult.ErrorOpenPort); } result = CommunicationResult.ErrorWritePort; StarPrinterStatus printerStatus = port.GetParsedStatus(); byte[] commands = parser.SendCommands; port.WritePort(commands, 0, (uint)commands.Length); result = CommunicationResult.ErrorReadPort; byte[] readBuffer = new byte[1024]; uint totalReceiveSize = 0; uint startDate = (uint)Environment.TickCount; while (true) { Thread.Sleep(10); uint receiveSize = port.ReadPort(ref readBuffer, totalReceiveSize, (uint)(readBuffer.Length - totalReceiveSize)); if (receiveSize > 0) { totalReceiveSize += receiveSize; } byte[] receiveData = new byte[totalReceiveSize]; Array.Copy(readBuffer, 0, receiveData, 0, totalReceiveSize); if (parser.Parse(receiveData, (int)totalReceiveSize) == ParseResult.Success) { result = CommunicationResult.Success; break; } if ((UInt32)Environment.TickCount - startDate >= 1000) // Timeout { throw new PortException("ReadPort timeout."); } } } catch (PortException) { } return(result); }