Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 4개 있습니다.)

Raspberry PI Zero (W)에 FTDI 장치 연결 후 C/C++로 DTR 제어

지난 글에서 살펴본 FTDI 장치를,

PC에 연결해 동작하는 자신만의 USB 장치 만들어 보기
; https://www.sysnet.pe.kr/2/0/11606

제가 가지고 있는 "라즈베리 파이 Zero W" 모델에 사용해 봤습니다. 그냥 마이크로 USB 단자에 연결 및 해제 시 dmesg 로그를 보면 다음과 같은 메시지를 확인할 수 있습니다.

[처음 연결했을 때]
Indeed it is in host mode hprt0 = 00021501
usb 1-1: new full-speed USB device number 2 using dwc_otg
Indeed it is in host mode hprt0 = 00021501
usb 1-1: New USB device found, idVendor=0403, idProduct=6001
usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1: Product: FT232R USB UART
usb 1-1: Manufacturer: FTDI
usb 1-1: SerialNumber: A901J4AU
NOHZ: local_softirq_pending 40
usbcore: registered new interface driver usbserial
usbcore: registered new interface driver usbserial_generic
usbserial: USB Serial support registered for generic
usbcore: registered new interface driver ftdi_sio
usbserial: USB Serial support registered for FTDI USB Serial Device
ftdi_sio 1-1:1.0: FTDI USB Serial Device converter detected
usb 1-1: Detected FT232RL
usb 1-1: FTDI USB Serial Device converter now attached to ttyUSB0

[연결 해제 시]
usb 1-1: USB disconnect, device number 2
ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0
ftdi_sio 1-1:1.0: device disconnected

[두 번째 연결 시]
Indeed it is in host mode hprt0 = 00021501
usb 1-1: new full-speed USB device number 3 using dwc_otg
Indeed it is in host mode hprt0 = 00021501
usb 1-1: New USB device found, idVendor=0403, idProduct=6001
usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 1-1: Product: FT232R USB UART
usb 1-1: Manufacturer: FTDI
usb 1-1: SerialNumber: A901J4AU
ftdi_sio 1-1:1.0: FTDI USB Serial Device converter detected
usb 1-1: Detected FT232RL
usb 1-1: FTDI USB Serial Device converter now attached to ttyUSB0

위의 메시지에 나온 데로 tty 장치에는 다음과 같이 ttyUSB0가 열거됩니다.

$ ls /dev/ttyUSB*
/dev/ttyUSB0




자, 그럼 "PC에 연결해 동작하는 자신만의 USB 장치 만들어 보기" 글에서 했던 대로 DTR 신호를 활성화시키는 C/C++ 프로그램을 만들어 보겠습니다. 아쉽지만 Raspberry PI Zero에서는 .NET Core가 지원이 안되므로 C/C++로 만들어야 합니다.

그렇다고는 해도 Visual Studio의 리눅스 지원이 워낙 훌륭해서,

Visual Studio 2017에서 Raspberry Pi C++ 응용 프로그램 제작
; https://www.sysnet.pe.kr/2/0/11358

윈도우에서도 쉽게 개발할 수 있습니다. 게다가 저 글을 쓸 때와는 또 다르게 자동으로 include 관련 파일들도 싱크를 맞춰주기 때문에,

IntelliSense for Remote Linux Headers
; https://blogs.msdn.microsoft.com/vcblog/2018/04/09/intellisense-for-remote-linux-headers/

인텔리센스 환경까지 제공합니다. 어쨌든 간단하게 다음과 같이 만들고,

#include <stdio.h>
#include <string>
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>

using namespace std;

int main(int argc, char *argv[])
{
    // http://xanthium.in/Controlling-RTS-and-DTR-pins-SerialPort-in-Linux

    string portNumber = "0";
    string portName = "/dev/ttyUSB";

    if (argc < 2)
    {
        cout << "FtdiApp [portNumber]" << endl;
        cout << "ex - set DTR on /dev/ttyUSB0" << endl;
        cout << "\t" << "FtdiApp 0" << endl;
        return 1;
    }

    portNumber = argv[1];
    portName.append(portNumber);

    int fd = open(portName.c_str(), O_RDWR | O_NOCTTY);
    if (fd == -1)
    {
        cout << "NO Port: " << portName << endl;
        return 1;
    }

    int dtrFlag = TIOCM_DTR;
    ioctl(fd, TIOCMBIS, &dtrFlag);

    cout << "Press any key to clear DTR again." << endl;
    
    char notUsed[80];
    fgets(notUsed, sizeof(notUsed), stdin);

    ioctl(fd, TIOCMBIC, &dtrFlag);
    close(fd);

    return 0;
}

빌드하면 Raspberry PI Zero에 배포가 됩니다. 실습까지 해봐야겠죠? ^^ 이를 위해 FTDI Basic 모듈을 USB 케이블로 Raspberry pi zero와 연결하고 FTDI Basic 모듈과 LED를 다음과 같이 연결해 줍니다.

ftdi_raspberrypi_1.png

ftdi_raspberrypi_2.png

그런 다음, 라즈베리 파이의 연결 콘솔에서 다음과 같이 명령을 내리면 됩니다.

$ ./FtdiApp 0

실행 후 다음번 엔터 키를 누를 때까지 LED의 불이 들어오고 있는 것을 확인할 수 있습니다.

(첨부 파일은 예제 C++ 프로젝트와 회로도를 포함합니다.)




참고로, 아직 Visual Studio 2017의 인텔리센스를 위한 리눅스 헤더 파일 지원이 불안정한 것 같습니다. 프로젝트를 다시 로드했더니 인텔리센스 기능이 모두 풀렸는데요, 그래서 다시 "Tools" / "Options"의 "Cross Platform" / "Connection Manager" / "Remote Headers IntelliSense Manager"를 통해 업데이트했는데도,

ftdi_raspberrypi_3.png

(위의 화면에서 "Explore" 버튼을 누르면 열리는) 헤더 파일에 대한 로컬 캐시가 그냥 비어 있는 체로 남아 있었습니다. 할 수 없습니다, 이런 경우에는 그냥 수작업으로 헤더 파일을 모두 복사해야 합니다. 방법은 다음의 글에도 나오지만,

Visual C++ for Linux Development
; https://blogs.msdn.microsoft.com/vcblog/2016/03/30/visual-c-for-linux-development/

pscp를 이용해,

Download PuTTY: latest release 
; http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

다음과 같은 식으로 헤더 파일을 로컬 캐시에 복사하면 됩니다. (또는 네트워크 공유 폴더를 접근해 복사해도 상관없습니다.)

pscp -r [pi_user_name]@[pi_address]:/usr/include .

가령 다음과 같은 환경인 경우,

로컬 캐시 경로: %LOCALAPPDATA%\Microsoft\Linux\HeaderCache\1.0\-1370237495\usr
PI 사용자 계정: mypi
PI 주소: 192.168.100.5

다음과 같이 입력해 주면 됩니다.

C:\temp> cd %LOCALAPPDATA%\Microsoft\Linux\HeaderCache\1.0\-1370237495\usr

C:\Users\...[생략]...\-1370237495\usr> pscp -r mypi@192.168.100.5:/usr/include .
mypi@192.168.100.5's password:

Visual Studio 2017은 로컬 헤더 파일의 변경을 인식하고 곧바로 다시 인텔리센스 기능을 제공합니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/12/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 31  32  33  34  35  36  37  38  39  [40]  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12622정성태5/2/20216372오류 유형: 713. XSD 파일을 포함한 프로젝트 - The type or namespace name 'TypedTableBase<>' does not exist in the namespace 'System.Data'
12621정성태5/1/20219831.NET Framework: 1053. C# - 특정 레지스트리 변경 시 알림을 받는 방법 [1]파일 다운로드1
12620정성태4/29/202111948.NET Framework: 1052. C# - 왜 구조체는 16 바이트의 크기가 적합한가? [1]파일 다운로드1
12619정성태4/28/202112435.NET Framework: 1051. C# - 구조체의 크기가 16바이트가 넘어가면 힙에 할당된다? [2]파일 다운로드1
12618정성태4/27/202110999사물인터넷: 58. NodeMCU v1 ESP8266 CP2102 Module을 이용한 WiFi UDP 통신 [1]파일 다운로드1
12617정성태4/26/20218662.NET Framework: 1050. C# - ETW EventListener의 Keywords별 EventId에 따른 필터링 방법파일 다운로드1
12616정성태4/26/20218627.NET Framework: 1049. C# - ETW EventListener를 상속받았을 때 초기화 순서파일 다운로드1
12615정성태4/26/20216799오류 유형: 712. Microsoft Live 로그인 - 계정을 선택하는(Pick an account) 화면에서 진행이 안 되는 문제
12614정성태4/24/20219476개발 환경 구성: 570. C# - Azure AD 인증을 지원하는 ASP.NET Core/5+ 웹 애플리케이션 예제 구성 [4]파일 다운로드1
12613정성태4/23/20218552.NET Framework: 1048. C# - ETW 이벤트의 Keywords에 속한 EventId 구하는 방법 (2) 관리 코드파일 다운로드1
12612정성태4/23/20218649.NET Framework: 1047. C# - ETW 이벤트의 Keywords에 속한 EventId 구하는 방법 (1) PInvoke파일 다운로드1
12611정성태4/22/20217946오류 유형: 711. 닷넷 EXE 실행 오류 - Mixed mode assembly is build against version 'v2.0.50727' of the runtime
12610정성태4/22/20217760.NET Framework: 1046. C# - 컴파일 시점에 참조할 수 없는 타입을 포함한 이벤트 핸들러를 Reflection을 이용해 구독하는 방법파일 다운로드1
12609정성태4/22/20219046.NET Framework: 1045. C# - 런타임 시점에 이벤트 핸들러를 만들어 Reflection을 이용해 구독하는 방법파일 다운로드1
12608정성태4/21/202110081.NET Framework: 1044. C# - Generic Host를 이용해 .NET 5로 리눅스 daemon 프로그램 만드는 방법 [9]파일 다운로드1
12607정성태4/21/20218596.NET Framework: 1043. C# - 실행 시점에 동적으로 Delegate 타입을 만드는 방법파일 다운로드1
12606정성태4/21/202112482.NET Framework: 1042. C# - enum 값을 int로 암시적(implicit) 형변환하는 방법? [2]파일 다운로드1
12605정성태4/18/20218552.NET Framework: 1041. C# - AssemblyID, ModuleID를 관리 코드에서 구하는 방법파일 다운로드1
12604정성태4/18/20217319VS.NET IDE: 163. 비주얼 스튜디오 속성 창의 "Build(빌드)" / "Configuration(구성)"에서의 "활성" 의미
12603정성태4/16/20218170VS.NET IDE: 162. 비주얼 스튜디오 - 상속받은 컨트롤이 디자인 창에서 지원되지 않는 문제
12602정성태4/16/20219368VS.NET IDE: 161. x64 DLL 프로젝트의 컨트롤이 Visual Studio의 Designer에서 보이지 않는 문제 [1]
12601정성태4/15/20218476.NET Framework: 1040. C# - REST API 대신 github 클라이언트 라이브러리를 통해 프로그래밍으로 접근
12600정성태4/15/20218660.NET Framework: 1039. C# - Kubeconfig의 token 설정 및 인증서 구성을 자동화하는 프로그램
12599정성태4/14/20219349.NET Framework: 1038. C# - 인증서 및 키 파일로부터 pfx/p12 파일을 생성하는 방법파일 다운로드1
12598정성태4/14/20219495.NET Framework: 1037. openssl의 PEM 개인키 파일을 .NET RSACryptoServiceProvider에서 사용하는 방법 (2)파일 다운로드1
12597정성태4/13/20219513개발 환경 구성: 569. csproj의 내용을 공통 설정할 수 있는 Directory.Build.targets / Directory.Build.props 파일
... 31  32  33  34  35  36  37  38  39  [40]  41  42  43  44  45  ...