반응형

2022.01.04 - [C#] - C# Wireless Network Adapter Disable/Enable - 무선 네트워크 어댑터 활성화/비활성화 1

의 소스를 조금 더 심플하게 수정해 보자.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
using System.Management;
 
 
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ListAllNetworkAdapters();
        }
 
        private static void ListAllNetworkAdapters()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2""SELECT * FROM Win32_NetworkAdapter");
 
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    if (queryObj["Name"].ToString().Contains("Wireless"))
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_NetworkAdapter Instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Name: {0}", queryObj["Name"]);
                        Console.WriteLine("NetEnabled: {0}", queryObj["NetEnabled"]);
 
                        if ((bool)queryObj["NetEnabled"])
                        {
                            queryObj.InvokeMethod("Disable"null); // 관리자 권한 실행 필요
                            Console.WriteLine("Disabled.");
                        }
                        else
                        {
                            queryObj.InvokeMethod("Enable"null); // 관리자 권한 실행 필요
                            Console.WriteLine("Enabled.");
                        }
 
                        Console.WriteLine("-----------------------------------");
                    }
                }
            }
            catch (ManagementException e)
            {
                Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}
 

 

소스를 입력하고 빌드한다.

 

활성화 되어 있던 와이파이가 표시되고 비활성화 되었다.

 

'사용 안 함' 상태에서 프로그램을 실행하면 '사용함' 상태로 바뀐다.

※ 이 프로그램은 관리자 권한으로 실행해야 한다.

 

반응형
Posted by J-sean
:
반응형

아두이노로 간단히 RF433MHz 통신이 가능하다.


왼쪽이 리시버, 오른쪽이 트랜스미터다.


송신기와 수신기를 아두이노에 연결해 준다. RadioHead Packet Radio library를 다운받고 아래와 같이 아두이노 라이브러리에 복사한다.


특별한 설치 과정은 없다. 그냥 복사한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module
// Tested on Arduino Mega, Duemilanova, Uno, Due, Teensy, ESP-12
 
#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif
 
RH_ASK driver;
// RH_ASK driver(2000, 4, 5, 0); // ESP8266 or ESP32: do not use pin 11 or 2
// RH_ASK driver(2000, 3, 4, 0); // ATTiny, RX on D3 (pin 2 on attiny85) TX on D4 (pin 3 on attiny85), 
// RH_ASK driver(2000, PD14, PD13, 0); STM32F4 Discovery: see tx and rx on Orange and Red LEDS
 
void setup()
{
  #ifdef RH_HAVE_SERIAL
    Serial.begin(9600);    // Debugging only
  #endif
    if (!driver.init())
  #ifdef RH_HAVE_SERIAL
      Serial.println("init failed");
  #else
      ;
  #endif
    else
      Serial.println("init succeeded");
}
 
void loop()
{
  const char *msg = "Hello Wireless World!";
 
  driver.send((uint8_t *)msg, strlen(msg));
  driver.waitPacketSent();
  #ifdef RH_HAVE_SERIAL
      Serial.println("Message sent.");
  #endif
  
  delay(200);
}


Transmitter 소스를 컴파일하고 아두이노에 업로드한다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// ask_receiver.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to receive messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) receiver with an Rx-B1 module
// Tested on Arduino Mega, Duemilanova, Uno, Due, Teensy, ESP-12
 
#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h> // Not actually used but needed to compile
#endif
 
RH_ASK driver;
// RH_ASK driver(2000, 4, 5, 0); // ESP8266 or ESP32: do not use pin 11 or 2
// RH_ASK driver(2000, 3, 4, 0); // ATTiny, RX on D3 (pin 2 on attiny85) TX on D4 (pin 3 on attiny85), 
// RH_ASK driver(2000, PD14, PD13, 0); STM32F4 Discovery: see tx and rx on Orange and Red LEDS
 
void setup()
{
  #ifdef RH_HAVE_SERIAL
    Serial.begin(9600);    // Debugging only
  #endif
    if (!driver.init())
      
  #ifdef RH_HAVE_SERIAL
      Serial.println("init failed");
  #else
      ;
  #endif
    else
      Serial.println("init succeeded");
}
 
void loop()
{
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  memset(buf, 0, RH_ASK_MAX_MESSAGE_LEN);
  uint8_t buflen = sizeof(buf);
 
  if (driver.recv(buf, &buflen)) // Non-blocking
  {
    // Message with a good checksum received, dump it.
    driver.printBuffer("Got:", buf, buflen);
    #ifdef RH_HAVE_SERIAL
      String message = (char*)buf;
      Serial.print("Message: ");
      Serial.println(message);
    #endif
  }
 
  delay(200);
}


Receiver 소스를 컴파일하고 아두이노에 업로드한다.


송신기에서는 계속 메세지를 보낸다.


송신기에서 보낸 메세지를 수신기에서 받는다. printBuffer()로 출력한 메세지는 각 문자의 아스키 코드값이다.


void RHGenericDriver::printBuffer(const char* prompt, const uint8_t* buf, uint8_t len)

Parameters

[in] prompt string to preface the print

[in] buf Location of the buffer to print

[in] len Length of the buffer in octets.


Prints a data buffer in HEX. For diagnostic use


※ 참고: RH_ASK Class Reference


반응형
Posted by J-sean
:
반응형

nRF24L01 모듈은 Serial Peripheral Interface(SPI) 프로토콜을 사용하여 2.4GHz 무선 통신이 가능하다.


중국에서 구매한 nRF24L01 모듈 2개.


8개의 핀이 있다.


nRF24L01의 핀맵.


아두이노의 핀맵. nRF24L01과 연결할 핀을 확인 한다.



이렇게 연결 한다.


[nRF24L01 - Arduino]

VCC - 3.3V

GND - GND

CSN - 8

CE - 7

MOSI - 11

SCK - 13

IRQ - N/A

MISO - 12


통신을 해야 하므로 2개 모두 연결해 준다.


수신 프로그램을 컴파일 하고 아두이노에 업로드 한다.


송신 프로그램을 컴파일 하고 다른 아두이노에 업로드 한다.



수신 프로그램이 업로드된 아두이노의 시리얼 모니터를 확인해 보자.

통신이 잘 안된다면 10μF 커패시터를 연결 하고 3.3V 외부 전원을 사용 하자.


GND is the Ground Pin. It is usually marked by encasing the pin in a square so it can be used as a reference for identifying the other pins.


VCC supplies power for the module. This can be anywhere from 1.9 to 3.9 volts. You can connect it to 3.3V output from your Arduino. Remember connecting it to 5V pin will likely destroy your nRF24L01+ module!


CE (Chip Enable) is an active-HIGH pin. When selected the nRF24L01 will either transmit or receive, depending upon which mode it is currently in.


CSN (Chip Select Not) is an active-LOW pin and is normally kept HIGH. When this pin goes low, the nRF24L01 begins listening on its SPI port for data and processes it accordingly.


SCK (Serial Clock) accepts clock pulses provided by the SPI bus Master.


MOSI (Master Out Slave In) is SPI input to the nRF24L01.


MISO (Master In Slave Out) is SPI output from the nRF24L01.


IRQ is an interrupt pin that can alert the master when new data is available to process.


반응형
Posted by J-sean
: