10. 시리얼 통신 헤더파일(wiringSerial.h) 분석. 인터럽트처럼 동작할 수 있게 코드변환.

9에서 배운 것처럼 통신을 하면 되지만. 우리가 원하는 통신은,

자동차 방향이 가운데를 유지하다가 신호가 오면 좌우로 변환되고, 신호가 없으면 다시 가운데를 유지하도록 하는 것이다.


이렇게 하려면 if 등의 조건문이 필요한데... 일단 목표는 LED를 3개 놓고 평소에는 가운데를 입력 받다가 L 누르면 왼쪽 그리고 가만히 있으면 다시 가운데 불켜지고, R 누르면 오른쪽 켜지는 것을 구현하는 것이다.



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
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <string.h>
#include <errno.h>
 
#include <wiringPi.h>
#include <softPwm.h>
#include <wiringSerial.h>
 
int main ()
{
  int fd ; //시리얼 통신 확인 변수
  int count ;
  unsigned int nextTime ;
 
  if ((fd = serialOpen ("/dev/ttyAMA0", 115200)) < 0)  // 두번째 인자값이 보레이트 설정
  {   // fd가 -1이면 아래 처럼 unable 나온다.
    fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;                   
    return 1 ;
  }
 
  if (wiringPiSetup () == -1)
  {
    fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
    return 1 ;
  }
 ////////////////////////////////////////////별거 아님////////////////////////////////////////////////
 
  nextTime = millis () + 300 ;
 
  while(1) 
  {
      scanf("%d", &count);  // 통신확인 안되서 임의로 테스트 해볼 수 있게 scanf
      fflush (stdout) ;
      serialPutchar (fd, count) ; // 데이터 전송해주는 함수
      nextTime += 300 ; //delay 역할?
    
       if(getchar == 1)  // 제일 왼쪽
       {
           softPwmWrite(ledMap[0], values[7]);
       }
    
       else if(getchar ==6)  // 제일 오른쪽
       {
           softPwmWrite(ledMap[7], values[7]);
       }
    
       else
       {
           softPwmWrite(ledMap[3], values[7]);   //빵판의 LED 3,4가 centre
           softPwmWrite(ledMap[4], values[7]);
       }
  
 //////////////////////////////////////////////////////////////////
    delay (3) ;
 
    while (serialDataAvail (fd)) // RX에 데이터 들어오는지 확인하는 함수
    {
      printf (" -> %3d", serialGetchar (fd)) ;  // RX 데이터 받는 함수
      fflush (stdout) ;
    }
  }
 
  printf ("\n") ;
  return 0 ;
}


설정

트랙백

댓글