'2014/03/21 글 목록 :: Horizontal Grays S2

이전 포스팅에서 Bluetooth Serial Port를 생성하고 연결하기 위해 몇가지 쉘 명령어를 이용해야만 했다.


기본적으로 내가 하고 있는 Linux Client PC는 Monitor도 없이 Stand Alone 으로 동작해야 해서 


전원을 켜면 제작한 프로그램이 실행되고 알아서 블루투스 연결하고 와이파이 연결하고 해야한다.


와이파이야 자동으로 연결되니 별 문제 없다면 블루투스는 프로그램 시작 전에 쉘 명령어를 입력해야 해서


C에서 입력 하는 방법을 검색 ㅋ (왠지 프로그래밍을 구글이 하는 것 같다. ^^;)


- 물론 윈도우 컴퓨터 등에서 원격접속으로 이래저래 실행 할 수도 있다. -


http://www.digipine.com/programming/25045


위 링크를 참고해서 다음과 같이 프로그램을 실행해보았다.


//Shell command

//Connect Bluetooth Seria Port

int ret = system("sudo su");

if(ret == -1) printf("sudo su Failed\n");

else printf("sudo su OK\n");


ret = system("password");

if(ret == -1) printf("Password input Failed\n");

else printf("Password input OK\n");

 즉 나는 장치를 컨트롤 해야하므로 Root 권한을 획득해야 한다.

그래서 루트권한 획득 sudo su 명령을 입력



그랬더니 다음과 같은 Error T.T

sudo: no tty present and no askpass program specified

sudo: no tty present and no askpass program specified

Sorry, try again.

sudo: no tty present and no askpass program specified

sudo: no tty present and no askpass program specified

Sorry, try again.

sudo: no tty present and no askpass program specified

sudo: no tty present and no askpass program specified

Sorry, try again.


http://stackoverflow.com/questions/21659637/sudo-no-tty-present-and-no-askpass-program-specified-netbeans


위 링크를 보니 문제는 sudo 명령 자체가 터미널에 붙여서 사용하는 것인데 쉘 명령으로 실행하니  sudo 명령을 attach 할 수 없는데 있던 것 같다.

상기 링크에서는 askpass를 이용하라는 데 난 좀 더 무식하게 해결하기로 했다.


방법은 sudo configuration을 수정하는 것

(이 방법은 추천하고 싶지 않다. sudo 명령을 비번없이 실행할 수 있기에 위험하다.)


위와 같이 sudo select-editor를 실행하면 Editor를 선택할 수 있다. nano가 편하니 2번 선택

(한번 선택하면 앞으로 계속 nano로 선택된다.)


그리고 visudo 실행한다.


# This file MUST be edited with the 'visudo' command as root.

#

# Please consider adding local content in /etc/sudoers.d/ instead of

# directly modifying this file.

#

# See the man page for details on how to write a sudoers file.

#

Defaults        env_reset

Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:$


# Host alias specification


# User alias specification


# Cmnd alias specification


# User privilege specification

root    ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges

%admin ALL=(ALL) ALL


# Allow members of group sudo to execute any command

#%sudo  ALL=(ALL:ALL) ALL  

%sudo ALL=NOPASSWD: ALL

# See sudoers(5) for more information on "#include" directives:


#includedir /etc/sudoers.d



아래쪽 %sudo 항목에서 

%sudo  ALL=(ALL:ALL) ALL  -> %sudo ALL=NOPASSWD: ALL 이렇게 바꿔줌


그리고 ctrl + o 로 저장 ctrl + x 로 나오면 앞으로 sudo 명령을 입력해도 비번을 묻지 않는다.


* 원래는 Ubuntu 에서 sudo configuration은 /etc/sudoers 에서 관리하는데 위험하기 때문에  visudo (/etc/sudoers.tmp)로 관리한다.

* visudo의 자세한 사용법은 검색 ㅋ (위험하니까 책임회피 ㅋ)

* 사실 한번 잘못써서 터미널에서 sudo를 사용못하게 되어서 복구모드로 진입해서 겨우 해결 (식겁했다는 T.T) http://www.psychocats.net/ubuntu/fixsudo  <= 이 사이트가 도움이 되었슴




여튼 포스팅이 삼천포로 가고 있다. ㅋㅋㅋ

다시 원래의 주제로 돌아와서 C에서 Shell 명령이 잘 되는지 다시 해보기로 한다.


결과는 다음과 같았다.


혹시 몰라 터미널을 실행하는 명령어를 입력했더니 Terminal 창이 하나 딱 뜬다.

그리고 기다린다. T.T

Terminal에서 exit 입력해서 끝내고 rfcomm connect 명령을 실행한다. 그리고 성공한다.

그런데 문제는 위와같이 실행중이 되어 버린다는 것이다.


사실 터미널에서 rfcomm connect를 수행할 때도 딱 저 메시지가 뜨고 터미널 작업은 새로운 터미널을 띄우고 했는데 T.T

여튼 어쩔수 없이 Ctrl+C로 연결을 끊고 넘어갔슴..

연결을 끊었으니 통신은 당연 안됨 T.T


다른 방법을 강구해보자!!





현재 하고 있는 작업 중

Server PC(Windows 7) 에서 명령을 보내면 Client PC(Linux) 에서 명령을 받아 해석하고 수행 그리고 결과를 넘겨주어야 하는데 이를 블루투스를 통해서 한다.

이를 위하여 Server PC와 Client PC는 블루투스 시리얼포트를 이용하여 통신을 하게 된다.


일단 Server는 윈도우 환경에서 BlueSoleil을 이용하니까 별문제가 없다. 

리눅스 PC에 NEXT-BT104라는 블루투스 동글을 설치하였는데 

이게 윈도우 상에서는 Serial Port Service가 보이는 걸로보아  (아마 SPP Protocol 아니면 RFCOMM protocol 이겠지) 시리얼 포트를 지원하는 것 같은데

리눅스에 꽂아두면 Server PC에서 사용가능한 서비스에 Serial Port가 나타나지 않는다.


 우분투/설정/블루투스 에서도 Serial Port 해당 서비스 안보임 T.T

이를 해결하기 위해 역시 검색 

http://robotbaram.tistory.com/entry/%EB%B8%94%EB%A3%A8%ED%88%AC%EC%8A%A4-%EC%8B%9C%EB%A6%AC%EC%96%BC-%ED%8F%AC%ED%8A%B8-%EC%84%A4%EC%A0%95 

위 링크에 가장 쉬운 방법이 있었다.


* Blueman 설치

$sudo apt-get install blueman



으로 설치하고 프로그램(이름이 "블루투스 관라지", 참고로 우분투/설정/블루투스는 이름이 "블루투스")을 실행하면



메뉴의 설정을 누르면 서비스에 Serial Port가 보임 이를 이용하여 연결하면 나의 경우 /dev/rfcomm0 로 연결됨 


자.. 이건 GUI환경에서이고 Terminal 환경에서 접속을 시도해 보자.

(이유는 C 프로그램 안에서 위의 작업들이 수행되어야 해서 T.T)


참고한 사이트는 아래와 같다.

http://stackoverflow.com/questions/15464475/how-to-setup-serial-communication-in-processing-to-dev-rfcomm0


위와 같이 터미널에서 hcitool scan 하면 주변의 블루투스 장치들이 검색된다.


연결은 rfcomm 을 이용한다.


$sudo nano /etc/bluetooth/rfcomm.conf

우선 위 명령어를 이용하여 rfcomm.conf 파일을 수정해주자.


# RFCOMM configuration file.

#


rfcomm0 {

#       # Automatically bind the device at startup

        bind yes;

#

#       # Bluetooth address of the device

        device 00:15:83:6B:FA:C1;

#

#       # RFCOMM channel for the connection

       channel 1;

#

#       # Description of the connection

        comment "Jslee-laptop-bluetooth";

}



기본적으로는 모두 #으로 주석처리 되어있는데 필요한 부분은 #을 지워 활성화 시켜준다.

bind no -> bind yes

device XX:XX:XX:XX:XX:XX: 연결하고자 하는 PC의 Bluetooth Address 

channel 1

그리고 혹시 몰라 comment도 ^^;



위 그림과 같이

$sudo rfcomm connect 0


를 입력하면


접속 성공!!!


Server PC의 BlueSoreil 에서도 연결이 되었슴이 나타난다.



그리고 작성한 시리얼통신 프로그램 예제를 실행하니

/dev/rfcomm0: Permission denied


메시지가 뜨고 실행이 종료된다.


즉 /dev/rfcomm0에 대한 권한이 없다는 것인데 이는 권한을 부여하면 된다.




위 그림과 같이


$ls -al /dev/rfcomm0 

하면 현재 /dev/rfcomm0 의 권한이 root 에게만 read ,write 가능한 것을 볼 수 있다.

이를

$chmod a+rw /dev/rfcomm0 

해주고 다시

$ls -al /dev/rfcomm0 

확인하면

read, write 권한이 모두에게 주어진 것을 볼 수 있다.



그리고 다시 실행하니 성공!!! 


자 이제 위의 쉘 명령들을 C프로그램 안에서 구현해봐야겠다.


+ Recent posts