Skip to main content

Use C# program to control the light and dark of photoelectric sensor?


How can I control the brightness of the sensor? Can anyone recommend an affordable, entry-level hardware solution that would accomplish this? (Other programming languages, Raspberry Pis, or microcontrollers are not considered.)

A friend sent me this code:

#include <stdio.h>
#include "CH341.h"

static int open_device(HANDLE *out)
{
    *out = CH341OpenDeviceEx(0);
    if (*out == NULL || *out == INVALID_HANDLE_VALUE) {
        return -1;
    }
    return 0;
}

static void close_device(HANDLE h)
{
    CH341CloseDevice(h);
}

static int configure_io(HANDLE h)
{
    if (CH341ResetDevice(0) != 0) return -1;

    if (CH341SetOutput(0, 0xffffffff, 0x00000000, 0x00000000) != 0) return -1;
    if (CH341SetOutput(0, 0b11111, 0xffffffff, 0xffffffff) != 0) return -1;

    return 0;
}

int main(void)
{
    HANDLE hdlCh341;

    if (open_device(&hdlCh341) != 0) {
        fprintf(stderr, "Failed to open CH341 device\n");
        return 1;
    }

    printf("%lu\n", (unsigned long)hdlCh341);
    printf("%lu\n", CH341GetVersion());

    if (configure_io(hdlCh341) != 0) {
        fprintf(stderr, "Failed to configure CH341 I/O\n");
        close_device(hdlCh341);
        return 1;
    }

    close_device(hdlCh341);
    return 0;
}

Comments