用nodejs和socketio从arduino读取数据
Reading data from arduino with nodejs and socketio
我有这个简单的代码,我运行在arduino
char inp;
void setup(){
Serial.begin(9600);
}
void loop(){
while(Serial.available()>0){
inp = Serial.read();
Serial.print(inp);
}
Serial.print("--");
delay(200);
}
它不停地写"——"如果我向arduino发送一些东西它会回复这个
在js文件中有
var strinInfo = "";
tempSerial.on("data", function(data) {
console.log("data -> " + data);
if (stringInfo.length < 37){
stringInfo += data;
}else{
io.sockets.emit("message", stringInfo);
stringInfo = ""
}
});
通过socket发送我从arduino得到的东西。我的问题是,例如,如果我发送
"thisisanunusuallongandterriblestringofsymbolsblablablablablablabla"
缺少字符:
---------thisisanunusuallongandterribles,
gofsymbolsblablablablabla--blabla ----,
-------------------------------------,
在这个例子中,我缺少"trin"。有没有一种不失去这些角色的方法?
我从node开始,所以我的猜测是,在它发出消息的时间之间…从那一刻起的内容就消失了,也许我错了。
查看您的代码,它可能是两件事。
- 您可能在侦听器准备好之前发送数据,下面的代码库已经解决了这个问题。
- 下面这行让我担心
if (stringInfo.length < 37){
。如果您获得连续的小数据包,则使您超过37的数据包将只打印stringInfo
而不打印data
部分。
连接到Arduino的lib:
-
[Johnny Five][1]
-
[Firmata][2]
两个程序都与Arduinos交互。Firmata是低级别的,johnny - 5在上面运行。
我认为这可能是baud-rate
的问题。
如果您使用serialport
模块与Arduino进行串行通信,则必须将serialport baud-rate
设置为9600(与接收端相同,即Arduino)。
串口波特率默认为115200。
相关文章:
- 从桌面读取python文件时高亮显示代码
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- NodeJS-readline暂停和恢复事件发射器(逐行读取)
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 如何在Javascript中读取unsigned int
- 如何读取表单中的输入数据
- 使用johnny-five和arduino读取输入引脚
- 使用 Python 从 Arduino 读取串行数据
- 用nodejs和socketio从arduino读取数据
- 从模拟端口读取时从Arduino串行接收到的错误数据
- 是否可以通过EthernetShield连接Arduino来读取文本文件?