1

I am totally new to python. I am using python 3.4.3 and I have been trying to transmit data to usb to a LED driving circuitry.I want to modulate data which is reaching to the usb port. I have a usb to serial converter installed in my system. can anyone suggest a programme code for the same. The programme i have tried is

import serial
ser=serial.Serial("/dev/ttyUSB0", 115200)
ser.open()
ser.isOpen()

print ('Enter your commands below.\r\nInsert "exit" to leave the application.')

I am having MS Windows 8.1 with 64 bit OS.

I have installed the serial module using pyserial-2.7.win32_py3k.exe downloaded from https://pypi.python.org/pypi/pyserial.The error message is

Traceback (most recent call last):
File "C:/Users/shamsu/Desktop/ss.py", line 10, in <module>
bytesize=serial.SEVENBITS
File "C:\Python34\lib\site-packages\serial\serialwin32.py", line 38, in __init__
SerialBase.__init__(self, *args, **kwargs)
File "C:\Python34\lib\site-packages\serial\serialutil.py", line 282, in __init__
self.open()
File "C:\Python34\lib\site-packages\serial\serialwin32.py", line 66, in open
raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port '/dev/ttyUSB1': FileNotFoundError(2, 'The system cannot find the path specified.', None, 3)

Did i installed serial module for 32 bit? my system is using 64 bit windows 8.1 OS.

I don't know whether this programme is correct for my application. Can anyone help me?

Shamsudheen P
  • 11
  • 1
  • 4
  • The error says it can't open serial port /dev/ttyUSB1 as its not found. usb/tty1 is the linux serial port not windows. On windows you will need to go to the device manager and see what com port your usb to serial adaptor is using. eg ser = serial.Serial('COM1', 9600) – lxx May 01 '15 at 07:23
  • Thanx for reply. Now could you please tell me how to transmit and receive an image to the usb port? – Shamsudheen P May 01 '15 at 08:57
  • usb -serial isn't usb its serial. Open an image, convert it to an array or whichever format you need, then send it via serial. How depends on what you need at the led driving end. binary bits, hex, int ??? – lxx May 01 '15 at 10:59
  • I think it is better to use binary bits for me. I dnt know whether other schemes have better performance.can you mention how to convert an image to an array,please? – Shamsudheen P May 02 '15 at 13:18
  • What have you tried ? using opencv(cv2) or pil its very easy to convert to an array or to access the image in an array form. Post your code so people can help /advise you on it. What are you using at the led end ? an arduino or ? – lxx May 03 '15 at 07:54

1 Answers1

1

On windows change to

ser = serial.Serial('COM1', 9600) or ser = serial.Serial(port='COM4')

For use in a class can do it like

self.ser=serial.Serial(port='\\.\COM1', baudrate=9600, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1)

to open an image , could use PIL or opencv eg How do I convert a numpy array to (and display) an image?

also basic image processing tutorial http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_core/py_basic_ops/py_basic_ops.html

with opencv2 and numpy

    import cv2
    import numpy as np
    image = cv2.imread("image.png") # or full path to image

    print(image.size)
    print(image.shape)    
    print image[0,0]
    ser.write(image[0,0])

Do the serial writing in a loop and iterate across the image. Or first convert it to a black and white image then send it.

on sending files with pyserial Using Pyserial to send a file?

how to read data with pyserial How to read data from pyserial incrementally?

That should be enough to get you started

Sean
  • 17
  • 9
lxx
  • 1,326
  • 20
  • 30