erjdriver
Posts: 64
Joined: Wed Jun 23, 2021 1:58 am

Getting WIFI and USB serial port to work on Pi0W

Mon Jun 27, 2022 8:48 pm

I've got a PI zero and and soon as it boots up - I can ssh to it via WIFI. So I assume the ethernet gadget is setup.

I then plug in the USB cable (not power) to my PC's usb port - hoping to see a serial port - I don't see it on the PC's devices list.

Here's what I'm hoping to achieve - ssh via WIFI for development and the usb wired serial port for commuication between my zero and the pc server program I'm writing.

I know I edited the config files long time ago - but can't remember exactly what I did.

User avatar
HawaiianPi
Posts: 7431
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Getting WIFI and USB serial port to work on Pi0W

Mon Jun 27, 2022 9:10 pm

erjdriver wrote:
Mon Jun 27, 2022 8:48 pm
I've got a PI zero and and soon as it boots up - I can ssh to it via WIFI. So I assume the ethernet gadget is setup.
Not sure why you would assume that, since SSH over WiFi has nothing to do with Ethernet gadget mode. Most people use Ethernet gadget mode to SSH into a Pi Zero without a wireless connection.

erjdriver wrote:
Mon Jun 27, 2022 8:48 pm
I then plug in the USB cable (not power) to my PC's usb port - hoping to see a serial port - I don't see it on the PC's devices list.
That's confusing... :?

Are you trying to create a serial gadget, or an Ethernet gadget (they are not the same thing).

erjdriver wrote:
Mon Jun 27, 2022 8:48 pm
Here's what I'm hoping to achieve - ssh via WIFI for development and the usb wired serial port for commuication between my zero and the pc server program I'm writing.
Not sure why you'd need both, but okay.
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups, and where is that annoying music coming from?

erjdriver
Posts: 64
Joined: Wed Jun 23, 2021 1:58 am

Re: Getting WIFI and USB serial port to work on Pi0W

Mon Jun 27, 2022 9:52 pm

Apologies for my lack of clarity and also ignorance on this matter...let me be clearer.

I'm writing a client program on the zero and the server program on the PC and having them communicate via the usb serial port...if I can.

I'd like the above and yet at the same time be able to develop the client program via ssh from the PC. I'm assuming that if I ssh over usb - then there won't be a serial port to be opened by my programs.

Right now, ssh is working via WIFI - that is I can connect with no usb cable connection.

That's fine by me...I can use it for development.

Now just need a serial port when I plug the usb cable in.

Normally when you connect the cable - you see the serial port in Windows Device Manager - I don't see it.

Hope I made it a little bit clearer.

User avatar
thagrol
Posts: 7931
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: Getting WIFI and USB serial port to work on Pi0W

Mon Jun 27, 2022 10:35 pm

OK, so by default the USB port on a zeroW works in host only mode so when connected to a PC's USB port no data/device is detected.

Here's ther quick and dirty way to get it to act as a USB serial device:
  1. Add the following to /boot/config.txt:

    Code: Select all

    dtoverlay=dwc2
  2. Add the following to the end of /boot/cmdline.txt ensuring every thing remains on a single line:

    Code: Select all

     modules-load=g_serial
  3. Reboot
You should now see a /dev/ttyGS0 on the Pi and a new whatever your OS calls it serial port on the PC.

Editied for typos
Last edited by thagrol on Mon Jun 27, 2022 11:23 pm, edited 1 time in total.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
thagrol
Posts: 7931
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: Getting WIFI and USB serial port to work on Pi0W

Mon Jun 27, 2022 10:36 pm

erjdriver wrote:
Mon Jun 27, 2022 9:52 pm
Normally when you connect the cable - you see the serial port in Windows Device Manager - I don't see it.
Nope. Not unless your OS is non-standard or has been tweaked.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
thagrol
Posts: 7931
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: Getting WIFI and USB serial port to work on Pi0W

Mon Jun 27, 2022 10:43 pm

erjdriver wrote:
Mon Jun 27, 2022 9:52 pm
I'd like the above and yet at the same time be able to develop the client program via ssh from the PC. I'm assuming that if I ssh over usb - then there won't be a serial port to be opened by my programs.
Should have replied in one post not in pieces. Oh well.

You can get that USB connection to act as both serial and network (ethernet) between Pi and USB host. See USB Ethernet Gadget A Beginner's Guide section 6.1.3. With a little modification, it may be possible to get two serial ports via the configfs method.

ssh doesn't work over serial.* You can log in over serial if you start a getty on that serial port** but if you do so you can't use that port to communicate between your programs at the same time.

*: It can if you run a PPP or SLIP connection over the serial ports but that's a whole other can of worms.
**: Ask your favourite web search engine.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

erjdriver
Posts: 64
Joined: Wed Jun 23, 2021 1:58 am

Re: Getting WIFI and USB serial port to work on Pi0W

Mon Jun 27, 2022 10:48 pm

thagrol wrote:
Mon Jun 27, 2022 10:35 pm
OK, so by default the USB port on a zeroW works in host only mode so when connected to a PC's USB port no data/device is detected.
I've got `dtoverlay=dwc2` at the end of my config.txt file.

This is the current cmdline.txt file - 1 line

`console=serial0,115200 console=tty1 root=PARTUUID=775f7f6a-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether silent quiet splash loglevel=0 logo.nologo`

So I just add SPACE g_serial to the end or after 'splash'???

Note there's already a g_ether in there.

UPDATE: I added g_serial between 'splash' and 'loglevel' and rebooted pi and I don't see the serial port on the PC.

Note still ssh connection is via WIFI.

User avatar
thagrol
Posts: 7931
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: Getting WIFI and USB serial port to work on Pi0W

Mon Jun 27, 2022 11:12 pm

erjdriver wrote:
Mon Jun 27, 2022 10:48 pm
thagrol wrote:
Mon Jun 27, 2022 10:35 pm
OK, so by default the USB port on a zeroW works in host only mode so when connected to a PC's USB port no data/device is detected.
I've got `dtoverlay=dwc2` at the end of my config.txt file.

This is the current cmdline.txt file - 1 line

`console=serial0,115200 console=tty1 root=PARTUUID=775f7f6a-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_ether silent quiet splash loglevel=0 logo.nologo`

So I just add SPACE g_serial to the end or after 'splash'???

Note there's already a g_ether in there.
Nope. You can only run one g_* module at a time. Use g_cdc instead of g_ether. g_cdc provides both ethernet and serial USB devices.

This is explained in the guide I linked to.

I'm confused and curious: why are you asking about WiFI and serial if you're currently using g_ether? They are completely unrelated and both can provide a network connection with ssh to the PC.

My answer was based on your indication of WiFi with serial over USB not getting both a USB ethernet gadget and USB serial gadget working together.

Incidentally, if you already have the ethernet gadget working, why not use sockets for the data connection rather than serial? That way you only need g_ether and, just like any other network connection, can run multiple streams/applications/connections over it.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

erjdriver
Posts: 64
Joined: Wed Jun 23, 2021 1:58 am

Re: Getting WIFI and USB serial port to work on Pi0W

Tue Jun 28, 2022 12:03 am

Ok I changed the modules-list to g_cdc and removed g_ether and g_serial

Now I see a new device CDC Composite Gadget on Windows - but looks like it's missing drivers - no serial port available.


Re why I'm using serial port instead of ethernet/tcp - 1. I already wrote the server code for serial i/o - but that's not a huge issue - mainly I'm thinking serial i/o will be faster than tcp/ip and more reliable than udp. My program sends text lines back and forth.

Since I already have WIFI working and don't need g_ether for wired SSH - can I just change from g_cdc to g_serial and get the serial port working - or will I still need a driver.

Once again my apologies for my lack of knowledge of the pi/linux etc.

User avatar
thagrol
Posts: 7931
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK

Re: Getting WIFI and USB serial port to work on Pi0W

Tue Jun 28, 2022 9:36 am

erjdriver wrote:
Tue Jun 28, 2022 12:03 am
Ok I changed the modules-list to g_cdc and removed g_ether and g_serial

Now I see a new device CDC Composite Gadget on Windows - but looks like it's missing drivers - no serial port available.
Hmm. That merits further investigation. When I have the time.
Re why I'm using serial port instead of ethernet/tcp - 1. I already wrote the server code for serial i/o - but that's not a huge issue - mainly I'm thinking serial i/o will be faster than tcp/ip and more reliable than udp. My program sends text lines back and forth.
A network connection is at least an order of magnitude (Mb/s rather than Kb/s faster than serial.
Since I already have WIFI working and don't need g_ether for wired SSH - can I just change from g_cdc to g_serial and get the serial port working - or will I still need a driver.
You shouldn't need a driver for g_serial (I didn't) but if you do, I'd expect the internet to provide.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

erjdriver
Posts: 64
Joined: Wed Jun 23, 2021 1:58 am

Re: Getting WIFI and USB serial port to work on Pi0W

Tue Jun 28, 2022 9:03 pm

The g_serial port worked as advertised.

I had to install pyserial on the zero - but the same serial code that I wrote for the server - works for the client. Which is a plus!


I'll proceed as is with serial# - but will fall back on the tcp transport if it turns out to be too slow.

Thanks much for your help.

Return to “Advanced users”