SoFunction
Updated on 2024-11-19

Example of python implementation to control COM port

The use of RS232 serial cable or RS232 to USB of this type of interface, will need to com port as an interface for input and output modulation.

Wrote a script to control the COM port, using Python's built-in serial library.

The code is as follows:

# coding=utf-8
 
import serial
import time
 
def setTout(t):
  print "Old Timeout is:[%s]" % () 
  (t)
  print "New Timeout is:[%s]" % () 
 
def sendShell(sp,cmd):
  (cmd+"\n")
  print "send shell cmd:[%s]" % cmd
  str = ()
  return str
 
def shell_io(sp,cmd,sleepTime):
  str = sendShell(sp,cmd) 
  print str
  (sleepTime)
  
po1 = ('com1',115200) 
timeStart = () 
portnow =      
print "COM port now is:[%s]" % portnow
setTout(5)
 
shell_io(po1,"ls",2)
 
shell_io(po1,"pwd",2)
 
shell_io(po1,"ls -l",2)
 
()
 

The above example of this python implementation to control the COM port is all that I have shared with you, I hope to give you a reference, and I hope you will support me more.