Chhabilal
Chhabilal

Reputation: 1104

Use serial port in Python without installing external packages

I have following code:

import serial
import time

ser = serial.Serial('com4',115200,timeout=1)
while 1:
    time.sleep(10)
    print ser.readline()

I do not want to use serial module and allowed to use only standard python modules in my code.

Any quick suggestion how can I do that?

Upvotes: 0

Views: 2787

Answers (2)

Ashfiya
Ashfiya

Reputation: 1

import serial
import pyautogui

# Open serial port
arduino = serial.Serial('COM7', 9600)  # COM port may differ based on your system

while True:
    command = arduino.readline().decode('utf-8').strip()  # Read Arduino input

    if command == "button_pressed":
        pyautogui.press('enter')  # Simulate pressing the 'Enter' key on laptop
        print("Enter key pressed!")

Upvotes: 0

Pyserial is written in pure python; you can copy the serial folder into your project as a package, and it would work without installing anything.

Alternatively you can see its source code on how to implement similar functionality on your platform (Win32?) without including the whole library.

Upvotes: 5

Related Questions