[英]RPI with multiple pics over rs485 - CCS compiler
我正在嘗試將rpi3作為主機,將pic作為從機,將rs485作為網絡介質。 rpi循環遍歷從站的ID,它一一發送給它們,然后等待指定的從站(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(); 從我的圖片代碼中獲取圖片,然后當圖片回復時,下一張不會,下一張按計划打印,以下也不會打印,依此類推。
一個明顯的問題是,使用9600在UART上發送10個字節的數據需要10ms以上的時間。 您不應使用一些自釀的死等待延遲,而應在循環中輪詢UART rx標志。
1字節數據= 1個起始位,8個數據位,1個停止位(無分度,1個停止位)= 10位
發送一位需要9600 ^ -1我們。
9600 ^ -1 * 10位* 10字節= 10.42ms最佳情況
在PIC方面,fgets()等待CR字符, 但您不從RPI 發送它 。 另一個問題可能是
while(serial.available()> 0){data = serial.read(); }
在我看來,您一直在覆蓋變量data
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.