공부/Embedded

[Raspberry PI] 기본 세팅 / 시리얼 통신

래울 2025. 4. 27. 21:54

Raspberry PI SSH 연결 / VNC 연결

1. Raspberry PI Imager 다운로드

https://www.raspberrypi.com/software/

 

Raspberry Pi OS – Raspberry Pi

From industries large and small, to the kitchen table tinkerer, to the classroom coder, we make computing accessible and affordable for everybody.

www.raspberrypi.com

 

2. Raspberry PI OS 설치

 

3. 라즈베리 파이 접속 후, Interface Options 설정

$ sudo raspi-config 

 

Interface Options에서 SSH와 VNC를 Enable 설정

 

4. SSH 연결

Moba든 xshell 이든, Putty든 아무거나 해서 연결

 

5. VNC Viewer

라즈베리파이의 경우 별도의 마우스/키보드를 연결해서 사용해야하는데, 귀찮으니까 VNC Viewer를 설치해서 연결

https://www.realvnc.com/en/connect/download/viewer/?lai_vid=DBWRDBO2khy6&lai_sr=0-4&lai_sl=l

 

Download VNC Viewer by RealVNC®

RealVNC® Viewer is the original VNC Viewer and the most secure way to connect to your devices remotely. Download VNC Viewer by RealVNC® now.

www.realvnc.com

 


USB to TTL

1. USB to TTL 컨버터/케이블 준비

https://www.devicemart.co.kr/goods/view?no=1164522&srsltid=AfmBOorjpBJqF-7JH_xvgYrb48SpfHkJ797Yyq1wRsMHv0MLP1JVT-v8

 

USB to TTL Serial Cable

PL2303TA(PL2303HX) 칩셋을 이용한 USB to TTl 컨버터

www.devicemart.co.kr

 

2. Pin Map보고 연결

라즈베리 파이와 연결을 위해 맞게 Pin Map에 맞게 연결해준다.

Raspberry PI 4B Pin Map https://linuxhint.com/gpio-pinout-raspberry-pi/

 

휜색 - UART RX

초록 - UART TX

빨강/검정 - 5V/GND

 

 

3. 장치이름확인

USB연결 시 출력되는 로그 메시지로 알 수 있음

$ dmesg | grep tty

→ ttyS0

 

4. 테스트 코드 작성

// serial_write.c
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>

int main() {
    int serial_port = open("/dev/ttyS0", O_RDWR);

    if (serial_port < 0) {
        perror("Error opening serial port");
        return 1;
    }

    struct termios tty;
    memset(&tty, 0, sizeof tty);

    // 현재 시리얼 포트 설정 가져오기
    if (tcgetattr(serial_port, &tty) != 0) {
        perror("Error from tcgetattr");
        return 1;
    }

    // 설정 수정
    cfsetospeed(&tty, B115200);
    cfsetispeed(&tty, B115200);

    tty.c_cflag |= (CLOCAL | CREAD);    // 포트 소유 + 읽기 활성화
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;                 // 8-bit
    tty.c_cflag &= ~PARENB;             // 패리티 없음
    tty.c_cflag &= ~CSTOPB;             // 1 스톱비트
    tty.c_cflag &= ~CRTSCTS;            // 하드웨어 플로우컨트롤 없음

    // RAW 모드 설정
    tty.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // 입력 라인모드 해제
    tty.c_iflag &= ~(IXON | IXOFF | IXANY);          // 소프트웨어 플로우컨트롤 없음
    tty.c_oflag &= ~OPOST;                           // 출력 후처리 없음

    // 변경사항 적용
    if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
        perror("Error from tcsetattr");
        return 1;
    }

    // 메세지 전송
    const char* msg = "testcode\n";
    write(serial_port, msg, strlen(msg));

    printf("메세지를 전송했습니다: %s\n", msg);

    // 포트 닫기
    close(serial_port);

    return 0;
}

 

5. 시리얼 통신 결과 확인