This works on a Pi4 and does not use a USB-USB cable to connect PC and Pi (which can cause conflicts between the power supplies). It uses a USB to USB-C cable to connect a PC USB port to the Pi's power connector. So the PC provides power to the Pi and a serial connection can be set up as follows. The PC sees the Pi as a COM port.
On the Pi:
Windows code. Use Device Manager to find the COM port number.
On the Pi:
Code:
In /boot or /boot/firmwareAdd to config.txt dtoverlay=dwc2Add to cmdline.txt modules-load=g_serialCreates a serial port file: /dev/ttyGS0C code: instream = fopen("/dev/ttyGS0","r"); outstream = fopen("/dev/ttyGS0","w"); fread(....,instream); fwrite(....,outstream); Windows code. Use Device Manager to find the COM port number.
Code:
HANDLE hCom; COMMTIMEOUTS cto; BOOL retval; int comport,nread,nwrit,count; unsigned char buf[16]; static char serport[16] = {"\\\\.\\COMxx"}; // sets to \\.\COMxx // set up client with COM port number = comport comport = 3; // for example COM3 // replace xx in serport[] with COM port number if(comport < 10) { serport[7] = (char)(comport + '0'); serport[8] = 0; } else { serport[7] = (char)((comport/10) + '0'); serport[8] = (char)((comport%10) + '0'); serport[9] = 0; } // CreateFile connects to Pi hCom = CreateFile( serport, GENERIC_READ | GENERIC_WRITE, 0, // exclusive-access NULL, // no security attributes OPEN_EXISTING, 0, // 0=not overlapped I/O or FILE_FLAG_OVERLAPPED NULL // hTemplate ); if(hCom == INVALID_HANDLE_VALUE) // CreateFile error return(0); // set time outs cto.ReadIntervalTimeout = 5; cto.ReadTotalTimeoutMultiplier=5; cto.ReadTotalTimeoutConstant=5; cto.WriteTotalTimeoutMultiplier=5; cto.WriteTotalTimeoutConstant=5; SetCommTimeouts(hCom,&cto); // hCom is now an open connection to Pi // Read/write serial data ReadFile(hCom,buf,count,&nread,NULL); WriteFile(hCom,buf,count,&nwrit,NULL); // disconnect CloseHandle(hCom);Statistics: Posted by petzval — Tue Oct 22, 2024 10:26 am