954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

send raw data via serial

Hi,

got a quick quickie...

Im trying to send commands to a micrcontroller via a USB->serial converter.

Im writing with wxPython on an XP machine it thats important.

I have been using the win32 module along with USPP and so far so good, apart from one thing. As far as i can work out it is only possible to write strings to the port as USPP uses a file handling style.

I am hoping to be able to send simple 8bit packets. I tried converting my data into the corresponding ASCII character and writing that to the port, but for anything lower than 0x20 it wont work as anything lower is an unprintable character...

Any help would be greatly appreciated.


Mark :mrgreen:

MarkWalker84
Newbie Poster
19 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

hmm... 20 views no replies...

Is this just a really difficult thing to do or have i missed something? done something wrong? i tried searching the forum but came up with nothing.

Thanks,


Mark

MarkWalker84
Newbie Poster
19 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Mark, I think it is just a very specialized area. I did a lot of that kind of work in my earlier days using Delphi and the RS232 serial port. The modules (ADAM from Advantech Co., Ltd.) I used exchanged data as strings. It worked very well and was fun!

I trust you have taken a look at PyVisa:
http://pyvisa.sourceforge.net/

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

I hadnt actually come across that before - i will dig around the siter and see what it unearths :-)

I feared this wasnt exactly a straight forward thing to do... never mind though - thats half the fun of programming :-)

Thanks for the link,


Mark

MarkWalker84
Newbie Poster
19 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

It almost seems like what you need is a simple C function to write the char to the port, and then link it in to your Python program.

Jeff

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 
It almost seems like what you need is a simple C function to write the char to the port, and then link it in to your Python program. Jeff

If you use Windows XP forget that, you can only get to the ports using the designated driver. The days you could go directly to a port are long gone!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Yeah - i tried writing a program a few months back in VC++ which ultimately ended in failure when i realised it was near impossible to access serial / parallel ports with out some SERIOUS knowledge of kernals / drivers etc... much more than the basics...

I was under the impression that Python was a lot more user friendly in this aspect and so far i would have to agree... but its this one thing is really holding me up :-(

If it is possible to write a string to the port... why is it not possible to write an int, or hex value?? ARGH! lol


Mark

MarkWalker84
Newbie Poster
19 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
If it is possible to write a string to the port... why is it not possible to write an int, or hex value?? ARGH! lol

Wouldn't packing ( http://docs.python.org/lib/module-struct.html ) the data work for you?

Regards,
Greg

Jergosh
Newbie Poster
3 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Python has full set of binary operators, but I don't think there is one way to send bitstring to USB chip. Ultimately that is how data would travel in the serial wire.

bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
 

Its looking a lot like this cant be done...

So i think im going to have to do some sort of work around.

Just seems absolutely ridiculous to me that it is not possible to send binary / hex data to a serial port.


Mark

MarkWalker84
Newbie Poster
19 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
Its looking a lot like this cant be done... So i think im going to have to do some sort of work around. Just seems absolutely ridiculous to me that it is not possible to send binary / hex data to a serial port. Mark

Wait a second. Going back to your first post:I have been using the win32 module along with USPP and so far so good, apart from one thing. As far as i can work out it is only possible to write strings to the port as USPP uses a file handling style.

I am hoping to be able to send simple 8bit packets. I tried converting my data into the corresponding ASCII character and writing that to the port, but for anything lower than 0x20 it wont work as anything lower is an unprintable character...

So your problem isn't sending a single byte, nor is it communicating with the device. Rather, your problem is sending chars between 0x01 and 0x20.

Now, if I type

print '\x03'


I get a , which means that the terminal printed something else instead ... but 0x03 was actually sent to the character mapper.

What happens when you try to send 0x03 to your device? Does it get sent as the box character? Do you have success sending 0x25 (say)?

Jeff

jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 92
Solved Threads: 156
 

Hey Mark
Are you able to send the integer values using python to you controller now?

Siddi

siddijaiprakash
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

I might have missed something, but did you try PySerial?

import serial

x = serial.Serial(portNumber)  
rx = x.readline()  #or x.read() for one byte
tx = x.write("something")
wtcolson
Newbie Poster
2 posts since Dec 2009
Reputation Points: 10
Solved Threads: 1
 

I realise this is an old thread, but it showed up in google when I was looking for an answer to this, just starting with python. I recently tried the same thing in perl with success.

If you want to send something like the sequence 0xaa 0x06 0xff, do something like:

import serial,struct
port = serial.Serial('/dev/ttyS0')
outputStr = struct.pack('BBB',0xaa,6,255)
port.write(outputStr)
port.close()
mr_snarf
Newbie Poster
2 posts since Jan 2012
Reputation Points: 23
Solved Threads: 0
 

What microcontroller are you using?

Tech B
Posting Whiz in Training
268 posts since May 2009
Reputation Points: 59
Solved Threads: 33
 
What microcontroller are you using?

I'll assume you mean me due to timing.

I'm sending commands via serial to a PIC16F628, which is controlling several servos. The servo controller code for it is from http://burningsmell.org/pic16f628/ which makes use of an initial 0xaa byte to signal that the rest of the command is coming. The next byte is the servo id and the final is the desired position.

mr_snarf
Newbie Poster
2 posts since Jan 2012
Reputation Points: 23
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You