示例#1
0
        public async Task <CurtainHelper.CurtainStateObject> SendMotorCommand(string data)
        {
            var cmd = CRCHelper.StringToByteArray(data.Replace(" ", ""));

            var str = BitConverter.ToString(cmd, 0, cmd.Length).Replace("-", " ");

            //_logger.LogInformation("SendCmd : " + str);
            using (var socket = SafeSocket.ConnectSocket(remoteEP))
            {
                await SendAsync(socket, cmd, 0, cmd.Length, 0).ConfigureAwait(false);

                var response = await ReceiveAsync(socket);

                if (_curtainHelper != null)
                {
                    return(await _curtainHelper.OnReceiveMotorData(response));
                }
                return(null);
            }
        }
示例#2
0
        public async Task <string> SendCommand(string data, bool log = false)
        {
            try
            {
                var cmd    = StringToByteArray(data.Replace(" ", ""));
                var cmdCRC = CRCHelper.get_CRC16_C(cmd);
                var cmd1   = new byte[cmd.Length + 2];
                cmd.CopyTo(cmd1, 0);
                cmdCRC.CopyTo(cmd1, cmd.Length);
                var str = CRCHelper.byteToHexStr(cmd1, cmd1.Length);
                if (log)
                {
                    _logger.LogInformation("SendCmd : " + str);
                }
                using (var socket = SafeSocket.ConnectSocket(remoteEP))
                {
                    var ret = await SendAsync(socket, cmd1, 0, cmd1.Length, 0).ConfigureAwait(false);

                    var response = await ReceiveAsync(socket);

                    if (log)
                    {
                        _logger.LogInformation("Cmd Receive : " + response);
                    }
                    if (_helper != null)
                    {
                        await _helper.OnReceiveCommand(response);
                    }
                    return(response);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("err data:" + data);
                _logger.LogError(ex.ToString());
                return(string.Empty);
            }
        }
示例#3
0
        public async Task SendCommand(string data, bool debug = false)
        {
            try
            {
                var cmd    = StringToByteArray(data.Replace(" ", ""));
                var cmdCRC = CRCHelper.Checksum(cmd);
                var cmd1   = new byte[cmd.Length + 1];
                cmd.CopyTo(cmd1, 0);
                cmd1[cmd.Length] = (byte)cmdCRC;
                var str = BitConverter.ToString(cmd1, 0, cmd1.Length).Replace("-", " ");
                if (debug)
                {
                    _logger.LogInformation("SendCmd : " + str);
                }
                using (var s = SafeSocket.ConnectSocket(remoteEP))
                {
                    var ret = await SendAsync(s, cmd1, 0, cmd1.Length, 0).ConfigureAwait(false);

                    var response = await ReceiveAsync(s);

                    if (debug)
                    {
                        _logger.LogInformation("Receive : " + response);
                    }

                    if (!string.IsNullOrWhiteSpace(response) && !string.IsNullOrEmpty(response))
                    {
                        await _helper.OnReceiveData(response);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("err data: " + data);
                _logger.LogError(ex.ToString());
            }
        }