英文:
Microbit truncates messages received from LoRa Reyax RYLR890/896 module
问题
我已经设置了一个MicroBit作为LoRa接收器,使用了Reyax RYLR890/896的LoRa模块。这些LoRa模块通过串口使用AT命令接收/发送信息。
在发射端,我有一个ESP32,我在Arduino框架中用C编程了它。它连接到一个Reyax RYLR890模块,并使用'AT+SEND'命令每隔几秒发送一条消息。我已经验证了它与另一个设置为接收器的Reyax RYLR890模块正确传输的方式:
- 连接到我的Macbook的USB端口,通过FTDI适配器将UART和USB串口协议进行转换。我使用CoolTerm串口监视器来查看接收到的消息。
- 连接到另一个ESP32,连接到我的Macbook的USB端口。我可以在PlatformIO的串口监视器中看到接收到的消息。
这两种情况下,传输都被正确发送和接收。
现在我在Microbit上设置了两个引脚(14、15)用于串口重定向。我能够接收到传输,但由于某种原因它们被截断了。我只看到"Hello W"而不是"Hello World"。这是我的代码:
let preamble = ""
let comma_first = 0
let generic_message = ""
let comma_second = 0
let sizeTx = 0
let payload = ""
let messageRx = ""
input.onButtonPressed(Button.A, function () {
serial.redirect(
SerialPin.P15,
SerialPin.P14,
BaudRate.BaudRate9600
)
basic.showIcon(IconNames.Yes)
})
function parseRx (message: string) {
// message: "+RCV=120,11,HELLO WORLD,-99,40\n"
preamble = message.substr(1, 3)
if (preamble == "RCV") { // Check
// Play tone
music.playTone(262, music.beat(BeatFraction.Whole))
// Find 1st comma
comma_first = message.indexOf(",")
// Get "11,HELLO WORLD,-99,40\n"
generic_message = message.substr(comma_first + 1, message.length - comma_first)
// Find next comma
comma_second = generic_message.indexOf(",")
// Extract size of payload and use it to get payload
sizeTx = parseFloat(generic_message.substr(0, comma_second)) // 11
payload = generic_message.substr(comma_second + 1, sizeTx) // HELLO WORLD
// Display
basic.showString("" + (payload.length))
if (payload.includes("WOR")) { // FAILS!! HELLO W
basic.showIcon(IconNames.SmallHeart)
}
}
}
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
messageRx = serial.readUntil(serial.delimiters(Delimiters.NewLine))
parseRx(messageRx)
basic.clearScreen()
basic.showIcon(IconNames.Happy)
basic.clearScreen()
})
我感激任何帮助解决这个问题的帮助。
英文:
I have set up a MicroBit as a LoRa receiver using a LoRa module from Reyax RYLR890/896. These LoRa modules receive/transmit information with AT commands over the serial port.
On the transmitter, I have an ESP32 that I have programmed in C in the Arduino framework. It is wired to a Reyax RYLR890 module and transmits a message every few seconds with an 'AT+SEND' command. I have verified that it transmits correctly with another Reyax RYLR890 module set up as receiver in the following ways:
- Hooked up to my Macbook's USB port via FTDI adapter to convert between UART and USB serial protocols. I use the CoolTerm serial monitor to see the received messages.
- Wired to another ESP32 connected my Macbook's USB port. I can see the received messages in the serial monitor in PlatformIO.
The transmissions are correctly sent and received in both cases.
Now I set up reception on a Microbit using two pins (14, 15) for serial redirect. I am able to receive transmissions but they are truncated for some reason. Instead of "Hello World", I only see "Hello W". Here is my code:
let preamble = ""
let comma_first = 0
let generic_message = ""
let comma_second = 0
let sizeTx = 0
let payload = ""
let messageRx = ""
input.onButtonPressed(Button.A, function () {
serial.redirect(
SerialPin.P15,
SerialPin.P14,
BaudRate.BaudRate9600
)
basic.showIcon(IconNames.Yes)
})
function parseRx (message: string) {
// message: "+RCV=120,11,HELLO WORLD,-99,40\n"
preamble = message.substr(1, 3)
if (preamble == "RCV") { // Check
// Play tone
music.playTone(262, music.beat(BeatFraction.Whole))
// Find 1st comma
comma_first = message.indexOf(",")
// Get "11,HELLO WORLD,-99,40\n"
generic_message = message.substr(comma_first + 1, message.length - comma_first)
// Find next comma
comma_second = generic_message.indexOf(",")
// Extract size of payload and use it to get payload
sizeTx = parseFloat(generic_message.substr(0, comma_second)) // 11
payload = generic_message.substr(comma_second + 1, sizeTx) // HELLO WORLD
// Display
basic.showString("" + (payload.length))
if (payload.includes("WOR")) { // FAILS!! HELLO W
basic.showIcon(IconNames.SmallHeart)
}
}
}
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
messageRx = serial.readUntil(serial.delimiters(Delimiters.NewLine))
parseRx(messageRx)
basic.clearScreen()
basic.showIcon(IconNames.Happy)
basic.clearScreen()
})
I appreciate any help figuring this out.
答案1
得分: 2
以下是您要翻译的内容:
原文:
It turns out that the size of the serial buffer was the issue that was the root cause of incoming radio transmissions being truncated. The default size is 20 Bytes (here) and triggers an event when 20 Bytes are rec'd (here).
翻译:
原来串行缓冲区的大小是导致无线电传输被截断的根本原因。默认大小为20字节(此处)并在接收到20字节时触发事件(此处)。
原文:
The fix turned out to be to increase the buffer size. This additional line of code serial.setRxBufferSize(128)
did the job. Ref. docs.
翻译:
解决方法是增加缓冲区大小。这一额外的代码行 serial.setRxBufferSize(128)
完成了任务。参考文档 docs。
原文:
let preamble = ""
let comma_first = 0
let generic_message = ""
let comma_second = 0
let sizeTx = 0
let payload = ""
let messageRx = ""
input.onButtonPressed(Button.A, function () {
serial.redirect(
SerialPin.P15,
SerialPin.P14,
BaudRate.BaudRate9600
)
serial.setRxBufferSize(128)
basic.showIcon(IconNames.Yes)
})
function parseRx (message: string) {
preamble = message.substr(1, 3)
if (preamble == "RCV") {
music.playTone(988, music.beat(BeatFraction.Whole))
comma_first = message.indexOf(",")
generic_message = message.substr(comma_first + 1, message.length - comma_first)
comma_second = generic_message.indexOf(",")
sizeTx = parseFloat(generic_message.substr(0, comma_second))
payload = generic_message.substr(comma_second + 1, sizeTx)
basic.showString("" + (payload.length))
if (payload.includes("WOR")) {
basic.showIcon(IconNames.SmallHeart)
}
}
}
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
messageRx = serial.readUntil(serial.delimiters(Delimiters.NewLine))
parseRx(messageRx)
basic.clearScreen()
basic.showIcon(IconNames.Happy)
basic.clearScreen()
})
翻译:
let preamble = ""
let comma_first = 0
let generic_message = ""
let comma_second = 0
let sizeTx = 0
let payload = ""
let messageRx = ""
input.onButtonPressed(Button.A, function () {
serial.redirect(
SerialPin.P15,
SerialPin.P14,
BaudRate.BaudRate9600
)
serial.setRxBufferSize(128)
basic.showIcon(IconNames.Yes)
})
function parseRx (message: string) {
preamble = message.substr(1, 3)
if (preamble == "RCV") {
music.playTone(988, music.beat(BeatFraction.Whole))
comma_first = message.indexOf(",")
generic_message = message.substr(comma_first + 1, message.length - comma_first)
comma_second = generic_message.indexOf(",")
sizeTx = parseFloat(generic_message.substr(0, comma_second))
payload = generic_message.substr(comma_second + 1, sizeTx)
basic.showString("" + (payload.length))
if (payload.includes("WOR")) {
basic.showIcon(IconNames.SmallHeart)
}
}
}
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
messageRx = serial.readUntil(serial.delimiters(Delimiters.NewLine))
parseRx(messageRx)
basic.clearScreen()
basic.showIcon(IconNames.Happy)
basic.clearScreen()
})
请注意,我已将原始文本中的HTML实体编码(例如"
)还原为常规的引号。
英文:
It turns out that the size of the serial buffer was the issue that was the root cause of incoming radio transmissions being truncated. The default size is 20 Bytes (here) and triggers an event when 20 Bytes are rec'd (here).
The fix turned out to be to increase the buffer size. This additional line of code serial.setRxBufferSize(128)
did the job. Ref. docs.
let preamble = ""
let comma_first = 0
let generic_message = ""
let comma_second = 0
let sizeTx = 0
let payload = ""
let messageRx = ""
input.onButtonPressed(Button.A, function () {
serial.redirect(
SerialPin.P15,
SerialPin.P14,
BaudRate.BaudRate9600
)
serial.setRxBufferSize(128)
basic.showIcon(IconNames.Yes)
})
function parseRx (message: string) {
preamble = message.substr(1, 3)
if (preamble == "RCV") {
music.playTone(988, music.beat(BeatFraction.Whole))
comma_first = message.indexOf(",")
generic_message = message.substr(comma_first + 1, message.length - comma_first)
comma_second = generic_message.indexOf(",")
sizeTx = parseFloat(generic_message.substr(0, comma_second))
payload = generic_message.substr(comma_second + 1, sizeTx)
basic.showString("" + (payload.length))
if (payload.includes("WOR")) {
basic.showIcon(IconNames.SmallHeart)
}
}
}
serial.onDataReceived(serial.delimiters(Delimiters.NewLine), function () {
messageRx = serial.readUntil(serial.delimiters(Delimiters.NewLine))
parseRx(messageRx)
basic.clearScreen()
basic.showIcon(IconNames.Happy)
basic.clearScreen()
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论