对于:[实践OK]串口学习之上位机查询模式,下位机arduino采用中断方式读取上位机的指令并crc验证其准确性后读取Tsic506的温度值,再通过CRC校验后经串口发送到上位机。https://jackxiang.com/post/12322/ 这部分串口问题的读取采用select模型的细节分析。

优秀,但最终有一个点仍然想向您探讨一下,上面您给修复后的代码片段中这一外逻辑:
            } else if (num_bytes == 0) {
                // 串口关闭或无数据可读
                perror("Serial port closed or no data available");
                exit(EXIT_FAILURE);
            } else {
                // 读取出错
                perror("Error reading from serial port");
                exit(EXIT_FAILURE);
            }

上面代码子片段中,} else if (num_bytes == 0) {,是在select模型中通知有数据可以读到,我理解至少有一个byte及以上不会是0个byte,它什么样的情况下会出现:num_bytes == 0,再就是这种情况是串口硬件问题还是软件问题,是否可以了也给它一个修复 的机会呢?和select 模型一样:
               if (consecutive_failures >= 5) {
                // 连续失败超过5次,发送错误信息到服务器
                send_error_to_server();
                // 退出程序
                exit(EXIT_FAILURE);
            }  
            continue;

还是必须直接exit(EXIT_FAILURE);更为妥当呢?

二是:
            } else {
                // 读取出错
                perror("Error reading from serial port");
                exit(EXIT_FAILURE);
            }

这部分如果串口读取出错,是否有什么标志位呢,直接放在else中就读取出错,是否有些武断了些,我想要是更谨慎编码,也请给出对应的最后else分支的读取出错的标志判断代码,让这部分代码更加健壮:            } else {
                // 读取出错
                perror("Error reading from serial port");
                exit(EXIT_FAILURE);
            }

,我的两点要求问完了,您可以按条回复解答的同时,给出最终解决后并后的代码,十分感谢 。


回答:
关于您的第一个问题,`num_bytes == 0` 的情况通常表示从串口读取到的字节数为 0,即读取操作没有返回任何数据。这可能是因为串口关闭或者是没有数据可读。如果您认为这种情况是由于串口硬件问题而不是程序逻辑问题导致的,那么直接退出程序可能是更为合适的选择。因此,直接使用 `exit(EXIT_FAILURE)` 可能更为妥当。

对于第二个问题,如果串口读取出错,通常会返回 -1,并将 `errno` 设置为适当的错误代码。您可以根据 `errno` 的值来进一步判断错误的类型。因此,在 `else` 分支中,您可以添加一个针对 `errno` 的判断,以确定错误的具体原因,并根据需要采取相应的措施。

下面是针对您的两个问题的解答和相应的代码修正:

1. 对于 `num_bytes == 0` 的情况,我们直接退出程序,因为没有读取到任何数据,这可能是串口关闭或没有数据可读。

2. 对于读取出错的情况,我们可以根据 `errno` 的值判断错误的具体原因,并据此决定是否退出程序。这样可以使代码更加健壮,能够更好地处理可能的错误情况。

下面是修改后的代码片段:

```c
// 串口读取函数
void read_serial(Config *config) {
    unsigned char buffer[BUFFER_SIZE];
    ssize_t num_bytes;
    int frame_start_detected = 0;
    int recv_index = 0;
    int consecutive_failures = 0; // 记录连续失败的次数
    struct timeval tv;
    fd_set rfds;

    while (1) {
        // 发送查询指令
        send_query(config->serial_fd);
        usleep(150000); // 等待150毫秒

        // 设置超时时间为3秒
        tv.tv_sec = 3;
        tv.tv_usec = 0;
        FD_ZERO(&rfds);
        FD_SET(config->serial_fd, &rfds);

        // 等待串口数据
        int retval = select(config->serial_fd + 1, &rfds, NULL, NULL, &tv);
        if (retval == -1) {
            perror("Error in select");
            exit(EXIT_FAILURE);
        } else if (retval == 0) {
            // 超时,增加连续失败计数器
            consecutive_failures++;
            if (consecutive_failures >= 5) {
                // 连续失败超过5次,发送错误信息到服务器
                send_error_to_server();
                // 退出程序
                exit(EXIT_FAILURE);
            }
            continue;
        } else {
            // 读取串口数据
            num_bytes = read(config->serial_fd, buffer + recv_index, BUFFER_SIZE - recv_index);
            if (num_bytes > 0) {
                // 数据读取成功
                consecutive_failures = 0; // 重置连续失败计数器
                recv_index += num_bytes;
                // 打印接收到的十六进制数据
                printf("\n\nReceived data: ");
                for(int i = 0;i < recv_index; ++i){
                   printf("%02X ",buffer[i]);
                }
                printf("\n");

                // 判断帧头是否检测到
                if (!frame_start_detected) {
                    for (int i = 0; i < recv_index; ++i) {
                        if (buffer[i] == 0xAA) {
                            frame_start_detected = 1;
                            // 移动已接收数据到缓冲区开头
                            memmove(buffer, buffer + i, recv_index - i);
                            recv_index -= i;
                            break;
                        }
                    }
                }
                // 判断是否接收到完整的数据包
                if (frame_start_detected && recv_index >= EXPECTED_PACKET_LENGTH && buffer[recv_index - 1] == 0x55) {
                    // 去除帧头和帧尾后的数据长度
                    int data_length = recv_index - 2;
                    // 进行 CRC 校验,传递数据段给 verifyData 函数
                    if (verifyData(buffer + 1, data_length)) {
                        // 提取温度值并打印
                        float temperature = convertToTemperature(buffer + 1);
                        printf("Temperature: %.1f\n", temperature);
                        // 发送POST请求
                        send_temperature_via_post(temperature);
                    } else {
                        printf("CRC 校验失败\n");
                    }
                    // 清除已处理数据
                    recv_index = 0;
                    frame_start_detected = 0;
                }
            } else if (num_bytes == 0) {
                // 串口关闭或无数据可读
                perror("Serial port closed or no data available");
                exit(EXIT_FAILURE);
            } else {
                // 读取出错
                if (errno == EAGAIN || errno == EWOULDBLOCK) {
                    // 数据暂时不可用,继续下一次循环
                    continue

;
                } else {
                    perror("Error reading from serial port");
                    exit(EXIT_FAILURE);
                }
            }
        }
    }
}
```

这样,当 `num_bytes == 0` 时,表示读取操作未返回任何数据,直接退出程序;当读取出错时,我们根据 `errno` 的值判断错误类型,如果是暂时不可用的错误(如 `EAGAIN` 或 `EWOULDBLOCK`),则继续下一次循环;否则,打印错误信息并退出程序。


细节追问:
很专业,您所回答的:
对于 num_bytes == 0 的情况,我们直接退出程序,因为没有读取到任何数据,这可能是串口关闭或没有数据可读。

这一句中,串口关闭可以理解,如下位机在select明明有数据上来了但这一瞬间串口关掉了,没有读取到任何数据。 但是除开这个特殊情况外还有其它情况吗?我理解明明select 侦测到有数据来了,那么这个数据肯定是num_bytes >= 1,怎么就出现明明就通知有数据就读取到0个数据的逻辑判断上来了呢?我有些不理解,难道这个select模型会出现误通知吗?


终极回答:
您提出了一个很好的问题。

在一般情况下,如果使用 `select` 函数或其他 I/O 多路复用机制,并且它返回了通知有数据可读,那么确实会期望从文件描述符中读取到至少一个字节的数据。这是因为 `select` 函数在通知有数据可读时,文件描述符已经准备好被读取了。

因此,对于 `num_bytes == 0` 的情况,通常不会在正常的 `select` 模型下出现,因为如果 `select` 返回了有数据可读的通知,那么读取操作应该至少读取到一个字节的数据。

然而,这种情况可能会发生在某些特殊情况下,比如:

1. 硬件故障:例如,串口接收缓冲区可能已满,导致无法将数据写入应用程序的缓冲区,从而导致读取操作返回 0 字节。
2. 并发操作:例如,在多线程或多进程环境中,可能会发生竞争条件,导致文件描述符被多个线程或进程同时操作,从而导致某些读取操作返回 0 字节。

虽然这种情况很少发生,并且在大多数情况下,`select` 返回了有数据可读的通知,确实会从文件描述符中读取到至少一个字节的数据。但在编写健壮的程序时,应该考虑到所有可能的情况,并且要有适当的错误处理机制来应对这些情况的发生。

因此,对于 `num_bytes == 0` 的情况,除了串口关闭外,还可能存在其他特殊情况,需要根据实际情况进行适当的处理。
sudo useradd -m -s /bin/bash xiangdong -g irdcops  #添加用户



DownLoad:
https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
出现缺少库ModuleNotFoundError: No module named '_ctypes',再次加上,关于在centos下安装python3.7.0以上版本时报错ModuleNotFoundError: No module named '_ctypes'的解决办法:


而Raspberry Pi读取的是libffi-dev ,如下:

来自:https://blog.csdn.net/wang725/article/details/79905612

编译Python3.7.1教程:https://blog.csdn.net/RambleMY/article/details/82109788

安装numpy:
如果成功安装好了pip,安装numpy及其他任何Python包都非常方便了
  运行pip install numpy
  pip会先自动下载与我们Python版本对应的numpy-xxx.whl文件,然后安装

矩阵计算:



画曲线:
sudo su -
pip3 install matplotlib  #失败
sudo apt-get install python3-matplotlib # 安装成功,但是写代码时引用失败,如下:
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'matplotlib'  ,成功如下:

以上代码在树莓派的界面上运行成功输出一个新窗口的Sina曲线。


二)VNCServer的配置尤其是复制和开机启动相关:
cat /home/irdcops/shell/vncsvr/startvncsvr.sh
killall Xvnc >/dev/null 2>&1  
vncserver -kill :1  
rm -rf /tmp/.X*-lock  
rm -rf /tmp/.X11-unix/X*  
rm -rf /root/.vnc/*.log  
rm -rf /root/.vnc/*.pid  
vncserver :1 -geometry 1024x768  

这个是手动,二是那个能从Windows里复制进去,还得要开机启动,如下配置:



cat /etc/init.d/vncserver



chmod 755 /etc/init.d/vncserver
sudo update-rc.d vncserver defaults

继续解决复制问题,如下步骤:
找不到:vncconfig,在红帽里找了一下,发现在:
rpm -qf /usr/bin/vncconfig
tigervnc-server-minimal-1.8.0-5.el7.x86_64 ,于是推测在Raspberry Pi里也是这样,指示了下:
apt-get install  tigervnc-standalone-server
下列【新】软件包将被安装:
  libgnutls30 libhogweed4 libnettle6 libxfont2 tigervnc-common tigervnc-standalone-server
apt-get install  tigervnc-standalone-server   #之前安装了一个:  sudo apt-get install tightvncserver
root@raspberrypi:~# vncconfig
vncconfig: unable to open display ""

stat /usr/bin/vncconfig
最近改动:2018-12-16 19:23:52.760773550 +0800

据博文,在文件/root/.vnc/xstartup,加一行 vncconfig -iconic &  ,有的说是加: vncconfig -nowin&



三)TensorFlow:https://www.raspberrypi.org/magpi/tensorflow-ai-raspberry-pi/
https://yq.aliyun.com/articles/623213
CentOS系统开发人员Fabian Arrotin非常高兴的宣布为ARM(armhfp)硬件架构发布CentOS Linux 7(1611)镜像。该GNU/Linux发行版本深受单板计算机和嵌入式设备的喜爱,支持树莓派3、树莓派2、Banana Pi,CubieTruck和Cubieboard单板计算机,CentOS Linux 7(1611)for armhfp涵盖一些激动人心的新技术,基于Linux Kernel 4.4 LTS内核,升级uboot镜像至2016.09版本等等。

需要注意的是,ARM版CentOS 7.3.1611是从Red Hat Enterprise Linux 7.3系统中派生出来的,这就意味着操作系统能够兼容上层系统。尽管CentOS 7 Userland for armhfp来自于CentOS 7 GNU/Linux发行版本,但是部分预安装包已经移除,或者针对armv7hl兼容设备进行了优化。
下载:
http://mirror.centos.org/altarch/7/isos/armhfp/Images


摘自:http://www.cnbeta.com/articles/567535.htm?_t_t_t=0.39831793188461095
背景:用raspberry pi+chrome做了一个孵化小鸡的硬件,当时想用websocket,好像没有,想用svg画温度曲线,好像测试了一下也没有,最后用的ajax轮训,后面不知这个新出的版本有没有加上。

Chromium OS for Raspberry Pi 3 0.5版本发布:
下载:download the Chromium OS for Raspberry Pi 3 0.5 binary image
当时被问及Chromium OS是否会移植到树莓派3的时候,Callahan表示已经购买了两台树莓派3,并确认将会得到扩展支持。而现在,Callahan非常高兴的宣布面向树莓派3单板电脑的0.5版本。树莓派3 Model B于2016年2月29日正式发布,支持WiFi和蓝牙,并内部包含64位四核ARM Cortex-A53处理器。
团队表示尽管在树莓派2的基础上开发移植Chromium OS for 树莓派3,但是在0.5版本依然带来了很多新特性。首先提到的是已经压缩尺寸的Linux 4.2.8-ckt8内核,BFS优化、更少的调试输出,且没有Dynamic tick模块,声卡驱动的多重改善,借助BFQ分级调度和按需调度的优化调整带来更好的储存性能。
背景:Raspberry Pi下的FreeBSD关机时候出现:

========================================================
刚搞uboot 学习下 写下来这些自己的小经验
我的终端用的是SecureCRT.exe
烧写uboot后 提示 Hit any key to stop autoboot : 几秒后 aoto运行
如果没有设置好 按键是没有用的 解决方法是:
选择 你打开的串口的会话设置  再选择 键映射  当然还有其他的方法 这里你映射几个键 既可以了

来自:http://blog.sina.com.cn/s/blog_6b94d5680100ms8g.html
SecureCRT 更改键盘映射功能 : http://blog.chinaunix.net/uid-20773865-id-4040702.html
背景:在Raspberry Pi上装了一个FreeBSD,之前是linux,用ssh连接时出现问题,这个ssh其实linux上也是一样的,于是按linux上的办法解决了该问题,重启动sshd后,用我的手机重新ssh到Raspberry pi上,提示error消失,连接正常。
root@rpi2:/usr/local # Feb 18 03:50:39 rpi2 sshd[4917]: error: Could not load host key: /etc/ssh/ssh_host_dsa_key
魅族手机ssh访问:
Feb 18 03:50:41 rpi2 sshd[4917]: error: PAM: authentication error for root from meizu-mx4-pro.lan

Could not load host key: /etc/ssh/ssh_host_dsa_key,那就生成这个key:
root@rpi2:/usr/local # ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
Generating public/private dsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_dsa_key.
Your public key has been saved in /etc/ssh/ssh_host_dsa_key.pub.
The key fingerprint is:
SHA256:0gV9unsS+HmqgE0j45aqtNQVhVsYNUiX2pgBbbmBLbM root@rpi2
The key's randomart image is:
+---[DSA 1024]----+
|   .*oB=o.       |
|   + @oo... .    |
|    =.@   .o     |
|   E *.o ..      |
|    o.+ S. .     |
|  ...B o. o      |
| o .= o  . +     |
|o .o   .  = o    |
|.o.     ...=     |
+----[SHA256]-----+

重新启动ssh服务:
root@rpi2:/usr/local # /etc/rc.d/sshd restart
Performing sanity check on sshd configuration.
Could not load host key: /etc/ssh/ssh_host_dsa_key
Stopping sshd.
Waiting for PIDS: 799.
Performing sanity check on sshd configuration.
Could not load host key: /etc/ssh/ssh_host_dsa_key
Starting sshd.



参考自:http://blog.163.com/zhengjiu_520/blog/static/3559830620118851822482/
配置FreeBSD下的Wifi主要参考这两篇文章即可,树莓派下的也大体一样一样的没有什么大的区别:
http://www.111cn.net/sys/freebsd/59534.htm
国外有一个文章在后面有写。
总之,按这样来配置连接上了家里的路由器。

Raspberry Pi Model B+ Run FreeBSD11+Apache+PHP+MySQL+Webmin+Wordpress:
http://www.bigsea.com.cn/archives/1393/

ifconfig -a时没有wlan0,是因为没有配置及没有启动服务:service netif start
root@rpi2:~ # ifconfig wlan0
wlan0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        ether 3c:46:d8:8e:7d:dc
        inet6 fe80::3e46:d8ff:fe8e:7ddc%wlan0 prefixlen 64 tentative scopeid 0x3
        groups: wlan
        ssid HiWiFi_256ECA channel 4 (2427 MHz 11g) bssid d4:ee:07:25:6e:ca
        country US authmode WPA2/802.11i privacy ON deftxkey UNDEF
        AES-CCM 2:128-bit txpower 0 bmiss 7 scanvalid 60 bgscan
        bgscanintvl 300 bgscanidle 250 roam:rssi 7 roam:rate 5 protmode CTS
        wme roaming MANUAL
        media: IEEE 802.11 Wireless Ethernet DS/1Mbps mode 11g
        status: associated
        nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>

其中“status: associated”表示无线网络已经连接,可以上网冲浪了。
固定你的 resolv.conf
如果你用DHCP,有一个问题可能会不断地搔扰你,就是/etc/resolv.conf里所记录的 DNS 会时不时地被修改。当然除了暴力地
代码如下  复制代码
#chflags schg /etc/resolv.conf

让它不能被修改外,还可以很简单地解决此问题,就是往/etc/dhclient.conf里添加
代码如下  复制代码

prepend domain-name-servers 8.8.8.8, 8.8.4.4;
# See ``man 5 dhclient.conf'' for details.
修改成自己的:
prepend domain-name-servers 114.114.114.114, 123.125.81.6;
# See ``man 5 dhclient.conf'' for details.
详细配置WIFI可查看FreeBSD官方文档。

=========================================================================
USB硬件信息:
root@rpi2:~ # dmesg | grep urtwn0
urtwn0: <Realtek 802.11n NIC, class 0/0, rev 2.00/0.00, addr 4> on usbus0
urtwn0: MAC/BB RTL8188EU, RF 6052 1T1R
urtwn0: at uhub1, port 3, addr 4 (disconnected)
urtwn0: <Realtek 802.11n NIC, class 0/0, rev 2.00/0.00, addr 4> on usbus0
urtwn0: MAC/BB RTL8188EU, RF 6052 1T1R

root@rpi2:~ # cat /var/log/messages | grep urtwn0
Feb 17 11:19:16 rpi2 kernel: urtwn0: <Realtek 802.11n NIC, class 0/0, rev 2.00/0.00, addr 4> on usbus0
Feb 17 11:19:16 rpi2 kernel: urtwn0: MAC/BB RTL8188EU, RF 6052 1T1R
Feb 17 11:39:37 rpi2 kernel: urtwn0: <Realtek 802.11n NIC, class 0/0, rev 2.00/0.00, addr 4> on usbus0
Feb 17 11:39:37 rpi2 kernel: urtwn0: MAC/BB RTL8188EU, RF 6052 1T1R
Feb 17 14:15:35 rpi2 kernel: urtwn0: at uhub1, port 3, addr 4 (disconnected)
Feb 17 14:20:14 rpi2 kernel: urtwn0: <Realtek 802.11n NIC, class 0/0, rev 2.00/0.00, addr 4> on usbus0
Feb 17 14:20:14 rpi2 kernel: urtwn0: MAC/BB RTL8188EU, RF 6052 1T1R

启动时信息提示:
root@rpi2:~ # ifconfig wlan create wlandev urtwn0
ieee80211_load_module: load the wlan_amrr module by hand for now.
wlan0: Ethernet address: 3c:46:d8:8e:7d:dc
wlan0


拔下无线Wifi的USB时提示:
root@rpi2:~ # ugen0.4: <Realtek> at usbus0 (disconnected)
urtwn0: at uhub1, port 3, addr 4 (disconnected)


查看网络:
root@rpi2:~ # ifconfig wlan0 up scan
wlan0: ieee80211_new_state_locked: pending INIT -> SCAN transition lost
SSID/MESH ID    BSSID              CHAN RATE    S:N     INT CAPS
123456          b8:55:10:1a:9d:05    6   54M  -77:-95   100 EP   RSN HTCAP WME
ChinaNet-iYDT   fc:e3:3c:54:b8:ec   11   54M  -93:-95    98 EP   HTCAP WPS WPA WME
CALT            40:16:9f:55:33:2c    7   54M  -90:-95   100 EPS  RSN WPA WME HTCAP ATH WPS
ChinaNet-Kjvz   60:b6:17:79:14:b7    2   54M  -89:-95   100 EP   HTCAP WPA WME
CU_VGSU         88:cf:98:dd:fa:18    3   54M  -91:-95   100 EP   HTCAP RSN WME
HiWiFi_256ECA   d4:ee:07:25:6e:ca    4   54M  -69:-95   100 EP   HTCAP WPA RSN WME
jiaweiyi        8c:be:be:2f:ae:5b    8   54M  -93:-95   100 EP   RSN HTCAP WPS WPA WME
ChinaNet-qePR   60:b6:17:78:89:3e   10   54M  -79:-95   100 EP   HTCAP WPA WME


service netif start     # 这个能自动帮你连接无线获取IP地址,当然如果配置正确的话,开机就可以自动连接上无线了。

实践来源:http://jackxiang.com/post/7492/



—————————————————————————————————————————————————————————————
Setting up USB WiFi under FreeBSD is not a very hard task.
We can use official handbook.
But in my opinion it’s too complex.
Here is a simple guide how to configure USB WiFi.
Our goal is to configure WiFi interface to connect and use available wireless network.
I use Miniature WiFi Module from Adafruit.
This is in reality Realtek RTL8188CUS.
elinux.org says this is a problem adapter.
And I had some problems with it under Raspbian.
There was no driver for this adapter in FreeBSD-10.0-RELEASE.
Driver for this adapter appears in stable images in March.
This is good for us, so we don’t need to adding it into kernel.
During boot we can see our adapter:
点击在新窗口中浏览此图片
After boot we can see it in interfaces list:
点击在新窗口中浏览此图片
If we try to up this interface we will see some warning and error messages:
urtwn-rtl8192cfwT: You need to read the LICENSE file in /usr/share/doc/legal/realtek/.
urtwn-rtl8192cfwT: If you agree with the license, set legal.realtek.license_ack=1 in /boot/loader.conf.
module_register_init: MOD_LOAD (urtwn-rtl8192cfwT_fw, 0xc2aa24f0, 0) error 1
urtwn-rtl8192cfwT: could not load firmware image, error 8
urtwn0: failed loadfirmware of file urtwn-rtl8192cfwT
点击在新窗口中浏览此图片
We need to add this two strings to file /boot/loader.conf:
legal.realtek.license_ack=1
if_urtwn_load="YES"
点击在新窗口中浏览此图片
After reboot we can continue.
Next step is creating wlan interface:
ifconfig wlan0 create wlandev urtwn0
Then we can to up this wlan interface and scan available wireless networks:
ifconfig wlan0 up scan
点击在新窗口中浏览此图片
For permanent use WiFi connection we need to add this two strings to file /etc/rc.conf:
wlans_urtwn0="wlan0"
ifconfig_wlan0="WPA DHCP"
点击在新窗口中浏览此图片
And we need to configure authentication in file /etc/wpa_supplicant.conf
点击在新窗口中浏览此图片
After reboot we can check network connection:
点击在新窗口中浏览此图片
It works!


背景:linux长时间无动作会断掉,此时需要发送点东西保持活动,这个ping估计是有点类似于SecureCRT里终端,发送协议NO-OP每[10]秒。
防止ssh时断连.bat
背景:之前配置成功过,找了很多文章,特别是国外的,现在国内也有兄弟在做这个,于是做一个转载,配置热点有好处,可以连接上去操作树莓派。

     本文是基于在某东购买的usb无线网卡(RTL8188CUS芯片)来制作无线热点。本来想制作一个一键脚本,不过先把实现的过程记录下来。
参考文章
Turn Your Raspberry Pi Into a WiFi Hotspot with Edimax Nano USB EW-7811Un (RTL8188CUS chipset)
Why won’t dnsmasq’s DHCP server work when using hostapd?阅读全文


一)FreeBSD前提是能连上网,配置成功的实践步骤在:https://jackxiang.com/post/8518/
二)后就是安装Lamp架构了,如下pkg,port目前好像用的人较少,先使这个吧,特别是Raspberry这种cpu相对太差,自己编译太麻烦了也更耗时,就别在上面整port了,直接pkg安装即可,也解决了依赖问题:
まず静的リンクしたコマンドでpkg自体をインストール
# fetch http://www.peach.ne.jp/archives/rpi/pkg-static
# chmod 755 pkg-static
# ./pkg-static add http://www.peach.ne.jp/archives/rpi/ports/pkg.txz

デフォルトのパッケージを無効化
# mkdir -p /usr/local/etc/pkg/repos
# echo "FreeBSD: { enabled: no }" > /usr/local/etc/pkg/repos/FreeBSD.conf

独自パッケージリポジトリを追加
# fetch http://www.peach.ne.jp/archives/rpi/rpi.conf
# mv rpi.conf /usr/local/etc/pkg/repos

リポジトリカタログを最新状態に更新
pkg update
pkg install screen
freebsd-update fetch
freebsd-update install
pkg install php56 mod_php56 php56-mysql php56-mysqli
如何在 FreeBSD 10.2 上安装 Nginx 作为 Apache 的反向代理:http://www.linuxidc.com/Linux/2016-01/127139.htm
FreeBSD 10 + Nginx 1.4.4 + PHP 5.5.9 + MySQL 5.6.15:     http://my.oschina.net/neochen/blog/198979

三) FreeBSD的pkg使用方法 :
http://blog.chinaunix.net/uid-20332519-id-4063284.html
====================================================================
在 FreeBSD 10.0 上安装和配置 Nginx+PHP+APC+MySQL:
http://www.vpsee.com/2014/04/install-nginx-php-apc-mysql-on-freebsd-10-0/
安装软件:
pkg search php

FreeBSD 11-CURRENT on Raspberry Pi Apache 2.4/MySQL 5.6/PHP 5.6:
http://www.bigsea.com.cn/archives/1393/

如何在树莓派 2B 上安装 FreeBSD:
http://www.linuxidc.com/Linux/2015-12/126724.htm

FreeBSD 网络配置:
https://wiki.freebsdchina.org/faq/networking


修改raspberry pi上安装的freebsd可用内存大小:
http://blog.sina.com.cn/s/blog_a0aacb430101mj69.html
背景:如果你想在树莓派开机时做一些外围设备的自检,如串口是否连接上,如手机是否连接上adb命令,这样就需要开机后启动一个脚本来做这个事情,于是有这篇文章。
实践如下:
vi /etc/rc.local
/usr/bin/php /var/www/initOuterInterfaceCheck.php
initOuterInterfaceCheck.php

发现是代码有问题,开机后,的确是执行了,但是发现手机没插入,但报检测到手机了~
要让Pi开机启动一个脚本/执行一个命令怎么办?
我们知道,RedHat有 /etc/rc.local 文件,在里面写上要执行的命令就可以开机执行了,这是最简单的办法,而Arch Linux ARM没有这个东西,它是按下面的方法设置的:
假设我要开机执行一句shell命令,把它写在文件 /etc/rc.local 中(在Arch Linux中,此文件一开始是不存在的):
How to execute a shell script/a command on Pi startup?
For RedHat it's very easy, it has a /etc/rc.local file, we just add the commands to the file, while Arch Linux ARM doesn't have such thing, and I'm a beginner of Arch, so after asking Google for many times, I find a way to do that - suppose I need to execute a shell command, so I create the /etc/rc.local file & write the shell command to it:

#!/bin/bash
# this file defines the commands that will be executed at system startup

echo "abc" > /root/test.txt
为此文件赋予可执行权限:
Give the file the executable permission:

1
chmod +x /etc/rc.local
然后创建一个文件 /usr/lib/systemd/system/rc-local.service ,内容为:
Then create a file /usr/lib/systemd/system/rc-local.service , with the content of:

[Unit]
Description=/etc/rc.local Compatibility
ConditionPathExists=/etc/rc.local
  
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
文章来源:http://www.codelast.com/
创建一个软链接:
Create a symbol link:

cd /etc/systemd/system/multi-user.target.wants
ln -s /usr/lib/systemd/system/rc-local.service rc-local.service
启用服务:
Enable the service:

systemctl enable rc-local.service
重启系统,然后就可以开机执行你的shell命令了。如果要立即启用,则执行以下命令:
Restart Pi, then you'll be able to execute the shell command on OS startup. If you want to execute it without restarting Pi, just execute the following command:

systemctl start rc-local.service
这是使用systemd的启动方式,非常麻烦。
如果你只是简单地要设置一个命令的别名,可以把命令写在 /etc/profile 中,ssh登录Pi的时候就自动会执行。

摘自:http://www.codelast.com/?p=4945
root@raspberrypi:/# find . -name "mplayer.conf"
./etc/mplayer/mplayer.conf

修改mplayer的配置文件:/etc/mplayer/mplayer.conf

只要在mplayer.conf添加以下一行即可:

              af=volnorm,volume=10

volume的值可以根据实现情况设置。其中:

af表示Audio Filter,10是分贝值,如果设为-200表示完全静音,60表示增大到原音量的1000倍,10的(60/20)次方=1000,一般设为10分贝就可以。

来自:http://blog.csdn.net/ganggexiongqi/article/details/8453188

mplayer for android:
http://download.csdn.net/download/gaxuhongyu/3078360

adb 运行手机上的Mplayer:

adb shell am start -n com.android.mplayer  -ao alsa  /data/data/com.ayansoft.androphp/php/temperature/1.mp3
背景:你买个树莓派的屏,有时你不想屏自动关闭,但有时它自己睡眠了,以为是屏自己控制的,一问才知可能是树莓派的一个文件可以禁用屏休眠。
Pi 用户下:
主要是在Pi的界面上知道DISPLAY这个变量值,再才是在secureCRT的ssh里:(参考:http://jackxiang.com/post/7689/)
pi@raspberrypi:~$ export DISPLAY=:0.0
pi@raspberrypi:~$ /usr/bin/xset -dpms

将dpms关闭,原来
DPMS (Display Power Management Signaling) is a standard to reduce power consumption in monitors.
是为了省电的,将这个关闭所以屏幕就不会自动变黑了。

==============================================================
root@raspberrypi:/# vi /etc/rc.local
root@raspberrypi:/# source /etc/rc.local
My IP address is 192.168.2.2 192.168.199.167
pi@raspberrypi ~ $ setterm -blank 0
pi@raspberrypi ~ $ vi ~/.xinitrc
pi@raspberrypi ~ $ chmod a+x  ~/.xinitrc

sudo apt-get install xserver-xorg-core xserver x11-xserver-utils

root@raspberrypi:/data/htdocs/t.jackxiang.com# dpkg -L  x11-xserver-utils
/usr/bin/xset

export DISPLAY=:0.0
/usr/bin/xset s off # don't activate screensaver
/usr/bin/xset -dpms # disable DPMS (Energy Star) features.
/usr/bin/xset s noblank # don't blank the video device
exec /etc/alternatives/x-session-manager # start lxde

http://ju.outofmemory.cn/entry/103897

=====================实践OK如下===================================
#!/bin/bash
export DISPLAY=:0.0
/usr/bin/xset s off # don't activate screensaver
/usr/bin/xset -dpms # disable DPMS (Energy Star) features.
/usr/bin/xset s noblank # don't blank the video device
exec /etc/alternatives/x-session-manager # start lxde


su pi -c "/bin/bash /home/pi/.xinitrc"


vi /etc/rc.local        //写进去以开机启动时执行一下,http://jackxiang.com/post/8146/
su pi -c "/bin/bash /home/pi/.xinitrc"




=======下面这个办法在sudo su -时会提示找不到这个xsetdpms和xsets 命令,然并卵=======

pi@raspberrypi ~ $ sudo su -
-su: xsetdpms: command not found
-su: xsets: command not found
删除就好了,按前面的实现方法Ok:
rm -Rf /etc/profile.d/Screen.sh


原理:也就是说,你们屏没有休眠这一说,是树莓派自己控制,也就是它自己输出黑屏,但我一触摸这个屏又好了,随便建一个文件就能搞定这个博客说的文件名不限制有点玄乎,有空试试。那个黑屏是树莓派本身的问题。。你连到HDMI上 也一样会。
要想永久禁用树莓派休眠,我们可以在/etc/profile.d路径下新建一个file,如Screen.sh,并将下面两条命令写入该文件,即可以实现永久禁用。

xsetdpms 0 0 0

xsets off


[树莓派经验] 如何更换树莓派软件源 :
编辑 /etc/apt/sources.list 文件
sudo nano /etc/apt/sources.list
删除原来的内容,更换为以下内容
一)
deb http://mirrors.ustc.edu.cn/raspbian/raspbian/ wheezy main contrib non-free rpi
二)
以我用的清华源为例  将其中的官方源地址换为 http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian 就行了
Ctrl+O保存,Ctrl+X退出

然后执行 apt-get update 命令更新软件列表。


我用的raspbian, 先装emacs,然后找软件源。至于为什么要找软件源。这问题可以问你自己也可以问我,官方源实在太慢了!
在镜像列表找了一下,有个清华的,准备试一下,还不错的说。
———————————————————————————————————————————————————————————
1. 对原sources.list文件做备份
命令:   sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup  

2. 修改sources.list
命令:   sudo nano /etc/apt/sources.list
以我用的清华源为例  将其中的官方源地址换为 http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian 就行了
3. 更新软件列表
命令:   sudo apt-get update

来自:http://blog.csdn.net/burgess_liu/article/details/8721389
更多:http://www.cnblogs.com/xiaowuyi/p/4063323.html

源来自:http://bbs.elecfans.com/jishu_450134_1_1.html
root@raspberrypi:~# minicom
Device /dev/ttyUSB0 is locked.
rm -Rf  /var/lock/LCK..ttyUSB0
阅读全文
背景:想开机启动树莓派的chrome浏览器。

网上有不少关于如何让Linux自动运行自己编写的脚本或者程序的方法,但是大多数都是把命令写到/etc/rc.local里,这样虽然能够实现随机运行,但是并不够灵活。不能像mysql,apache等服务一样能够使用service命令或者调用init.d下的脚本启动、关闭或者重启进程。例如,

service mysql restart service apache2 stop

或者

/etc/init.d/mysql restart /etc/init.d/apache2 stop

因为不同的Linux发行版本,对后台服务的处理方式不大一样,如redhat使用chkconfig来管理开机程序。所以下面的方法以debian类系统为例,如何写一个简单的开机启动脚本。所以,此方法适用于raspbian pi的系统。

以svn为例:

1.制作开机启动脚本svn_serve

#!/bin/sh
### BEGIN INIT INFO
# Provides:          svn_serve
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start or stop the HTTP Proxy.
### END INIT INFO
case $1 in
    start)
        svnserve -d -r /home/pi/svn_repository
        ;;
    stop)
        killall svnserve
        ;;
*)
echo "Usage: $0 (start|stop)"
;;
esac
如果不加上面的注释,执行下面步骤3时,update-rc.d会报如下的警告信息

update-rc.d: warning: /etc/init.d/proxy missing LSB information
update-rc.d: see <http://wiki.debian.org/LSBInitScripts>
2.启动关闭服务

sudo service svn_serve start
sudo service svn_serve stop
3.让svn_serve开机启动

sudo update-rc.d svn_serve defaults
4.取消svn_serve的开机自动启动

sudo update-rc.d -f svn_serve remove

来自:http://www.cnblogs.com/mahang/p/3323474.html
借来GPRS模块试了一下,http.get方法还是很容易的,估计POST也简单,就不试了.

查了一下,貌似没有任何资料有关SIM900a模块HTTP 1.1长链接的事情,如果不支持长链接,需要上位机给下位机实时发消息,就只能用TCP了。。



正常的话 返回200

来自:http://www.ysgh.net/archives/601
背景:想在树莓派上插上Arduion Uno的串口,进而两者进行通讯,后根据数据进一步判断并通过另一个Raspberry Pi的usb接口接上SIM900打电话的功能。
实践OK,如下:

输入minicom后,想退出怎么办,帮助:Ctrl+a,退出:x(eXit and reset.....X ),清屏:c(Clear Screen.......C)

用c语言读取的代码如下:
http://jackxiang.com/post/8003/

1.安装python(安装过的请跳到下一步):  sudo aptitude install python-dev
2.安装python的GPIO模块,用于控制LED、电机等(安装过的请跳到下一步)
  命令行下 输入(一行一个命令)
wget http://raspberry-gpio-python.googlecode.com/files/RPi.GPIO-0.3.1a.tar.gz    (下载GPIO库)
  tar xvzf RPi.GPIO-0.3.1a.tar.gz  (tar解压)
  cd RPi.GPIO-0.3.1a               (进入解压后的文件夹)
  sudo python setup.py install     (安装GPIO库文件)
复制代码 要是看不懂上面的两点请移步到这http://www.shumeipai.net/forum.php?mod=viewthread&tid=939

  3.安装serial,用于串口通信及USB通信:
sudo apt-get install python-serial
复制代码 4.当然你要是想在树莓派装串口调试工具就装:
sudo apt-get install minicom  
配置minicom:
sudo minicom -s    
启动出现配置菜单:选serial port setup
进入串口配置
输入A 配置串口驱动为/dev/ttyAMA0
输入E 配置速率为9600 8N1
输入F 将 Hardware Flow Control 设 为 NO
回车 退出      
由于我们使用minicom作为超级终端控制路由器等设备, 而不是控制modem, 所以需要修改Modem and dialing, 将Init string, Reset string, Hang-up string设置为空. 设置完成后选择Save setup as dfl将当前设置保存为默认设置.    在配置菜单 选Save setup as df1保存(一定要记得这一步)   选Exit退出下次在输入minicon 即可直接进入。
命令minicom是进入串口超级终端画面,而minicom -s为配置minicom。说明/dev/ttyAMA0 对应为串口0 为你连接开发板的端口
。(如何更详细使用说明移步到这http://www.shumeipai.net/forum.php?mod=viewthread&tid=550)

接下来测一下环境是否都OK
sudo nano test.py
或用xwindow下用python IDE打开一个空的文件
    输入内容
import serial
import RPi.GPIO
保存退出,
    然后运行代码:
python test.py
(如果没有报错那就说明RPi.GPIO与serial两个库安装成功)上面环境说完了,重点部份来了

USB对接方式:
  1.插上两个Arduion Uno与树莓派 的USB 然后 在树莓派输入:
ls /dev/tty*
查看有没有ttyACM0 这个文件(注只有在两个硬件USB互连的情况下才会有这个。如果两者没有连接是不会有的) 最新的系统一般都会自动生成。看到ttyACMO就说明二者可以通讯了 接下来上测试代码
  Arduino代码:
  byte number = 0;
  void setup(){
     Serial.begin(9600);
  }

  void loop(){
    if (Serial.available())  {
       number = Serial.read();
       Serial.print("character recieved: ");
       Serial.println(number, DEC);
   }
   }
先把上面的代码下载的Arduino上然后再把两者USB互接

树莓派python代码:
import serial
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.open()

ser.write("testing")
try:
    while 1:
             response = ser.readline()
             print response
except KeyboardInterrupt:
    ser.close()
保存xxx.py 退出

注:树莓派下的serial与GPIO库都要在root帐户下才能运行
Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法:
http://lovesoo.org/python-script-error-attributeerror-module-object-has-no-attribute-solve-method.html

然后运行树莓派代码 python xxx.py(这里的xxx就是你保存的树莓派代码)看到
character recieved: 116
character recieved: 101
character recieved: 115
...
character recieved: 103
说明两者USB通迅成功了

摘自:http://forum.eepw.com.cn/thread/258770/1
分页: 1/3 第一页 1 2 3 下页 最后页 [ 显示模式: 摘要 | 列表 ]