Exemplo n.º 1
0
    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
        });
    }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sample : Getting printer serial number.
        /// </summary>
        public static CommunicationResult GetSerialNumber(ref string serialNumber, IPort port)
        {
            CommunicationResult result = CommunicationResult.ErrorUnknown;

            try
            {
                result = CommunicationResult.ErrorOpenPort;

                if (port == null)
                {
                    return(CommunicationResult.ErrorOpenPort);
                }

                result = CommunicationResult.ErrorWritePort;

                StarPrinterStatus printerStatus = port.GetParsedStatus();

                byte[] getInformationCommands = new byte[] { 0x1b, 0x1d, 0x29, 0x49, 0x01, 0x00, 49 }; // ESC GS ) I pL pH fn (Transmit printer information command)

                port.WritePort(getInformationCommands, 0, (uint)getInformationCommands.Length);

                result = CommunicationResult.ErrorReadPort;
                byte[] readBuffer       = new byte[1024];
                uint   totalReceiveSize = 0;
                string information      = "";

                uint startDate = (uint)Environment.TickCount;

                while (true)
                {
                    if ((UInt32)Environment.TickCount - startDate >= 3000) // Timeout
                    {
                        throw new PortException("ReadPort timeout.");
                    }

                    uint receiveSize = port.ReadPort(ref readBuffer, totalReceiveSize, (uint)(readBuffer.Length - totalReceiveSize));

                    if (receiveSize > 0)
                    {
                        totalReceiveSize += receiveSize;
                    }
                    else
                    {
                        continue;
                    }

                    byte[] receiveData = new byte[totalReceiveSize];

                    Array.Copy(readBuffer, 0, receiveData, 0, totalReceiveSize);

                    bool receiveResponse = false;

                    if (totalReceiveSize >= 2)
                    {
                        for (int i = 0; i < totalReceiveSize; i++)
                        {
                            if (receiveData[i] == 0x0a && // Check the footer of the command.
                                receiveData[i + 1] == 0x00)
                            {
                                for (int j = 0; j < totalReceiveSize - 9; j++)
                                {
                                    if (receiveData[j] == 0x1b &&
                                        receiveData[j + 1] == 0x1d &&
                                        receiveData[j + 2] == 0x29 &&
                                        receiveData[j + 3] == 0x49 &&
                                        receiveData[j + 4] == 0x01 &&
                                        receiveData[j + 5] == 0x00 &&
                                        receiveData[j + 6] == 49)
                                    {
                                        string responseStr = Encoding.ASCII.GetString(receiveData);

                                        int infoStartIndex = j + 7;                                                         // information start index.
                                        int infoEndIndex   = (int)(totalReceiveSize - 2);                                   // information end index.
                                        information = responseStr.Substring(infoStartIndex, infoEndIndex - infoStartIndex); // Extract information from priinter response.

                                        receiveResponse = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (receiveResponse)
                    {
                        break;
                    }
                }

                int serialNumberStartIndex = information.IndexOf("PrSrN="); // Check serial number tag.

                if (serialNumberStartIndex == -1)
                {
                    throw new PortException("Parse serial number failed.");
                }

                serialNumberStartIndex += "PrSrN=".Length;

                string temp = information.Substring(serialNumberStartIndex);

                int serialNumberEndIndex = temp.IndexOf(","); // Check comma.

                if (serialNumberEndIndex == -1)               // Not find comma.
                {
                    serialNumberEndIndex = temp.Length;       // End of information.
                }

                serialNumber = temp.Substring(0, serialNumberEndIndex); // Parse serial number information.

                int nullIndex = serialNumber.IndexOf("\0");             // Check null(for clone serial number).

                if (nullIndex != -1)                                    // Find null.
                {
                    serialNumber = serialNumber.Substring(0, nullIndex);
                }

                result = CommunicationResult.Success;
            }
            catch (PortException)
            {
            }

            return(result);
        }
        /// <summary>
        /// Sample : Getting printer information.
        /// </summary>
        public static CommunicationResult GetPrinterInformation(ref string receiveInfomation, string tag, 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[] getInformationCommands = new byte[] { 0x1b, 0x1d, 0x29, 0x49, 0x01, 0x00, 49 }; // ESC GS ) I pL pH fn (Transmit printer information command)

                port.WritePort(getInformationCommands, 0, (uint)getInformationCommands.Length);

                result = Result.ErrorReadPort;
                string      information    = "";
                byte[]      readBuffer     = new byte[1024];
                List <byte> allReceiveData = new List <byte>();

                uint startDate = (uint)Environment.TickCount;

                while (true)
                {
                    if ((UInt32)Environment.TickCount - startDate >= 3000) // Timeout
                    {
                        throw new PortException("ReadPort timeout.");
                    }

                    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);

                    bool receiveResponse = false;

                    int totalReceiveSize = allReceiveData.Count;

                    if (totalReceiveSize >= 2)
                    {
                        for (int i = 0; i < totalReceiveSize; i++)
                        {
                            if (allReceiveData[i] == 0x0a && // Check the footer of the command.
                                allReceiveData[i + 1] == 0x00)
                            {
                                for (int j = 0; j < totalReceiveSize - 9; j++)
                                {
                                    if (allReceiveData[j] == 0x1b &&
                                        allReceiveData[j + 1] == 0x1d &&
                                        allReceiveData[j + 2] == 0x29 &&
                                        allReceiveData[j + 3] == 0x49 &&
                                        allReceiveData[j + 4] == 0x01 &&
                                        allReceiveData[j + 5] == 0x00 &&
                                        allReceiveData[j + 6] == 49)
                                    {
                                        string responseStr = Encoding.ASCII.GetString(allReceiveData.ToArray());

                                        int infoStartIndex = j + 7;                                                         // information start index.
                                        int infoEndIndex   = totalReceiveSize - 2;                                          // information end index.
                                        information = responseStr.Substring(infoStartIndex, infoEndIndex - infoStartIndex); // Extract information from priinter response.

                                        receiveResponse = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    if (receiveResponse)
                    {
                        break;
                    }
                }

                int infomationStartIndex = information.IndexOf(tag); // Check tag.

                if (infomationStartIndex == -1)
                {
                    throw new PortException("Parse printer information failed.");
                }

                infomationStartIndex += tag.Length;

                string temp = information.Substring(infomationStartIndex);

                int informationEndIndex = temp.IndexOf(","); // Check comma.

                if (informationEndIndex == -1)               // Not find comma.
                {
                    informationEndIndex = temp.Length;       // End of information.
                }

                receiveInfomation = temp.Substring(0, informationEndIndex); // Parse serial number information.

                int nullIndex = receiveInfomation.IndexOf("\0");            // Check null(for clone serial number).

                if (nullIndex != -1)                                        // Find null.
                {
                    receiveInfomation = receiveInfomation.Substring(0, nullIndex);
                }

                result = Result.Success;
                code   = StarResultCode.Succeeded;
            }
            catch (PortException ex)
            {
                code = ex.ErrorCode;
            }

            return(new CommunicationResult()
            {
                Result = result,
                Code = code
            });
        }
Exemplo n.º 5
0
        /// <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);
        }