Import serial module

Answered

Hai,

      I am installed all packages of serial module.when i run the program in pycharm it returns error, No module import serial.But when i run the same program on terminal the program ran.Please anyone help me.

Thanks and Regards

Aneesh

 

0
5 comments

Hello Aneesh,

Could you please attach sample code example for investigation? Do you use the same interpreter in your project (and run configuration) and in terminal?

 

0

import serial
import time
from Logger import *
from Timeout import *

LOG_WR = 'WR'
LOG_RD = 'RD'
STIMER = 1
"""
Singleton class implementation of Serial Communication.
Limited to only one connection
"""

class Serial_Comm:

def __init__(self, port, baud):
self.baud_rate = baud
self.port = port
self.log_handle = DebugLogger()

def write(self, in_msg=''):
"""
Perform a write operation on UART port
:return: <None>
"""
self.log_handle.log(self.log_handle.INFO, LOG_WR, "Opening Serial Port")
# open serial port
serial_uart_port = serial.Serial(self.port, self.baud_rate)
if (serial_uart_port.isOpen() == False):
serial_uart_port.open()
self.log_handle.log(self.log_handle.INFO, LOG_WR, "Port Opened for Writing ......")
# write a string
serial_uart_port.write(in_msg)
self.log_handle.log(self.log_handle.INFO, LOG_WR, "Writing Successfully Completed")
self.log_handle.log(self.log_handle.INFO, LOG_WR, "Closing Serial Port")
# Close port
serial_uart_port.close()

def read(self):
"""
Perform a read operation on UART port
:return: <rcv>
"""
rcv = ''

self.log_handle.log(self.log_handle.INFO, LOG_RD, "Opening Serial Port")
# open serial port
serial_uart_port = serial.Serial(self.port, self.baud_rate)
if (serial_uart_port.isOpen() == False):
serial_uart_port.open()
serial_uart_port.setTimeout(1)
while (STIMER ==1):
self.log_handle.log(self.log_handle.INFO, LOG_RD, "Port Opened for Reading ......")
# Read data
ch = serial_uart_port.read()
rcv += ch
if not ch:
self.log_handle.log(self.log_handle.INFO, LOG_RD, "End of File No data Received ......")
break
self.log_handle.log(self.log_handle.INFO, LOG_RD, "Reading Data is %s" % ch)
self.log_handle.log(self.log_handle.INFO, LOG_RD, "Closing Serial Port")
serial_uart_port.close()
return rcv

 

 

 

In my external libraries of pycharm, site packages are excluded. How can i include that site packages

Thanks and regards ,

Aneesh

0

Aneesh,

Your code runs fine after I installed "serial" package to project interpreter: https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html

Do you use the same interpreter in terminal and while running your code?

What do you mean by "external libraries"? Are they marked as excluded in project structure ("Settings | Project | Project Structure")?

 

1

Hello Yaroslav,

            Thank you so much for your reply. I found my mistake. It was on the configuration of interpreter.

 

Regards,

Aneesh

 

 

0

Aneesh,

You are welcome!

1

Please sign in to leave a comment.