0

I have a software that reads serial data (in coming bytes for sensor reading) from USB port. For simulation purpose I need to write a python script that continuous writes bytes to the USB port simulating a USB device. I require that the /dev/serial/by_id to be created as the software reads the data from that file itself. I cannot edit the software. How can I make this work. I read through the pyserial documentation but I could not figure it out

Requirements:

  1. the serial port is accessible at /dev/serial/file
  2. it is listed by serial.tools.list_ports.
  • Did you try https://stackoverflow.com/a/15095449/1581658 ? – SamBob Nov 08 '21 at 12:20
  • I did try. But its doesn't create the files /dev/serial/by_id or /by_path which is necessary for my software. – Nilesh Hampiholi Nov 08 '21 at 12:23
  • It does indeed create a device file indicated by `s_name` whilst running (it is closed when the program exits) Is this not created for you? Or are you having trouble using threading to keep that file open? Or do you need your file to be in a specific location (in which case you could use socat as in here: https://stackoverflow.com/a/37450382/1581658) or do you need a raw USB device simulated, not a serial device? In which case what are you opening that device with in your software under test? – SamBob Nov 08 '21 at 12:44
  • 1. No the s_name is not being created. 2. The software reads from the temp file /dev/serial/by_id/ "some_file_name" 3. I need to simulate a serial device. Lets say I have a sensor reading data continuously and that sensor reading is sampled and converted to bytes and sent to the serial usb port of my PC. And software takes it on form there So this sensor reading sampling and sending it to port is to be simulated. for simplicity lets just consider sampled data as zeros This how do I write them to serial port such that a external serial device is detected (without connecting device) . – Nilesh Hampiholi Nov 09 '21 at 12:26
  • "No the s_name is not being created". What did you do to verify that? If you `print(s_name)` inside the linked solution, what does it say? (it should be `/dev/pts/number` - that file is your simulated serial device). Similarly if you put `time.sleep(60)` at the end of that solution to prevent exit for a minute and run `ls /dev/pts` you should see a newly created file in there. That is your simulated device. What is your output for these two tests? – SamBob Nov 09 '21 at 12:57
  • Yes both of these tests run accurately. But when I do python -m serial.tools.list_ports s_name doesnt show up. And I need that file specifically in /dev/serial/file – Nilesh Hampiholi Nov 12 '21 at 10:34
  • Please add these new requirements to your question. Namely that 1) the serial port is accessible at `/dev/serial/file` and 2) it is listed by `serial.tools.list_ports` Note these are mutually exclusive as `serial.tools.list_ports` only searches for certain files inside `/dev` (see https://github.com/pyserial/pyserial/blob/master/serial/tools/list_ports_linux.py#L91) and not `/dev/serial/` so the likely solution is to create symlinks for both of those from the `pts` created by `openpty`. – SamBob Nov 12 '21 at 12:30
  • Here is another question that has a better description of my problem.(https://stackoverflow.com/questions/38399602/emulate-a-uart-like-piece-of-hardware-in-linux ) – Nilesh Hampiholi Nov 15 '21 at 12:36

0 Answers0