Pyserial never read

L

luca72

Hello at all
sorry for my english but i'm Italian.
I use pyserial to communicate via rs232 with an extarnal device called
smartmouse.
I write the exact line that i want , but when i read i read only the
echo ond not the other bytes.
When i meke the same project with delphi i solve this problem with the
call of the sleep.
But in python it don't work.
Have you got some ideas for solve my problem?

Best Regards

Luca
 
J

jdhmistey

I don't know alot about python but I could help you with english?
eventhough it was really good

best regards-
Just
 
F

francois.schnell

Hello,

I'm not sure I understand precisely your question but maybe you can try
a readline and a flushinput like that:

import serial

port= 0 # or the port where you're device is connected
baudrate=9600 # or the baudrate of your device

s = serial.Serial(port, baudrate) # Open the port

for i in range(100):
s.flushInput()
line= s.readline()
print line
 
L

luca72

Thanks for your help, but it don't solve the problem.
I receive only the echo and full stop.

Many Thanks

Best Regards

Luca
 
P

Peter Hansen

Nick said:
Try swapping pins 2 and 3 in the lead.

Anything's possible, but given that in his original post he says it
works when he uses Delphi, it seems unlikely making a change to the
hardware is necessary.

-Peter
 
P

Peter Hansen

luca72 said:
Thanks for your help, but it don't solve the problem.
I receive only the echo and full stop.

If you want help, you'll do better to post small pieces of code that you
are actually using, rather than making us guess or imagine what you are
doing. There are perhaps a dozen things that can go wrong with serial
communications, and it's not efficient for us to start suggesting them
one at a time...

-Peter
 
D

Dennis Lee Bieber

Anything's possible, but given that in his original post he says it
works when he uses Delphi, it seems unlikely making a change to the
hardware is necessary.
Silly thought, but is there any possibility of a buffering problem
-- perhaps Delphi is doing unbuffered I/O and PySerial (or somewhere) is
waiting for a /r/n (or some combo thereof).

I'd also be suspicious of handshaking -- if one configuration is
using different handshaking requirements...
--
 
N

Nick Craig-Wood

Peter Hansen said:
Anything's possible, but given that in his original post he says it
works when he uses Delphi, it seems unlikely making a change to the
hardware is necessary.

Sorry missed that bit!

Pyserial works very well in my experience (under linux).

Serial ports are generally a pain though ;-)
 
L

luca72

Thanks to all for the help,

here you find the code, pls note if i use handshaking = 1 the
application don't start.
in the delphi configuratio of com port if i use or not handshaking the
application work.
Best Regards at all

Luca

import serial
import win32file
port = 2
baudrate = 38400
bytesize =serial.EIGHTBITS
parity =serial.PARITY_ODD
stopbits =serial.STOPBITS_TWO
timeout = 1
ser = serial.Serial(2, baudrate=38400, bytesize=8,
parity=serial.PARITY_ODD, stopbits=2, timeout=3)
ct = ''
ch = ''
a = self.textCtrl1.GetValue()
ind = 0
ind1 = 2
lunghezza = len(a)
while ind < lunghezza :
b = a[ind:ind1]
b = int(b,16)
b = ~b
c = ''.join([str((b >> Digit) & 1) for Digit in range(7,
-1, -1)])
c1 = c[0:4]
c2 = c[4:]
c1 = c1[3:] + c1[2:3] + c1[1:2] + c1[0:1]
c2 = c2[3:] + c2[2:3] + c2[1:2] + c2[0:1]
c1 = hex(int(c1,2))
c2 = hex(int(c2,2))
c1 = c1[2:]
c2 = c2[2:]
c = c2+c1
ct = ct + c
ind = ind + 2
ind1 = ind1 + 2
c = int(c,16)
c = chr(c)
ch = ch + c


ser.write(ch)

elf.textCtrl2.SetValue(ct)
ser.readline()



Pls.Note i hove also try with read(number of byte ) with inWaiting(),
flush etc........


But no result.

Thanks Luca
 
G

Grant Edwards

Sorry missed that bit!

Pyserial works very well in my experience (under linux).

I've used it extensively under both Linux and Win32, and it
works very well under both.
Serial ports are generally a pain though ;-)

That's the truth.
 
P

Petr Jakes

Well, I think it is better to start with some simple code first.
Try to read serial port and print it out.
something like this could work:

import serial

s = serial.Serial(port=2,baudrate=38400, timeout=20)

while 1:
print s.readline()

Petr Jakes
 
S

sam

luca72 said:
Thanks to all for the help,

here you find the code, pls note if i use handshaking = 1 the
application don't start.
in the delphi configuratio of com port if i use or not handshaking the
application work.
Best Regards at all

Luca

import serial
import win32file
port = 2

Is port = 2 correct?
I thought that com ports under windows are designated as a string
"com1","com2",..etc.
baudrate = 38400
bytesize =serial.EIGHTBITS
parity =serial.PARITY_ODD
stopbits =serial.STOPBITS_TWO
timeout = 1
ser = serial.Serial(2, baudrate=38400, bytesize=8,
parity=serial.PARITY_ODD, stopbits=2, timeout=3)
ct = ''
ch = ''
a = self.textCtrl1.GetValue()
ind = 0
ind1 = 2
lunghezza = len(a)
while ind < lunghezza :
b = a[ind:ind1]
b = int(b,16)
b = ~b
c = ''.join([str((b >> Digit) & 1) for Digit in range(7,
-1, -1)])
c1 = c[0:4]
c2 = c[4:]
c1 = c1[3:] + c1[2:3] + c1[1:2] + c1[0:1]
c2 = c2[3:] + c2[2:3] + c2[1:2] + c2[0:1]
c1 = hex(int(c1,2))
c2 = hex(int(c2,2))
c1 = c1[2:]
c2 = c2[2:]
c = c2+c1
ct = ct + c
ind = ind + 2
ind1 = ind1 + 2
c = int(c,16)
c = chr(c)
ch = ch + c


ser.write(ch)

elf.textCtrl2.SetValue(ct)
ser.readline()
You might want to try using ser.read() instead of ser.readline() as
you may not be getting linefeed carrage return characters . I usually
setup a buffer to scan for the characters I expect.
Pls.Note i hove also try with read(number of byte ) with inWaiting(),
flush etc........


But no result.

Thanks Luca

Hope this helps
Sam Schulenburg
 
G

Grant Edwards

Is port = 2 correct?

Read the fine documentation at http://pyserial.sourceforge.net/:

Parameters for the Serial class

ser = serial.Serial(
port=None, #number of device, numbering starts at
#zero. if everything fails, the user
#can specify a device string, note
#that this isn't portable anymore
#if no port is specified an unconfigured
#an closed serial port object is created
baudrate=9600, #baudrate
bytesize=EIGHTBITS, #number of databits
parity=PARITY_NONE, #enable parity checking
stopbits=STOPBITS_ONE, #number of stopbits
timeout=None, #set a timeout value, None for waiting forever
xonxoff=0, #enable software flow control
rtscts=0, #enable RTS/CTS flow control
)
I thought that com ports under windows are designated as a string
"com1","com2",..etc.

You can use those as well, but they're non-portable.
serial.Serial(port=0) will open the first serial port on either
windows or linux.
 
D

Dennis Lee Bieber

Thanks to all for the help,

here you find the code, pls note if i use handshaking = 1 the
application don't start.
in the delphi configuratio of com port if i use or not handshaking the
application work.
Best Regards at all

Luca

import serial
import win32file

That equates to COM3 on windows; is that correct?
baudrate = 38400
bytesize =serial.EIGHTBITS
parity =serial.PARITY_ODD
stopbits =serial.STOPBITS_TWO
timeout = 1

Why bother defining all these, since you aren't using them in the
next line... In fact, you set up a three second timeout in the next
line.
ser = serial.Serial(2, baudrate=38400, bytesize=8,
parity=serial.PARITY_ODD, stopbits=2, timeout=3)

If you still want the above defined variables, then you want (the
name on the left of the = is the argument placeholder name, the copy on
the right is your predefined variable name -- using placeholder names
lets you leave things out, or change the order):

ser = serial.Serial(port, baudrate=baudrate, bytesize=bytesize,
parity=parity, stopbits=stopbits,
timeout=timeout)

NOTE: you defined "timeout" as 1, but then did the open with a
timeout of 3.

NOTE: I have never seen a stopbits of 2 used on anything faster than
around 300 baud. The most common setup is 8-bit, no parity, 1 stop bit
("8n1"). If the other end is sending with 1 stop-bit, your end may never
be seeing a "stop". It is always safe to SEND a longer stop than the
other end expects, but not the other way around.
ct = ''
ch = ''
a = self.textCtrl1.GetValue()
ind = 0
ind1 = 2
lunghezza = len(a)
while ind < lunghezza :
b = a[ind:ind1]
b = int(b,16)
b = ~b
c = ''.join([str((b >> Digit) & 1) for Digit in range(7,
-1, -1)])
c1 = c[0:4]
c2 = c[4:]
c1 = c1[3:] + c1[2:3] + c1[1:2] + c1[0:1]
c1 = c1[3] + c1[2] + c1[1] + c1[0]
c2 = c2[3:] + c2[2:3] + c2[1:2] + c2[0:1]
c1 = hex(int(c1,2))
c2 = hex(int(c2,2))
c1 = c1[2:]
c2 = c2[2:]
c = c2+c1
ct = ct + c
ind = ind + 2
ind1 = ind1 + 2
c = int(c,16)
c = chr(c)
ch = ch + c
I don't even want to think what that is supposed to be doing -- I
bet it can be done much easier....

Let's see... You are taking in some string; splitting it into
2-character chunks, interpreting those as Hex, 1s-complementing them,
making a string of binary from the result, reversing the bit order,
converting back to hex, then joining them, converting back to integer,
just to generate a character value.

It would be cleaner to just predefine a look-up table.
ser.write(ch)

elf.textCtrl2.SetValue(ct)
ser.readline()

If the serial package is anything like regular file I/O, that
line will buffer/block until a new-line character is received, then
return data upto/including the new-line. If the inbound data has
multiple lines, you need individual readlines.

ALSO... YOU NEVER SPECIFY A VARIABLE TO RECEIVE THE DATA -- ANYTHING
YOU DO READ IS BEING DUMPED ON THE FLOOR!

ReceiveData = ser.readline()
--
 
L

luca72

Hello
ALSO... YOU NEVER SPECIFY A VARIABLE TO RECEIVE THE DATA -- ANYTHING
YOU DO READ IS BEING DUMPED ON THE FLOOR!

I see the read data with a sniffer.

with the same serial caracteristic in delphi i obtain the right answer.

I use serial writestr and serial readstr.


If the serial package is anything like regular file I/O, that
line will buffer/block until a new-line character is received, then
return data upto/including the new-line. If the inbound data has
multiple lines, you need individual readlines.

For example i i send D0360000EC the right answer is
D0360000EC0000FFBC35DC44 etc...
i need new Line?

why if i use ser.read(10) i see only the first 5 byte and not the rest,
why if i use the inwaigth i answer that i have only 5 byte to read.


Regards

Luca
 
D

Dennis Lee Bieber

For example i i send D0360000EC the right answer is
D0360000EC0000FFBC35DC44 etc...
i need new Line?

why if i use ser.read(10) i see only the first 5 byte and not the rest,
why if i use the inwaigth i answer that i have only 5 byte to read.
This is not clear. What do you define to be the "5 byte and not the
rest". The above, from what I saw of it, implies an ASCII REPRESENTATION
of the data, not a binary...

The string "D0360000EC" is a 10-character string, not 5-bytes. If
the other end is echoing data, it will be echoing a 10 byte(character)
string. A ser.read(10) will see exactly that and nothing after it. And
if the other end is waiting for a certain 5 byte value, it is not seeing
that.
len("D0360000EC") 10
len("\xD0\x36\x00\x00\xEC") 5
print repr("\xD0\x36\x00\x00\xEC") '\xd06\x00\x00\xec'
dt = "D0360000EC"
print repr("".join([chr(int(dt[2*x:2*x+2],16)) for x in range(len(dt)//2)]))
'\xd06\x00\x00\xec'


However, if you are using some sort of protocol analyzer on the
serial line itself to see what the data passing is, and are not seeing
anything that you expect to see coming back... I'd suggest the problem
is not Python's serial implementation, but maybe the data and/or
handshaking being sent between the two machines.

Are the interface specifications for whatever you are trying to
communicate with available for review?


BTW: does the following do the equivalent of that mishmash of
string/integer/concatenation/shifts that filled you previous sample? I'd
bet it runs faster.

-=-=-=-=-=-=-=-=-=-
lookUp = { "0" : "F", "1" : "7",
"2" : "B", "3" : "3",
"4" : "D", "5" : "5",
"6" : "9", "7" : "1",
"8" : "E", "9" : "6",
"A" : "A", "B" : "2",
"C" : "C", "D" : "4",
"E" : "8", "F" : "0" }

a = "F493FFFFC8" #reverse engineered from your sample

if len(a) % 2:
a = "0" + a #ensure input is even length
a = a.upper() #ensure uppercase for lookup table

#build up ASCII representation of nybble-swapped, bit-swapped,
#ones-complement of input ASCII hex string
cl = [lookUp[a[i*2+1]] + lookUp[a[i*2]] for i in range(len(a) // 2)]

#convert to true binary (character) string
co = "".join([chr(int(x, 16)) for x in cl])

print len(co), repr(co) #just for testing
-=-=-=-=-=-=-=-=-=-=-

5 '\xd06\x00\x00\xec'

IE, if the input (named "a") is an ASCII representation of a
hexadecimal number, the output is a character string containing the
binary value, after manipulation (with half the length of the input
string).
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

The state of pySerial 3
Aborting a read with pySerial 1
Pyserial again 21
PySerial and termios 3
Unexpected long pyserial read delay on Windows 1
pyserial: Unexpected Local Echo 5
pyserial 5
pySerial help? 0

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top