-1

I have multiple Victron Energy devices that communicate through a serial port spitting out information every second. (Each Victron device communicates on a separate serial port)

What I want to do is grab the data from each device and put it combined all the data into just one json string (I have a library for that)

The only way I could think of doing it is set a variable in the class saying when finished and then check if all objects have the finished flag and then do something then.

Trevin Corkery
  • 653
  • 1
  • 6
  • 12
  • Do all the device's output come into the same serial port or are there multiple serial ports? – Jerry Jeremiah Jul 18 '19 at 04:31
  • There are multiple serial ports, for example /dev/ttyUSB0 and /dev/ttyUSB1 – Trevin Corkery Jul 18 '19 at 04:32
  • Looking Unix-y. I'm thinking a `select` statement and a bitmask to show which have and have not completed a message in the current reporting cycle. That said, you're asking the question too soon and have provided few details to work with. – user4581301 Jul 18 '19 at 04:45
  • What are you going to use the JSON string for, by the way? – user4581301 Jul 18 '19 at 04:48
  • My plan was to also host a tcp server and send the JSON string to all clients connected. – Trevin Corkery Jul 18 '19 at 04:53
  • Groovy. Just checking. We had someone come through earlier intending to use JSON as their internal data representation, and I wanted to talk you out of it as soon as possible if that's what you had in mind. – user4581301 Jul 18 '19 at 05:01

1 Answers1

0

You probably need to make separate thread for each serial port which would read data and then:

  • either just set flag that you have data, if you don't care that this data can be overwritten by subsequent read from port before processing
  • or add data to this port queue

In main thread you wait either for all flags to be set or all queues to be non-empty then process data.

sklott
  • 2,634
  • 6
  • 17