0

I have a PCI card with a db25 port. This in turn connects to two devices with a de9 port. I need to talk to these devices using rs485. I'm working on a C++11 library that currently creates char arrays for the protocol commands.

I would like to put these arrays into an output stream and read the response from an input stream. I have a feeling that this is a possibility, but I'm not sure how. The current questions that I have are as follows:

  1. Should I use termios? How do I create a stream with termios?
  2. Should I use an fstream to open /dev/tty*? How would I set values like the baud rate with fstream?
  3. Is there another cleaner option?

I tried googling for a while but I didn't come up with any answers.

EDIT:

Someone in the office pointed me to boost asio: https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/reference/serial_port.html

There are stream implementations possible with it: https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/overview/serial_ports.html

https://github.com/fedetft/serial-port/blob/master/6_stream/serialstream.cpp

More detailed explanation: https://gist.github.com/kaliatech/427d57cb1a8e9a8815894413be337cf9

I got the settings that I need from: https://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html (the non canonical one seemd to work best). The termios version that I made worked and am currently converting it to boost/asio

I will try to report back if it works

Typhaon
  • 828
  • 8
  • 27
  • 1
    A device such as **/dev/tty*** refers to a serial terminal, not a stream. See [Serial Programming Guide for POSIX Operating Systems](http://www.cmrr.umn.edu/~strupp/serial.html) Unless your system has full hardware support for half-duplex RS-485, then you'll have difficulties. See https://stackoverflow.com/questions/25250731/automatically-changing-rts-for-rs-485-communication – sawdust Oct 19 '22 at 22:43
  • Yes it actually does. I'm working with a PCI card specifically for RS485 – Typhaon Oct 20 '22 at 07:54
  • 1
    I was referring to more than just an RS-485 transceiver. Before you rejoice, check the Linux driver for the UART used on the PCI card that you have. Odds are that the RS-485 **ioctl** is not implemented. BTW that HOWTO guide you mentioned has low quality code that is not portable. You've been warned. – sawdust Oct 20 '22 at 08:05
  • @sawdust What do you mean with low quality code that is not portable? What is wrong with it? I'm just following an internship and I'd love to learn! – Typhaon Oct 20 '22 at 08:26
  • 1
    See [Setting Terminal Modes Properly](https://www.gnu.org/software/libc/manual/html_node/Setting-Modes.html) – sawdust Oct 20 '22 at 17:05
  • A you meant using tcgetattr right? Thanks for letting me know! What I currently do is creating two termios objects, oldtio and newtio. Run tcgetattr(fd, &oldtio). And then run memcpy(&newtio, &oldtio, sizeof(termios)) sop I can clean it up nicely aftere I'm done. – Typhaon Oct 21 '22 at 08:48
  • For future reference and for anyone having the same problem: The examples in https://gist.github.com/kaliatech/427d57cb1a8e9a8815894413be337cf9 seem to be working. As @sawdust pointed out, there are some problems in https://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html . Using termios is OS specific, see: https://www.gnu.org/software/libc/manual/html_node/Setting-Modes.html – Typhaon Oct 21 '22 at 08:52

0 Answers0