3

I have 5 different games written in python that run on a raspberry pi. Each game needs to pass data in and out to a controller using a serial connection. The games get called by some other code (written in nodeJS) that lets the user select any of the games.

I'm thinking I don't want to open and close a serial port every time I start and finish a game. Is there anyway to make a serial object instance "global", open it once, and then access it from multiple game modules, all of which can open and close at will?

I see that if I make a module which assigns a Serial object to a variable (using PySerial) I can access that variable from any module that goes on to import this first module, but I can see using the id() function that they are actually different objects - different instances - when they are imported by the various games.

Any ideas about how to do this?

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
Madbutter
  • 31
  • 1

2 Answers2

0

Delegate the opening and management of the serial port to a separate daemon, and use a UNIX domain socket to transfer the file descriptor for the serial port to the client programs.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

The serial object is saying it will handle data arriving at a particular port.

You can't have more than one process handling TCP data on a port.

When the games import the common serial module, I assume they are being run in separate processes, this means they can't create a serial object on the same port.

You would have to have the common module run the games, either in the same process or a new process. Sorry this answer is a little general, but your question is quite broad.

Community
  • 1
  • 1
Peter Wood
  • 23,859
  • 5
  • 60
  • 99