OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Python: Read data via serial port from Velleman k8090

  • Thread starter Thread starter Josh
  • Start date Start date
J

Josh

Guest
I have a Velleman k8090 Relay Board from which I am trying to read some data. I can write to the board fine, but whenever I output the read data, I get strange characters like a diamond or an upside-down question mark. Here is part of my code:

Code:
import serial
COM_PORT = 'COM4'
class Velleman8090:
    def __init__(self, port=COM_PORT):
        self.port = port
        self.baud_rate = 19200
        self.data_bits = 8
        self.parity = 'N'
        self.stop_bits = 1
        self.flow_control = 'N'

    def open_device(self):
        self.talk = serial.Serial(self.port, self.baud_rate, self.data_bits, self.parity, self.stop_bits)

    def firmware_version(self):
        data = packet(0x71, 0x00, 0x00, 0x00)
        self.talk.write(data)
        print self.talk.read()

    def close_device(self):
        self.talk.close()

def chksum(cmd,msk,p1,p2):
    return (((~(0x04 + cmd + msk + p1 + p2)) + 0x01) & 0xff)

def packet(cmd,msk,p1,p2):
    return str(bytearray([0x04, cmd, msk, p1, p2, chksum(cmd, msk, p1, p2), 0x0f]))

def main():
    vm8090 = Velleman8090()
    vm8090.open_device()        
    vm8090.firmware_version()
    vm8090.close_device()

Why am I getting these strange characters? The documentation states that it should return the year and the week.

EDIT: After some searching and troubleshooting, I am now pretty sure it has something to do with the Python code or data types and not the hardware. This forum contains an example in C which reads from the board. I got that example to properly read from the board so I am fairly sure it has something to do with the Python code and not the hardware itself.
<p>I have a Velleman k8090 Relay Board from which I am trying to read some data. I can write to the board fine, but whenever I output the read data, I get strange characters like a diamond or an upside-down question mark. Here is part of my code:</p>

<pre><code>import serial
COM_PORT = 'COM4'
class Velleman8090:
def __init__(self, port=COM_PORT):
self.port = port
self.baud_rate = 19200
self.data_bits = 8
self.parity = 'N'
self.stop_bits = 1
self.flow_control = 'N'

def open_device(self):
self.talk = serial.Serial(self.port, self.baud_rate, self.data_bits, self.parity, self.stop_bits)

def firmware_version(self):
data = packet(0x71, 0x00, 0x00, 0x00)
self.talk.write(data)
print self.talk.read()

def close_device(self):
self.talk.close()

def chksum(cmd,msk,p1,p2):
return (((~(0x04 + cmd + msk + p1 + p2)) + 0x01) & 0xff)

def packet(cmd,msk,p1,p2):
return str(bytearray([0x04, cmd, msk, p1, p2, chksum(cmd, msk, p1, p2), 0x0f]))

def main():
vm8090 = Velleman8090()
vm8090.open_device()
vm8090.firmware_version()
vm8090.close_device()
</code></pre>

<p>Why am I getting these strange characters? The documentation states that it should return the year and the week. </p>

<p>EDIT: After some searching and troubleshooting, I am now pretty sure it has something to do with the Python code or data types and not the hardware. <a href="http://forum.velleman.eu/viewtopic.php?f=3&t=6186" rel="nofollow">This forum</a> contains an example in C which reads from the board. I got that example to properly read from the board so I am fairly sure it has something to do with the Python code and not the hardware itself.</p>
 

Latest posts

M
Replies
0
Views
1
Mohit Pant
M
Top