저는 Arduino 프로젝트를 통해 모터를 제어하고 센서를 읽습니다. 나는 라이브러리 (SerialPort와 SerialPort2) 중 하나를 사용하여 직렬 포트에서 브라우저로 읽고 쓸 수있는 중간 채널로 Node.js을 사용하여 웹보기를 사용하기로 결정했습니다.Arduino 무선 및 Node.js
Arduino를 전선을 사용하여 USB 장치에 직접 연결할 때 두 가지 모두 제대로 작동하지만 Arduino를 내 무선 어댑터 ** (APC220)를 통해 USB 장치에 연결할 때 Node.js에서 아무 것도 읽지 못하는 것 같습니다.) Arduino 직렬 모니터를 사용하여받은 모든 것을 읽을 수는 있지만.
그 뒤에 가능한 모든 이유를 확인했습니다. Wire2 직렬 및 APC220 및 브리지 커넥터 (USB- 직렬 변환기)를 사용하여 Arduino 통신에 사용하는 전송 속도를 확인했습니다. 9600 보오율, 패리티/흐름 제어 없음, 데이터 비트 : 8, 정지 비트 : 1
동작은 다음과 같습니다. COM port에 문제없이 연결하고 오류 인쇄를 시도했지만 SerialPort 라이브러리로 식별 할 수없는 것으로 보입니다. 그러면 이벤트 (데이터)가 읽히지 않습니다. 즉, 노드 (열려있는 경우에도 Node.js)와 직렬 포트가 상호 작용하지 않습니다.
참고 :다른 Arduino를 USB 포트와 무선 어댑터 사이의 매체로 사용할 수는 있지만이 문제를 이해하고 이러한 해결 방법을 사용하지 않고 깨끗하게 해결하고 싶습니다.
무엇이 문제 일 수 있습니까?
서버 [Node.js를] : 모니터는 다른 프로그램을 실행하지
var SerialPort = require('serialport2').SerialPort;
var portName = 'COM15';
var io = require('socket.io').listen(8000); // Server listens for socket.io communication at port 8000
io.set('log level', 1); // Disables debugging. This is optional. You may remove it if desired.
var sp = new SerialPort(); // Instantiate the serial port.
sp.open(portName, { // portName is instatiated to be COM3, replace as necessary
baudRate: 9600, // This is synchronised to what was set for the Arduino code
dataBits: 8, // This is the default for Arduino serial communication
parity: 'none', // This is the default for Arduino serial communication
stopBits: 1, // This is the default for Arduino serial communication
flowControl: false // This is the default for Arduino serial communication
});
io.sockets.on('connection', function (socket) {
// If socket.io receives message from the client browser then
// this call back will be executed.
socket.on('message', function (msg) {
console.log(msg);
});
// If a web browser disconnects from Socket.IO then this callback is called.
socket.on('disconnect', function() {
console.log('disconnected');
});
});
var cleanData = ''; // This stores the clean data
var readData = ''; // This stores the buffer
sp.on('data', function (data) { // Call back when data is received
readData = data.toString(); // Append data to buffer.
// If the letters '[' and ']' are found on the buffer then isolate what's in the middle
// as clean data. Then clear the buffer.
console.log(readData); // **Here you should be able to print the data if you receive any**
if (readData.indexOf(']') >= 0 && readData.indexOf('[') >= 0) {
cleanData = readData.substring(readData.indexOf('[') + 1, readData.indexOf(']'));
readData = '';
console.log("-- "+cleanData);
io.sockets.emit('message', cleanData);
}else if(readData.indexOf('[') >= 0){
cleanData = readData.substring(readData.indexOf('[') + 1, readData.length);
readData = '';
}else if(readData.indexOf(']') >= 0){
cleanData += readData.substring(0, readData.indexOf(']'));
readData = '';
console.log("-- "+cleanData);
io.sockets.emit('message', cleanData);
}else{
cleanData += readData;
readData = '';
}
//console.log(readData);
//io.sockets.emit('message', readData);
});
서버와 동시에 직렬 모니터를 사용하고 있지 않습니다. 충돌 (다른 응용 프로그램이 점유/사용)을 유발할 수 있습니다. 좋은 생각인데, 곧 상세한 직렬 포트 판독기로 패킷을 분석하려고 노력할 것입니다. 하지만 변환기의 설정을 이미 확인했는데 정상입니다. 사실 직렬 모니터와 java의 직렬 리더 (다른 Java 프로젝트에서 사용)에서 작업 중이므로 문제는 노드 js의 직렬 포트 라이브러리에 있다는 것을 의미합니다. 아마도 분석 결과에 따라 약간의 조정이 필요할 것입니다. 귀하의 의견에 진심으로 감사 드리며, – CME64
을 주시면 자바 스크립트에서 작성한 코드를 제공하여 시리얼 포트를 열어 해당 부분에 도움을 주어야합니다. 어떤 라이브러리를 사용 했습니까? 직렬 회선을 어떻게여십시오? 올바른 termios 설정을 수행합니까? 가장 간단한 프로그램을 arduino에서 호스트 및 백으로 "Hello world"를 보낼 수 있도록하려고 했습니까? – zmo
@zmo 코드가 추가되었지만 그렇지 않은 경우 내 질문이 복잡해 보일 것이라고 생각합니다. 와이어를 통해 작동하지만 코드를 잘못 입력 한 것은 없지만 무선을 사용할 때 모든 직렬 포트 판독기 (예 : java, arduino ..)에서 작동합니다. 노드 js 직렬 포트 라이브러리 이외입니다.이를 통해 이동해야하는 경우 bruteforce 솔루션과 그 시간이 걸릴 것입니다. 그것이 내가 질문을 게시 한 이유입니다. – CME64