Reading serial.print data with unknown characters python I've got some simple python code working to read the serial.print data that is being sent from my Arduino Uno. It seems to be receiving the values correctly and printing them fine, however it is not receiving it exactly as I expected and I cant find a way to get rid of the extra characters.

Here is what the output looks like, with the 'b's and the apostrophes which shouldn't be there:


  b'0 L/hour' 
  
  b'0 L/hour' 
  
  b'0 L/hour' 
  
  b'0 L/hour' 
  
  b'0 L/hour' 
  
  b'64 L/hour' 
  
  b'648 L/hour' 
  
  b'168 L/hour' 
  
  b'8 L/hour'


My python code is as follows:

import serial
arduino = serial.Serial('COM1', 9600, timeout=.1)


while True:
    data = arduino.readline()[:-2]  # the last bit gets rid of the new-line chars
    if data:
        print(data)


and the arduino code:

volatile int  flow_frequency;  // Measures flow meter pulses
unsigned int  l_hour;          // Calculated litres/hour                      
unsigned char flowmeter = 2;  // Flow Meter Pin number
unsigned long currentTime;
unsigned long cloopTime;

void flow ()                  // Interruot function
{ 
   flow_frequency++;
} 

void setup()
{ 
   pinMode(flowmeter, INPUT);
   Serial.begin(9600); 
   attachInterrupt(0, flow, RISING); // Setup Interrupt 
                                     // see http://arduino.cc/en/Reference/attachInterrupt
   sei();                            // Enable interrupts  
   currentTime = millis();
   cloopTime = currentTime;
} 

void loop ()    
{
   currentTime = millis();
   // Every second, calculate and print litres/hour
   if(currentTime &gt;= (cloopTime + 1000))
   {     
      cloopTime = currentTime;              // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min. (Results in +/- 3% range)
      l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour 
      flow_frequency = 0;                   // Reset Counter
      Serial.print(l_hour, DEC);            // Print litres/hour
      Serial.println(" L/hour");


   }
}


is it the arduino side or the python side that is adding the extra characters? I want to remove the "b" and apostrophes...
This is not an answer, but you have a insidious little problem in your code that can be easily solved disabling interrupts before using flow_frequency:

  if(currentTime &gt;= (cloopTime + 1000))
   {     
     cloopTime = currentTime;              // Updates cloopTime
       // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min. (Results in +/- 3% range)
      noInterrupts();
      int flow_copy = flow_frequency;
      flow_frequency = 0;                   // Reset Counter
      interrupts();
      l_hour = (flow_copy * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour 


The reason is that you need to freeze flow_frequency so it doesn't change while you are using it. If an interrupt comes while your are using flow_frequency, you will end up with a value that's a mixed of old and new.

Solution: Copy the values updates inside the ISR before using them. Disable interrupts before doing it and reenable after that.

Reason: High level operations like flow_frequency++ and flow_frequency=0 are not atomic. At machine level they are composed of multiple machine instructions, which can be interrupted at any time, leaving things in an undeterminated state.