2017-02-01 28 views
2

我試圖剝奪rpi3作爲一個主,PIC作爲奴隸,rs485作爲網絡媒介。 rpi循環遍歷從屬的ID,它將它們逐個發送,並等待來自指定從屬站(pic)的回覆。 每張圖片都會讀取接收到的數據(地址)並將其與地址進行比較,如果是,則圖片必須回覆rpi。通過rs485多個圖片的RPI - CCS編譯器

關於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; 
     } 
    } 
} 

的PIC的代碼是所有圖片中常見的,唯一的區別是IDS。 PIC代碼是

#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();從我的圖片代碼,然後當一個圖片回覆時,下面的一個不會,下一個會按計劃打印,下面將不會打印任何內容等等。

+0

你怎麼複用那個串行通信?你可以上傳程序框圖或電路原理圖嗎? –

+0

「當我從rpi發送一個不存在於圖片中的地址時,沒有人回覆rpi」不是按照規範說的嗎?如果您試圖解決不存在的問題,那麼您的系統配置錯誤。 – Lundin

回答

0

一個明顯的問題是,使用9600通過UART發送10個字節的數據需要超過10ms。您不應該使用一些自釀的死鎖等待延遲,而是在循環中輪詢UART rx標誌。

1字節數據= 1個起始位,8個數據位,1位停止位(無partity,1個停止位)= 10個比特

它需要9600^-1我們來發送一個比特。

9600^-1 * 10位* 10個字節= 10.42ms最好的情況下

0

在PIC側,與fgets()等待CR字符,但你不從RPI發送。另一個問題可能是

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

在我看來,你繼續覆蓋變量data