pyserial read

//python

import serial

port = serial.Serial('COM5',9600)

while( port.isOpen()):
    comdata = int(input())

    if(comdata == 1):
        port.write(str.encode('1'))
    elif(comdata == 2):
        port.write(str.encode('2'))

    elif (comdata == 3):
        port.write(str.encode('3'))
    elif (comdata == 4):
        port.write(str.encode('4'))

    elif (comdata == 5):
        port.write(str.encode('5'))
    elif (comdata == 6):
        port.write(str.encode('6'))

    elif (comdata == 7):
        port.write(str.encode('7'))
    elif (comdata == 8):
        port.write(str.encode('8'))

    else:
        print('Invalid input!!!!')
        
// arduino

const int LED1 = 8;
const int LED2 = 9;
const int LED3 = 10;
const int LED4 = 11;
char comdata;

void setup() {

  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  Serial.begin(9600);
}

void loop() {

  if (Serial.available() > 0) {
      comdata =(Serial.read());
      
    if (comdata == '1') {
      Serial.println("LED1 is ON");
      digitalWrite(LED1, HIGH);
    }  
    else if (comdata == '2') {
      Serial.println("LED1 is OFF");
      digitalWrite(LED1, LOW);
    }
    
    
    else if (comdata == '3' ) {
      Serial.println("LED2 is ON");
      digitalWrite(LED2, HIGH);
    }
    else if (comdata == '4' ) {
      Serial.println("LED2 is OFF");
      digitalWrite(LED2, LOW);
    }
    
    else if (comdata == '5') {
      Serial.println("LED3 is ON");
      digitalWrite(LED3, HIGH);
    }
    else if (comdata == '6') {
      Serial.println("LED3 is OFF");
      digitalWrite(LED3, LOW);
    }
    
    else if (comdata == '7') {
      Serial.println("LED4 is ON");
      digitalWrite(LED4, HIGH);
    }
    
    else if (comdata == '8') {
      Serial.println("LED4 is OFF");
      digitalWrite(LED4, LOW);}


    
  }
}


4
2
Tiny Posers 120 points

                                    # https://pyserial.readthedocs.io/en/latest/pyserial_api.html
# pip install pyserial
import serial

if __name__ == "__main__":
    with serial.Serial('COM10') as serial_port:
        line = serial_port.readline()

4 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
python library for serial communication python view serial communication python usb serial communication python serial ir communication example python serial communication code python serial communication example pyserial read example python to python serial communication without pyserial pyserial python install serial communication python serial communication server serial communication in python Serial communication in oython python3 serial reading example serial communication with python python ser.read python serial readline timeout python serial.readline UART python python serial\ print in serial python python open serial line on produced id com port python serials port python python pyserial example readline serial python pyserial code example python and serial communication python write to serial port reading serial string pyserial create a serial terminal on python serial python communication python com port communication python seriel pyserial com port serial port python exampl pyserial read how to get serial in python read serial python write over serial port python python uart python2 serial how to use python serial module serial python using serial python example python read from serial port read serial data python pyserial tutorial how to use the serial module in python? seriall on python python serial read line python serial write how to use serial port in python listen to a COM port with pyserial serial terminal python pyserial listen python serial example python serial communication linux python serial communication python serial .Serial python read serial with marjers python serial interface uart pytho serial ports python python serial read python read/write serial port aserial.Serail() python serial commands serial.Serial python pyserial uart example pyserial readline pyserial example python with serial() as port python with serial python serial with python com port stop read write python com port stop read at 10047 python serial port communication example how to use com port in python serial() python port com python pyserial read line serial python python serial serial port python pyserial read serial port in linux python serial port example serial readline python read serial port python serial read in python serial in python serialport python python com port serial communication python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source