WICG / serial

Serial ports API for the platform.
https://wicg.github.io/serial/
Other
255 stars 46 forks source link

A empty data package sent when web serial write data #168

Closed qcscofield closed 1 year ago

qcscofield commented 1 year ago

For example:

let chunk_size = 1024 * 1024
await device.open({baudRate: 115200, bufferSize: 1024 * 1024});
buffer = new Uint8Array(1024 * 1024 * 10)
let remainingBytes = buffer.byteLength
let i = 0
while (remainingBytes > 0) {
            let chunk = buffer.subarray(i * chunk_size, (i + 1) * chunk_size);
            let serialWriter = await device.writable.getWriter()
            serialWriter.write(chunk)
            await serialWriter.close()
            remainingBytes -= chunk.byteLength;
            i += 1;
}

10MB data loop 10 times to write. When writing 1MB of data, there is one empty packet for every 1KB sent. Do you know why ? image

demonguy commented 1 year ago

The empty packet cause exception from the device When device receive this empty packet, it will treat it as an invalid packet, and then return error.

devanlai commented 1 year ago

I'm pretty sure this is not specific to Web Serial.

IIRC, for USB CDC-ACM interfaces, zero-length packets are normal signaling mechanism and your application should be able to tolerate receiving them. And if your device is sending data, you may need to make sure that you send ZLPs as needed to avoid the case where it gets "stuck" when sending a multiple of the endpoint packet size.

It's been a while since I've reread the USB spec, so I'll leave it to other StackOverflow answers and online resources to go over the details.

reillyeon commented 1 year ago

That is correct. A ZLP is sent at the end of a transaction which has just sent a full packet. This is performed by the USB CDC-ACM driver and it out of the control of applications using the platform serial API, such as implementations of the Web Serial API.

demonguy commented 1 year ago

Well. I'm not expert on USB CDC-ACM. What i know is that Qualcomm's own flash tool and a native windows application developed by us can communicate with the serial port correct without such error. If it's USB CDC driver's problem. I cannot explain why other native windows application works well

reillyeon commented 1 year ago

Is the native Windows application writing chunks that are an exact multiple of the USB packet size (looks like 1024 bytes)?

demonguy commented 1 year ago

Well, after searching some document. I found Qualcomm firehose protocol have a configure option called "ZlpAwareHost=0". In Windows it should be 1. Which means, the device should handle the difference between Windows and Linux

Thanks very much. I found this solution because you mentioned "ZLP". So i just search it :)