【问题标题】:RPI with multiple pics over rs485 - CCS compilerRPI 与 rs485 上的多张图片 - CCS 编译器
【发布时间】:2017-06-18 05:11:33
【问题描述】:

我正在尝试将 rpi3 作为主机,将 pic 作为从机,并将 rs485 作为网络介质。 rpi 循环遍历 slave 的 id,将它们一一发送,并等待来自指定 slave (pic) 的回复。 每个 pic 读取接收到的数据(地址)并与它的地址进行比较,如果是,则 pic 必须回复 rpi。

在 rpi 上我使用 pi4j java 库,而在图片上我使用 CCS 编译器进行编码。

第一个问题是,当我从rpi发送一个图片中不存在的地址时,没有人回复rpi,并且我在下面的指令中发送它的地址的图片也不会回复。

RPI 代码为:

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.serial.*;
import com.pi4j.util.Console;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.sql.Time;


public class SerialExample {

    public void tryIt() throws InterruptedException, IOException {

        final Console console = new Console();

        // print program title/header
        console.title("<-- The Pi4J Project -->", "Serial Communication Example");

        // allow for user to exit program using CTRL-C
        console.promptForExit();

        // create an instance of the serial communications class
        final Serial serial = SerialFactory.createInstance();

        try {
            // create serial config object
            SerialConfig config = new SerialConfig();

            config.device("/dev/ttyAMA0")
                    .baud(Baud._9600)
                    .dataBits(DataBits._8)
                    .parity(Parity.NONE)
                    .stopBits(StopBits._1)
                    .flowControl(FlowControl.NONE);


            // open the default serial device/port with the configuration settings
            serial.open(config);
            final GpioController gpio = GpioFactory.getInstance();

            // provision gpio pin #01 as an output pin and turn on
            final GpioPinDigitalOutput cts = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "CTS", PinState.HIGH);

            while (true) {
                short i = 1; // pic ids
                while (i < 5) {
                    cts.high();
                    serial.write(String.valueOf(i));
                     System.out.println("to slave: " + i);
                    Thread.sleep(15);
                    cts.low();
                    Thread.sleep(150);
                    byte[] data = null;

                    while (serial.available() > 0) {
                        data = serial.read();
                    }
                    if (data == null) {
                        System.out.println("[null data]");
                    } else {
                        String myData = new String(data);
                        System.out.println("reply from slave: " + myData);
                    }
                    i++;
                    Thread.sleep(1000);
                }
            }
        } catch (IOException ex) {
            console.println(" ==>> SERIAL SETUP FAILED : " + ex.getMessage());
            return;
        }
    }
}

图片的代码在所有图片中是通用的,唯一的区别是id。 图片代码是

#include <12F1822.h>
#include <String.h>
#fuses NOMCLR INTRC_IO PLL_SW
#use delay(clock=32000000)

#use rs232(baud=9600, xmit=Pin_A0, rcv=Pin_A1,enable=Pin_A2, bits=8, errors, stream=RS485)

void clear_usart_receiver(void) {
    char c;
    c = getc();
    c = getc();
}
int id [] = {0, 2};

void main() {

    while (1) {
        byte message[10];
        fgets(message, RS485);
        delay_ms(10);
        if (message[0] == '2')

        {
            fputs("s2 reply", RS485);
        }
        delay_ms(10);
        clear_usart_receiver();
    }
}

我已尝试删除 clear_usart_receiver();从我的图片代码,然后当一个图片回复时,下一个不会,下一个按计划打印,下面也不会打印,依此类推。

【问题讨论】:

  • 您如何复用该串行通信?你能上传框图或电路的一些原理图吗...?
  • "当我从 rpi 发送图片中不存在的地址时,没有人回复 rpi" 这不是按照规范吗?如果您尝试解决不存在的问题,则说明您的系统配置错误。

标签: java c pic raspberry-pi3 rs485


【解决方案1】:

在 PIC 方面,fgets() 等待一个 CR 字符,但您不会从 RPI 发送它。另一个问题可能是

while (serial.available() > 0) { 数据 = serial.read(); }

在我看来,您一直在覆盖变量 data

【讨论】:

    【解决方案2】:

    一个明显的问题是使用 9600 通过 UART 发送 10 个字节的数据需要超过 10 毫秒。您不应该使用一些自制的死等待延迟,而是在循环中轮询 UART rx 标志。

    1 字节数据 = 1 个起始位,8 个数据位,1 个停止位(无部分,1 个停止位)= 10 位

    发送一个比特需要 9600^-1 我们。

    9600^-1 * 10bits * 10 bytes = 10.42ms 最佳情况

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-18
      • 1970-01-01
      • 2019-03-07
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多