Развлекухи ради хочу подключить датчик движения к компьютеру. Самым простым вариантом посчитал подключение по Com порту.
Посмотрел распиновку, она такова:
1 CD Вход Обнаружена несущая
2 RXD Вход Принимаемые данные
3 TXD Выход Передаваемые данные
4 DTR Выход Хост готов
5 GND – Общий провод
6 DSR Вход Устройство готово
7 RTS Выход Хост готов к передаче
8 CTS Вход Устройство готово к приему
9 RI Вход Обнаружен вызов
У датчика движения есть выход, при срабатывании датчика на выходе появляется 12В. Думаю что нужно подключить выход датчика движения к 9 ноге Com порта.
Осталось, только читать бит отвечающий за 9 ногу на ком порте, подскажите в какую сторону копать?
Обнаружение сигнала на Com порте
при срабатывании датчика на выходе появляется 12В. Думаю что нужно подключить выход датчика движения к 9 ноге Com порта.
12В не многовато для COM?
UPD. Хотя, вроде до 15В...
Вроде как раз, от +3 до +12
12В не многовато для COM?
У него логические уровни как раз +-12 B, если не ошибаюсь.
Повезло мне, нашел на буржуйском форуме, чувак тоже самое делал.
Если на ногу 9 подать от 3 до 12 вольта, на 5 ногу земля, то программа отлавливает это дело, датчик движения пока не цеплял, попробовал с кроной 9В, работает!
Программа регистрация напряжения от +3 до +12 на ноге 9 Com порта
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/signal.h>
#include <linux/serial.h>
#define DEVICE "/dev/ttyS0"
#define TRUE 1
#define FALSE 0
// read the current level from RING INDICATOR pin
// Parameters: int fd – a handle to the file descriptor of the tty.
// Returns:
// TRUE (1) - if the TIOCM_RI flag is set.
// FALSE (0) - if the TIOCM_RI flag is not set.
// < 0 - if an error occurred reading the flags.
int get_rng_state(int fd)
{
int return_val = 0;
int serial = 0;
// Check that the file descriptor given is not rubish.
if(fd < 0)
{
fprintf(stderr, "Bad file descriptor (%d).\n", fd);
return fd;
}
// try to read the value.
if(((return_val = ioctl(fd, TIOCMGET, &serial))) < 0)
{
fprintf(stderr, "ioctl(TIOCMGET) failed: %d: %s\n", errno, strerror(errno));
return return_val;
}
// Success!, set the output value to TRUE or FALSE.
return (serial & TIOCM_RI) ? TRUE : FALSE;
}
// sample code for blocking until rng state changes
int main(int argc, char** argv)
{
int return_code = 0;
int omode = O_RDONLY;
// This struct will hold the results of the TIOCGICOUNT ioctl call.
// The serial_icounter_struct is defined in linux/serial.h
struct serial_icounter_struct serial_counter;
// open the serial stream
int fd = open(DEVICE, omode, 0777);
if(fd < 0)
{
fprintf(stderr, "open() failed: %d: %s\n", errno, strerror(errno));
return fd;
}
if((return_code = get_rng_state(fd)) < 0)
{
return return_code; // bug out if an error occurred.
}
else
{
printf("Device opened, intitial RI state: %s\n", (return_code ? "SET" : "UNSET"));
}
// detect rng changes forever
int i=0;
int state;
int wait_on_ri;
int ring_count = 0;
while(1)
{
// The flags to wait on. assigned to an int here so that it can be passed by ref.
wait_on_ri = TIOCM_RI;
// Get the current state, break out of the while loop if this fails.
if((state = get_rng_state(fd)) < 0)
{
// an error occurred reading the state.
return_code = state;
break;
}
// Print out "SET" if state != 0 or "UNSET" if state == 0.
printf("%6d rng state: %s\n", i++, (state ? "SET" : "UNSET"));
// store the current state
// block until line changes state
if((return_code = ioctl(fd, TIOCMIWAIT, &wait_on_ri)) < 0)
{
fprintf(stderr, "ioctl(TIOCMIWAIT) failed: %d: %s\n", errno, strerror(errno));
break; // break out of the while loop.
}
return_code = 0;
// make sure its the RI port
if ((return_code = ioctl(fd, TIOCGICOUNT, &serial_counter)) < 0)
{
fprintf(stderr, "ioctl(TIOCGICOUNT) failed: %d: %s\n", errno, strerror(errno));
break; // break out of the while loop.
}
return_code = 0;
// Notify that the count has changed.
if(ring_count != serial_counter.rng)
{
printf("Ring count changed from %d to %d\n", ring_count, serial_counter.rng);
ring_count = serial_counter.rng; // update the ring count.
}
else
{
// This should not happen, but if we print it out it will be easy to see if it does
// then we need to figure out why
printf("Ring count remains unchanged at %d\n", ring_count);
}
}
// clean up
close(fd);
return return_code;
}
У датчика движения есть выход, при срабатывании датчика на выходе появляется 12В.
Что же это за датчик-то такой? Я бы ожидал там увидеть лог. единицу.
Самый дешевый датчик движения
Самый дешевый датчик движения
Гугл мне тучу результатов дал, который из них?
Кстати, не обязательно подключать датчик именно к RI. У меня был случай,
что устройство выдавало сигнал DSR, соответственно я использовал TIOCM_DSR.
Пётр.