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:

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

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/

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

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

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!

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

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.

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

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

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")

Edited 6 Years Ago by wtcolson: n/a

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()
Comments
looks like a nice info

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.

This article has been dead for over six months. Start a new discussion instead.