Hi Andy,
 
The output below shows CANopen PDO message #2 (0x300) to ID 0x18, 1 byte, 
alternating between 0x01 and 0x00 being issued once per second.
 
pi@raspberrypi:~/projects/python $ python TestSerial.py
Sent CAN message with relay value = t318101
Sent CAN message with relay value = t318100
Sent CAN message with relay value = t318101
Sent CAN message with relay value = t318100
Sent CAN message with relay value = t318101
 
The little module shown in that photo I posted last time is connected to a 
pneumatic valve on Output #1.  It clicks ON and OFF once per second.   The code 
is running as a Python Command line program on a standard Raspian distro.  I'll 
bring out the Pi4 with LinuxCNC and take a look at linking into the HAL file.
 
IMHO the Python serial is very clumsy for dealing with serial with all the 
format and .encode parameters for simple character strings.  The source code is 
attached.
 
John Dammeyer
 
> From: andy pugh [mailto:bodge...@gmail.com]
> On Thu, 9 Sept 2021 at 05:31, John Dammeyer < <mailto:jo...@autoartisans.com> 
> jo...@autoartisans.com> wrote:
> > I've been reading
> >  <http://linuxcnc.org/docs/2.4/html/hal_comp.html> 
> > http://linuxcnc.org/docs/2.4/html/hal_comp.html
> 
> For this application I think that a Python userspace component using
> Pyserial is probably the easier approach.
> 
>  <http://linuxcnc.org/docs/2.8/html/hal/halmodule.html> 
> http://linuxcnc.org/docs/2.8/html/hal/halmodule.html
> 
> --
> atp
#!/usr/bin/python
# Test Application for CANUSB sending CANopen PDO #2 
# Version 0.1 10SEP21
# Written by 
#   John Dammeyer

import serial, time

# Serial stuff, change the serial port address by changing /dev/ttyS0 or COMx 
(for Windows)
# For USB based Serial dongles that use FTDI hardware /dev/ttyUSB0 is often the 
default.
# USB can be set for 115200 baud as long as the target hardware can support 
that since
# it doesn't really delay the LinuxCNC side.
ser = serial.Serial('/dev/ttyUSB0', 115200, bytesize=8, parity='N', stopbits=1, 
timeout=0, xonxoff=0, rtscts=0)

# Create some Global variables for readability
PDO = 0x300     # Process Data Object #2
NodeID = 0x18   # Device Node ID (0x01..0x7F)
PDO_ID = ''     # The character string that holds the OR operation of the PDO+ID
RelayImage = 0  # No relays set yet.
CANopenPDOMsg = ''  # For reporting what we send and so we don't have to 
recreate it each time.
TempStr = ""    # global so we can print what was sent.

# Function to send data to CANUSB dongle.  
# Parameters:
#   r is which relay to switch (1..8) 
#       does not validate relay # to see if in range.
#   level is TRUE or FALSE for relay ON or OFF.
# Outputs:
#   Sends CAN message to CANUSB.
#   Modifies Global RelayImage for control of more than one relay
#   Changes TempStr global variable for reporting message information.
def serial_command(r, level):
    global RelayImage         # if this isn't here then the OR operation fails 
with unitialized variable.
    global TempStr            # required so we can see what value was 
transmitted.
    global CANopenPDOMsg      # the message without the data.
    
    # Now empty the Rx Buffer for sample program we don't look at received 
messages.
    while ser.inWaiting():
        ser.reset_input_buffer()  #discard input for now.
     
    # Create the text value of the relay image.
    bitmask = 1 << (r-1)    # Select which relay to switch ON.
    if level == True:
        RelayImage = (RelayImage | bitmask)
    else:
        RelayImage = (RelayImage &  ~bitmask)
    ser.write(CANOpenPDOMsg.encode('utf-8'))  # send PDO 3xx to node 0x18 with 
1 byte
    # Format bit image to two hex characters, add CR character which tells 
CANUSB to send CAN message.
    TempStr = "{0:0{1}X}".format(RelayImage,2)
    ser.write(TempStr.encode('utf-8'))
    ser.write('\r'.encode('utf-8'))     # Tell CANUSB to send message.

try:
    # Create text value of PDO + ID
    PDO_ID = "{0:0{1}X}".format(PDO+NodeID,3)
    CANOpenPDOMsg = 't' + PDO_ID + '1'

    # Initialize CANUSB dongle
    ser.write('C\r'.encode('utf-8'))    # Close first
    ser.write('F\r'.encode('utf-8'))    # Clear Error Flags by reading them
    ser.write('S5\r'.encode('utf-8'))   # set 250kbps
    ser.write('O\r'.encode('utf-8'))    # Open CANUSB for communication.

    # Main Loop
    RelayValue = True
    RelayNumber = 1
    while 1:
        serial_command(RelayNumber, RelayValue)
        RelayValue = ~RelayValue
        print("Sent CAN message with relay value = " + CANOpenPDOMsg + TempStr)
        time.sleep(1)
        

except KeyboardInterrupt:
    raise SystemExit
_______________________________________________
Emc-users mailing list
Emc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/emc-users

Reply via email to