CoderChris
Posts: 3
Joined: Sat Jul 13, 2019 8:05 pm

Serial vs. Ethernet

Thu Sep 12, 2019 4:42 pm

This is a sort of a networking and servers question but could also fit in another category.

I'm using the Pi Zero as a USB serial gadget and am sending a trigger command over the USB cable to action an event on the device.

This is fine but I was thinking/wondering:

If I convert it to an ethernet gadget instead and send my message over TCP/IP (not UDP) would that give me any deliverabilty benefits for the message (in terms of retries, if necessary etc.) without needing to do anything (the protocol picking up resends of the message, etc. if missed somehow by a service listening on the pizero).

Note:
I don't actually have a problem that I'm trying to fix.
The question is around the best way of ensuring reliable messaging with minimal effort when considering options between listing on a serial terminal vs. listening on a TCP socket.
I'm doing this in python so have a slight but not proven concern about message deliverability when the garbage collector runs on the pi zero (am concerned the loop listening on the serial terminal may miss the message if stalled by the gc or similar at some point...)

Heater
Posts: 13357
Joined: Tue Jul 17, 2012 3:02 pm

Re: Serial vs. Ethernet

Thu Sep 12, 2019 6:21 pm

Missing messages due to garbage collection delays or whatever should not be a concern. The Linux kernel will be buffering incoming data. I presume a Windows machine does the same. Assuming you are not expecting gigabytes to be buffered whist your listener application stutters.

In general it all depends on what you are doing. A network connection with TCP/IP can also lose data if the connectivity is interrupted. It's up to you to pay attention to any errors and take any necessary recovery action. You may want a higher level protocol at your application level to verify data has arrived correctly.

Serial line connectivity can be very reliable if you use a suitable protocol over it.

Currently I have a device sending 20 messages per second at 115200 bad over a short serial link to a Pi. The code receiving the messages is written in Javascript, so it has similar garbage collection delays as Python, The messages have check sums and sequence numbers. I have not seen any errors or missing messages over weeks of operation now. It it were to drop a message here and there it would not be a catastrophe so that is good enough for me.
Memory in C++ is a leaky abstraction .

ejolson
Posts: 3578
Joined: Tue Mar 18, 2014 11:47 am

Re: Serial vs. Ethernet

Fri Sep 13, 2019 3:04 am

Heater wrote:
Thu Sep 12, 2019 6:21 pm
Currently I have a device sending 20 messages per second at 115200 bad over a short serial link to a Pi.
I think the comparison is between the serial USB gadget and the Ethernet gadget when communicating with the Pi Zero, neither of which are bad. The Ethernet gadget provides a natural way to open many different connections to send different types of information; the serial link is simpler. I would expect both to be essentially error free.

hippy
Posts: 5964
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Serial vs. Ethernet

Fri Sep 13, 2019 10:02 am

Given both work over USB I wouldn't expect there to be any reliability issues whether ethernet or serial. I also wouldn't expect there to be any problems during garbage collection. Any issues when a program is stalled, not actually reading the data sent, I would expect to depend on how much data is queued before data starts being dropped and lost.

I have no idea how much data is queued in either case. If ethernet queues whole frames of data that could use more memory than queuing however many bytes were actually sent over serial, so ethernet could stop queueing earlier, and that could have wider effects beyond the program which is using that data.

I don't know. Perhaps the best way to find out is to test both.

CoderChris
Posts: 3
Joined: Sat Jul 13, 2019 8:05 pm

Re: Serial vs. Ethernet

Fri Sep 13, 2019 10:59 am

Thanks for your replies everyone (and in advance to anyone else with anything further to say on this).

The Ethernet gadget also gives me some other flexibility in my project so I'm going to test a move to that vs. the current way I'm doing things, but all feedback has been appreciated :)

Return to “Networking and servers”