我正在使用来自Ublox的RaspberryPI 3和GPRS / GPS devKit。使用Raspbian和我在下面发布的代码,我能够建立一个gprs连接,并通过gprs模块和USB /串行连接访问互联网。但是,同一个Raspberry PI 3上的Ubuntu Mate发行版上的相同代码将无法正常工作,这给我一个“无法打开端口”的错误。
有什么区别,为什么它在Raspbian而不是在Ubuntu Mate上工作?这可能是linux发行版之间端口命名的问题吗?
谢谢!
通过串口建立连接的代码:(信息:我使用信号量来控制每个命令的输出并以正确的顺序启动它们)
import com.pi4j.io.serial.Serial;
import com.pi4j.io.serial.SerialDataEvent;
import com.pi4j.io.serial.SerialDataListener;
import com.pi4j.io.serial.SerialFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.logging.Level;
import java.util.logging.Logger;
import rx.Observable;
import rx.functions.Action1;
/**
*
* @author daniel.cardoza
*/
public final class Gsm {
private final Serial serial = SerialFactory.createInstance();
private static String DEVICE_PORT = "/dev/ttyACM0";
private static int BAUD_RATE = 115200;
private int test = 0;
//COMMANDS
public static String OPEN_ECM_DATA_CONNECTION = "AT+UCEDATA=2,0";
public static String CHECK_PIN = "AT+CPIN?";
public static String GPS_REQUEST = "AT+ULOC=2,3,1,5,10\r";
public static String RESULT_CODE_OK = "\r\nOK\r\n";
public static String TEXT_RESPONSE_ULOC = "+UULOC:";
public static String INTERNAL_CONTEXT = "AT+UPSDA=0,3";
public static String AGNSS_CONF = "AT+UGSRV=\"cell-live1.services.u-blox.com\",\"cell-live2.services.u-blox.com\",\"J-aI_-ZUNEef7ZGUv67LJw\",14,4,1,65,0,15�";
public static String AGNSS_LOG = "AT+UGIND=1";
public static String AGNSS_LOCATION_LOG = "AT+ULOCIND=1";
public static String ERROR = "AT+CMEE=2";
ArrayList<String> commandConfig = new ArrayList<String>();
ArrayList<String> commandConfigResponse = new ArrayList<String>();
private Semaphore semaphore = new Semaphore(1);
public Gsm() {
}
//Opening the AT and Data cghannel
public boolean connect() {
try {
System.out.println("conectamos");
serial.open(DEVICE_PORT, BAUD_RATE);
return true;
} catch (Exception e) {
//log
System.out.println(" Error : " + e.getMessage());
return false;
}
}
public void init()
{
commandConfig.add(CHECK_PIN);
commandConfig.add(ERROR);
commandConfig.add(CONNECT_GPRS);
commandConfig.add(OPEN_ECM_DATA_CONNECTION);
serial.addListener(new SerialDataListener() {
@Override
public void dataReceived(SerialDataEvent event) {
System.out.println("event data : " + event.getData());
semaphore.release();
}
});
Observable<String> observable = Observable.from(commandConfig);
observable.subscribe(new Action1<String>()
{
public void call(String command) {
System.out.println("command : " + command);
try {
semaphore.acquire();
serial.writeln(command);
System.out.println("test " + test);
test++;
} catch (InterruptedException ex) {
Logger.getLogger(Gsm.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
//Configuration of CDC-ECM channel
public void initialConfiguration() {
System.out.println("error");
commandConfig.add(ERROR);
System.out.println("connect gprs");
commandConfig.add(CONNECT_GPRS);
final String ultimoComando;
try {
serial.addListener(new SerialDataListener() {
@Override
public void dataReceived(SerialDataEvent event) {
System.out.println("event data : " + event.getData());
}
});
System.out.println("check pin");
serial.writeln(OPEN_ECM_DATA_CONNECTION);
} catch (Exception e) {
//log
System.out.println(" Error : " + e.getMessage());
}
}
}
调用的主要方法是:
public static void main(String[] args) {
Gsm gsm = new Gsm();
System.out.println("Empezamos la ejecución del programa");
if(gsm.connect())
{
System.out.println("connection successful");
gsm.init();
}
else
{
System.out.println("connection error");
}
//gsm.initialConfiguration();
}