Send serial command



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 14/12/2011 at 08:23, xxxxxxxx wrote:

    Hi,

    I am trying to send a serial command from C4D; a very simple code for now as I'm trying to get things working.

    import serial
    import c4d
      
    def main() :
        ser = serial.Serial(2)
        ser.baudrate = 9600
      
        command1 = chr(0x84)+chr(0x17)+chr(0x40)+chr(0x3E)
        ser.write(command1)
      
    if __name__=='__main__':
        main()
    

    The pyserial module is installed, and loads successfully. Also, communication on port COM3 seems established when the device is on. However, executing this script from the script manager does nothing. Nothing is thrown in the console, and the device (a pololu mini maestro 24) doesn't get the command. It does seem to get something, as the mini LED onboard blinks when I execute the script, but that's it.

    I understand this is a bit specialized, and most likely outside of the usual C4D python scope, but anyone has any idea of what I'm doing wrong? I know similar things have been achieved, sending serial commands through c4d's python module (Srek got it working with an arduino board), but I can't seem to get it working.

    For info, the bytes I mean to send are: 0x84, 0x17, 0x40, 0x3E
    Breakdown:
    Command "SET_TARGET" (0x84)
    To channel 23 (0x17)
    Send target 8000 (pair of data bytes with most significant bits cleared);
    Target low bits (0x40)
    Target high bits (0x3E)

    Any help or pointer is really appreciated,
    Thanks!

    -A



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/12/2011 at 01:56, xxxxxxxx wrote:

    Hi, does the same code work in the Python standalone? Cheers, Sebastian



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/12/2011 at 09:06, xxxxxxxx wrote:

    It does.

    And so does it within C4D now - my bad, I was sending the serial data through the TTL port instead of the command port. Changing it to COM4 got everything running smoothly.

    Pretty cool, so now I expanded the script to read value from a potentiometer (knob), and apply the knob rotation value to the selected object. I then use SetTime and CallCommand(12410) to record the values in real-time - works all fine. But I have to make sure only "record rotation" is toggled before running the script, otherwise it records a key for position and scale (and PLA if it's a polygonal object) at every frame.

    How would I go about checking the toggle state of the keyframe recording modes?

    Thanks,

    -A



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/12/2011 at 09:55, xxxxxxxx wrote:

    Ha, figured it out!

    c4d.IsCommandChecked takes care of all of it. Nice.

    -A



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/12/2011 at 10:33, xxxxxxxx wrote:

    I would love to see tutorials about this kind of thing.
    I've been wanting to learn more about using PC ports to control hardware with python. But I'm not sure where to even begin.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/12/2011 at 11:03, xxxxxxxx wrote:

    It's actually pretty easy, once you get over the communication protocol (and figure out the proper COM port to use 😄)

    download the pyserial package, and install it in the C4D python package directory. Then, you simply have to import the serial module whenever you want to establish communication with a specific port.

    It took me a few hours to figure out how to parse the received data from the device (in my case, it's a little-endian two-byte unsigned integer). The Struct module (to install as well) provides easy ways to parse and conform such data. It obviously all depends on the serial device you're trying to control, but once you know how said device communicates (what kind of data it expects, and what to expect back), you're all set.

    What kind of hardware are you thinking of controlling?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/12/2011 at 11:48, xxxxxxxx wrote:

    The Struct module (to install as well)

    Since when do you have to install the struct module ? Or is there another third-party one ?

    import struct
    

    Cheers,



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/12/2011 at 11:59, xxxxxxxx wrote:

    You're right, my bad - not install, but import.

    While at it; I've been using

    import math
      
    math.radians(x)
    

    to converting degrees to radians... Is there any difference (in terms of performance, or whatever) in using this instead of the Rad() from c4d.utils?



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/12/2011 at 12:04, xxxxxxxx wrote:

    "What kind of hardware are you thinking of controlling?"

    ^No idea.
    It just sounds like something fun.

    The python module stuff is something I've already done in C4D. And pretty straight forward.
    But I'm completely oblivious to the the whole process of controlling hardware (and vice versa) with it.
    I've never done it before. And I don't even know what hardware options are available.

    It would be great to see someone do a tutorial on it. Just to get an idea of how the overall process works. And help noobs like me get started with it.

    -ScottA



  • THE POST BELOW IS MORE THAN 5 YEARS OLD. RELATED SUPPORT INFORMATION MIGHT BE OUTDATED OR DEPRECATED

    On 15/12/2011 at 15:43, xxxxxxxx wrote:

    to converting degrees to radians... Is there any difference (in terms of performance, or whatever) in using this instead of the Rad() from c4d.utils?

    There isn't. Maybe it was implemented because of people that come from coffe would rather search for exactly that function-name in the py4-docs than for a general python-method in google or the official python-docs.

    Cheers,
    Niklas


Log in to reply