確認Pi 沒有主控台控制權,如果有,先關閉
pi@raspberrypi:~ $ dmesg | grep tty
[ 0.000000] Kernel command line:
dma.dmachans=0x7f35 bcm2708_fb.fbwidth=1024 bcm2708_fb.fbheight=768
bcm2709.boardrev=0xa21041 bcm2709.serial=0xf1ba6d8
smsc95xx.macaddr=B8:27:EB:1B:A6:D8 bcm2708_fb.fbswap=1
bcm2709.uart_clock=48000000 bcm2709.disk_led_gpio=47
bcm2709.disk_led_active_low=0 vc_mem.mem_base=0x3dc00000
vc_mem.mem_size=0x3f000000
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2
rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
[ 0.001532] console [tty1]
enabled
[ 0.093340] 3f201000.uart:
ttyAMA0 at MMIO 0x3f201000 (irq = 87, base_baud = 0) is a PL011 rev2
[ 0.596998] console [ttyAMA0] enabled
[ 4.446432] systemd[1]: Expecting
device dev-ttyAMA0.device...
[ 5.297264] systemd[1]: Starting
system-serial\x2dgetty.slice.
[ 5.319043] systemd[1]: Created
slice system-serial\x2dgetty.slice.
pi@raspberrypi:~ $
關閉方式:
Sudo raspi-config
選 9 Advanced Options
選 A7 Serial Enable/Disable shell
and kernel messages on the serial connection ,關閉他
Reboot後, dmesg | grep tty
pi@raspberrypi:~ $ dmesg | grep tty
[ 0.000000] Kernel command line:
dma.dmachans=0x7f35 bcm2708_fb.fbwidth=1024 bcm2708_fb.fbheight=768 bcm2709.boardrev=0xa21041
bcm2709.serial=0xf1ba6d8 smsc95xx.macaddr=B8:27:EB:1B:A6:D8 bcm2708_fb.fbswap=1
bcm2709.uart_clock=48000000 bcm2709.disk_led_gpio=47
bcm2709.disk_led_active_low=0 vc_mem.mem_base=0x3dc00000
vc_mem.mem_size=0x3f000000
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4
elevator=deadline fsck.repair=yes rootwait
[ 0.001516] console [tty1]
enabled
pi@raspberrypi:~ $
關閉了
去查看 /dev/ttyAMA0 不見了(網路上的範例都是用這個PORT勒 !!!)
參考資料,第一二篇有寫了一大推解決方法……..太難了,看無
進入視窗,左上角 Menu -> Preferences -> Raspberry Pi Configuration
->Interfaces -> Serial --> Enable
Sudo apt-get install minicom 安裝軟體
sudo minicom –b 9600 –o –D /dev/ttyAMA0
切換至新視窗後,按鍵盤的Key,確認在示波器有看到輸出信號
gpio readall -- >check state of all pin
gpio
mode 15 ALTO -->第8pin改成 ALTO state
程式:
Sudo chmod a+rw /dev/ttyAMA0 -->如果沒權限,就無法open port
#include <cstdlib>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
using namespace std;
int main(int argc, char** argv) {
//setup UART
int uart_filestream=-1;
uart_filestream=open("/dev/ttyAMA0",O_RDWR | O_NOCTTY |
O_NDELAY);
if(uart_filestream==-1)
{
printf("Unable to Open
UART \r\n ");
return 0;
}
//Configure UART
struct termios options;
tcgetattr(uart_filestream,&options);
options.c_cflag=B9600 | CS8 |
CLOCAL | CREAD;
options.c_iflag=IGNPAR;
options.c_oflag=0;
options.c_lflag=0;
tcflush(uart_filestream,TCIFLUSH);
tcsetattr(uart_filestream,TCSANOW,&options);
//Transmitting
unsigned char tx_buf[10];
unsigned char *p_buffer;
p_buffer=&tx_buf[0];
*p_buffer++='O';
*p_buffer++='I';
*p_buffer++='A';
if(uart_filestream !=-1)
{
int
count=write(uart_filestream,&tx_buf[0],(p_buffer-&tx_buf[0]));
if(count<0)
printf("TX error
\r\n");
}
close(uart_filestream);
printf("finish\r\n");
return 0;
}
參考資料:
Raspberry
Raspbian turning off the UART functioning as a serial console
Serial(UART)
communication on Raspberry Pi 2
Raspberry Pi 釋放主控台序列埠(GPIO14,GPIO15)
Using the UART (C語言)
沒有留言:
張貼留言