0

I am controlling some LEDs via the USB serial port using a Tkinter/Python3x script to debug what I suspect is a hardware problem.

from tkinter import *
import serial

usbport = 'COM3'
ser = serial.Serial(usbport, 9600, timeout=1)

def init():
    print("Started")

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()
        self.scale1 = Scale(master, from_=0, to=255, command=lambda ev: self.getBright(1), bd=5, bigincrement=2, length=360, width=30, label='RED')
        self.scale1.set(128)
        self.scale1.pack(side=LEFT)
        self.scale2 = Scale(master, from_=0, to=255, command=lambda ev: self.getBright(2), bd=5, bigincrement=2, length=360, width=30, label='GREEN')
        self.scale2.set(128)
        self.scale2.pack(side=LEFT)
        self.scale3 = Scale(master, from_=0, to=255, command=lambda ev: self.getBright(3), bd=5, bigincrement=2, length=360, width=30, label='BLUE')
        self.scale3.set(128)
        self.scale3.pack(side=LEFT)
        self.centre = Button(frame, text="Centre All", command=self.centre)
        self.centre.pack(side=TOP)


    def getBright(self, slider):

        if slider==1:
            brt = self.scale1.get()

        if slider==2:
            brt = self.scale2.get()  

        if slider==3:
            brt = self.scale3.get()

        ser.write(255)
        ser.write(slider)
        ser.write(brt)

    def centre(self):

        for LED in range(0, 4):

            ser.write(255)
            ser.write(LED)
            ser.write(128)

        self.scale1.set(128)
        self.scale2.set(128)
        self.scale3.set(128)


init()
root = Tk()
app = App(root)
root.mainloop()

On the Arduino Mega 2560 side I read in from serial a start byte, LED label & a brightness value...

int user_input[3];    
int start_byte;       
int LED;           // which LED to pulse? r=1 g=2 b=3
int brightness;
int i;               

// LED pins
#define REDPIN 7
#define GREENPIN 8
#define BLUEPIN 9

void setup() 
{ 

  // LED outputs
  pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);

  Serial.begin(9600);
} 

void loop() 
{ 
  if (Serial.available() > 2) {

    start_byte = Serial.read();
    if (start_byte == 255) {

      for (i = 0; i < 2 ; i++) {
        user_input[i] = Serial.read();
      }

      LED = userInput[0];
      brightness = user_input[1];

      switch (LED) {

        case 1:
          analogWrite(REDPIN, brightness);
          break;

        case 2:
          analogWrite(GREENPIN, brightness);
          break;

        case 3:
          analogWrite(BLUEPIN, brightness);
          break;

      }
    }
  }
}

Now I would like to see what is actually being sent via the serial port to the Arduino using the serial monitor (yes I know I can just print the values in the Python terminal that's not what I'm asking).

The problem is that the Arduino Serial Monitor is locked out. I'm guessing this is a Windows thing (Win10 x64)? Has anybody here come across this?

  • I think this is totally normal. Only one application can use the port at once – chrisl Oct 11 '19 at 14:08
  • Wire USB to Serial adapter to any other hw Serial of the Mega and print there – Juraj Oct 12 '19 at 8:07
1

Serial data can only go to one place at a time. Either your Python program can receive the data, or the Serial Monitor can receive the data. Similarly the serial port can only receive data from one place at a time.

So Windows prevents you doing something invalid by locking you out of the serial port when something else is using it.

This is normal, and entirely desired functionality.

Your simplest option is to just print what you receive in your Python program.

| improve this answer | |
  • So this is an in built Windows thing then? I have no problem reading a serial port from multiple applications when using Raspbian. – DrBwts Oct 11 '19 at 14:35
  • 1
    That's because Linux us far more sophisticated than Windows. However, if you open the port multiple times on Linux only one program will receive data. Which one? Impossible to say. – Majenko Oct 11 '19 at 14:40
  • @Majenko "Which one? Impossible to say." Very sophisticated, indeed. ;) – DataFiddler Oct 12 '19 at 10:15
  • 1
    @DataFiddler It depends on many factors. Maybe it will be the last one connected. Maybe the first. Maybe it will be a 50/50 split. I don't know the answer. Some Linux kernel hacker might, but I don't. Personally I use socat on Linux to "tee" the data off and pass it out to another pty device if I need to debug at that sort of level. – Majenko Oct 12 '19 at 10:25
  • @Majenko: Thanks for bringing socat to my attention. – DataFiddler Oct 13 '19 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.