我想從NodeJS控制Arduino。從NodeJS控制Arduino
我已經試過Duino,我得知該設備已準備就緒,調試器顯示命令已發送給Arduino,但沒有任何反應。
我也試過Johnny-Five,它表明設備已連接(在COM8上),但是這個事件永遠不會被觸發。
請幫忙! 謝謝..
我想從NodeJS控制Arduino。從NodeJS控制Arduino
我已經試過Duino,我得知該設備已準備就緒,調試器顯示命令已發送給Arduino,但沒有任何反應。
我也試過Johnny-Five,它表明設備已連接(在COM8上),但是這個事件永遠不會被觸發。
請幫忙! 謝謝..
我可能可以幫助你,你必須要更具體一點你真正想做什麼?
要讀取數據嗎?你想遠程控制它嗎?
編輯: 我也使用節點來控制一個Arduino,但我不使用杜伊諾也不約翰尼五,因爲沒有在我的項目適合。
相反,我在自己的電腦和機器人之間建立了自己的通信協議。
在Arduino上,代碼很簡單。它檢查串行是否可用,如果是,讀取並存儲緩衝區。使用switch
或if/else
那麼,我選擇我想我的機器人來執行動作(向前,向後移動,閃爍LED等)
進行通信需要通過發送bytes
,而不是人類可讀的行動。所以你需要做的第一件事就是設想兩者之間的小界面。 Bytes
是有用的,因爲在Arduino方面,你不需要任何轉換,它們與switch
很好地工作,而字符串則不是這種情況。
在Arduino的側面,你就會有這樣的事情:(請注意,你需要聲明DATA_HEADER
某處)
void readCommands(){
while(Serial.available() > 0){
// Read first byte of stream.
uint8_t numberOfActions;
uint8_t recievedByte = Serial.read();
// If first byte is equal to dataHeader, lets do
if(recievedByte == DATA_HEADER){
delay(10);
// Get the number of actions to execute
numberOfActions = Serial.read();
delay(10);
// Execute each actions
for (uint8_t i = 0 ; i < numberOfActions ; i++){
// Get action type
actionType = Serial.read();
if(actionType == 0x01){
// do you first action
}
else if(actionType == 0x02{
// do your second action
}
else if(actionType == 0x03){
// do your third action
}
}
}
}
}
在節點的一面,你就會有這樣的事情:(檢查serialport github欲瞭解更多信息)
var dataHeader = 0x0f, //beginning of the data stream, very useful if you intend to send a batch of actions
myFirstAction = 0x01,
mySecondAction = 0x02,
myThirdAction = 0x03;
sendCmdToArduino = function() {
sp.write(Buffer([dataHeader]));
sp.write(Buffer([0x03])); // this is the number of actions for the Arduino code
sp.write(Buffer([myFirstAction]));
sp.write(Buffer([mySecondAction]));
sp.write(Buffer([myThirdAction]));
}
希望它有幫助!
我只是想讓led閃爍,甚至熄滅或熄滅..只是什麼.. – HasanAboShally
幫助請..謝謝:) – HasanAboShally