示例#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());
            }
        }