コード例 #1
0
        /* Attempt to send data */
        private void SendData()
        {
            if (serialPort == null || !serialPort.IsOpen)
            {
                PutLog("请先连接串口后再发送数据!");
                return;
            }

            try
            {
                byte[] buffer;

                if (SendDataType == 0)
                {
                    buffer = Encoding.GetEncoding(setting.sendDataEncoding).GetBytes(SendDataText);
                    PutSendDataLog(SendDataType, SendDataText);
                }
                else if (SendDataType == 1)
                {
                    buffer = ByteConvert.HexStringToBytes(SendDataText);
                    PutSendDataLog(SendDataType, ByteConvert.BytesToHexString(buffer));
                }
                else
                {
                    buffer = ByteConvert.BinStringToBytes(SendDataText);
                    PutSendDataLog(SendDataType, ByteConvert.BytesToBinString(buffer));
                }

                if (buffer.Length == 0)
                {
                    PutLog("发送数据为空!");
                    return;
                }

                serialPort.Write(buffer, 0, buffer.Length);

                numberBytesSendInt += buffer.Length;

                NumberBytesSend = numberBytesSendInt.ToString();
            }
            catch (Exception e)
            {
                PutLog(e.ToString());
            }
        }
コード例 #2
0
        /* Displays data according to the display type set by the user */
        private void DisplayReceiveData()
        {
            if (receiveDisplayType >= 0 && receiveDisplayType <= 2)
            {
                byte[] buffer = receiveBuffer.ToArray();

                int offset = 0, count = buffer.Length;
                if (count > setting.maxShowByteCount)
                {
                    offset = count - setting.maxShowByteCount;
                    count  = setting.maxShowByteCount;
                }

                if (receiveDisplayType == 0)
                {
                    ReceiveData = ByteConvert.BytesToHexString(buffer, offset, count);
                }
                else if (receiveDisplayType == 1)
                {
                    ReceiveData = Encoding.GetEncoding(setting.receiveDataEncoding).GetString(buffer, offset, count);
                }
                else
                {
                    ReceiveData = ByteConvert.BytesToBinString(buffer, offset, count);
                }
            }
            else
            {
                int count = 0;

                ReceiveBuffer.Block tempBlock;
                Stack <string>      stack = new Stack <string>();

                for (int i = receiveBuffer.BlockCount - 1; i >= 0; --i)
                {
                    tempBlock = receiveBuffer.FindBlock(i);

                    int putCount = setting.maxShowByteCount - count;
                    if (putCount > tempBlock.Data.Length)
                    {
                        putCount = tempBlock.Data.Length;
                    }

                    if (putCount == 0)
                    {
                        break;
                    }

                    string head = tempBlock.Time.ToString(setting.receiveLogTimeFormat) + (setting.receiveLogLineFeedSplitsTimeAndContent ? '\n' : ':');

                    if (receiveDisplayType == 3)
                    {
                        stack.Push(head + ByteConvert.BytesToHexString(tempBlock.Data, tempBlock.Data.Length - putCount, putCount) + '\n');
                    }
                    else if (receiveDisplayType == 4)
                    {
                        stack.Push(head + Encoding.GetEncoding(setting.receiveDataEncoding).GetString(tempBlock.Data, tempBlock.Data.Length - putCount, putCount) + '\n');
                    }
                    else
                    {
                        stack.Push(head + ByteConvert.BytesToBinString(tempBlock.Data, tempBlock.Data.Length - putCount, putCount) + '\n');
                    }

                    count += tempBlock.Data.Length;
                    if (count > setting.maxShowByteCount)
                    {
                        break;
                    }
                }

                StringBuilder finalDisplay = new StringBuilder();
                while (stack.Count > 0)
                {
                    finalDisplay.Append(stack.Pop());
                }
                ReceiveData = finalDisplay.ToString();
            }
        }