3
votes

I have old DOS program which works with serial port. And I need to port it to linux. I've connected my serial device to linux via USB (using rs232 cable). It appears as /dev/ttyUSB0.

Can I use functions inb, outb (instead of DOS equivalent inp, outp) for reading or writing in device from my code? (inb and outb special function for lowlevel reading-writing bytes to specific port address). I don't understand how to determine proper port number.

Maybe there is another approach - for example open file '/dev/ttyUSB0'. But in this case I have a problem reading device registers such as modem control register (MCR) or line status register (LSR).

1
You should bettere describe how you going to work with device via serial line. You if you need to configure parity, number of bits and so on, you need just use open, read, write plus tcsetattr.fghj

1 Answers

1
votes

You can use inb/outb or mmap (eg. devmem, depends on architecture) for onboard standard serial port but it is not recommended in userland. You need root priviledges, you can cause conflict with kernel's driver and you have to be sure that your hardware (eg. 16C550) is full compatible with your code.

To trigger your program when serial port added can use udev rule or old hotplug. To determine port in case of USB serial port converter you can use USB bus id and port id. You can check the numbers via lsusb -t command.

  • serial port configuration: http://linux.die.net/man/3/termios
  • check if there are received data (LSR): select()
  • data inb / outb -> read() / write()
  • MSR (DCD, RI, CTS) / MCR (DTR, RTS if hardware flow control disabled) -> ioctl() or tty_ioctl

You can learn from picocom source code

Possible problem for realtime application: delays caused by USB bus polling and process/thread switching.