TOP16 Ⅱのサンプルコードが何を表しているのかわかりません.
当方プログラミング初心者です.
TOP16 Ⅱを使用してるのですが,いただいたサンプルコードがどのような実行結果になるのかがわかりません.
以下にサンプルコードを載せます.
わかる方がいたら教えていただけないでしょうか.
serial_io.c
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf("error %d from tcgetattr", errno);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf("error %d from tcsetattr", errno);
return -1;
}
return 0;
}
void set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf("error %d from tggetattr", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
printf("error %d setting term attributes", errno);
}
serial_io.h
#ifndef _SERIAL_IO_H
#define _SERIAL_IO_H
void set_blocking (int fd, int should_block);
int set_interface_attribs (int fd, int speed, int parity);
#endif
top 16Ⅱ.c
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <errno.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "serial_io.h"
#define BAUDRATE (B115200)
// The device can be found using command "ls /dev/tty*"
//
#define DEVICE ("/dev/ttyUSB0")
// useful command to clear the terminal screen
//
#define clrscr() printf("\e[1;1H\e[2J")
//----------------------------------------------------------------------
// Read analog input and return the value
//
//
// port = serial device handle
//
//----------------------------------------------------------------------
double TOP16II_readAnalog(int port, int analog_input)
{
int char_count = 0;
double ret_value = 0.0;
char buf[100];
char sendString[20];
// valid input number ?
//
if (analog_input < 1)
{
return(0.0);
}
if (analog_input > 8)
{
return(0.0);
}
tcflush(port, TCIFLUSH); // Discards old data in the rx buffer
sprintf(sendString, "#z%d\r", analog_input);
write(port, sendString, 4); // send 4 character command
//printf("Write %s\n", sendString);
tcflush(port, TCIFLUSH);
usleep (100000); // ** sleep enough to process and reply
char_count = read(port, &buf, 10); // read 10 character response
//printf("GOT %s\n", buf);
if (char_count == 10)
{
// correct number of characters received
//
buf[8] = 0 ;
ret_value = atof(&buf[1]);
}
return(ret_value);
}
//---------------------------------------------------------------------
// Set a digital output state
//
// on_off = the new setting
// port = serial device handle
//
// digital_output = the output to set
//
//
// Returns:
//
// 0 = success
// < 0 = error
//
//----------------------------------------------------------------------
double TOP16II_setDigital(int port, int digital_output, int on_off)
{
int char_count = 0;
int ret_value = -1;
char buf[50];
char sendString[20];
int output = 0 ; // the new setting
int mask = 1; // what bits will be affected ?
// valid output number ?
//
if (digital_output < 1)
{
return(-1);
}
if (digital_output > 8)
{
return(-1);
}
mask <<= (digital_output-1); // set bit position for the output
if (on_off == 1)
{
// turn the digital output ON
//
output = mask;
}
tcflush(port, TCIFLUSH); // Discards old data in the rx buffer
// format string into 8 bit 'hex' numbers
//
//printf("%d %d\n", output, mask);
sprintf(sendString, "#%02x%02x\r",output, mask );
write(port, sendString, 6); // send 4 character command
//printf("Write %s\n", sendString);
tcflush(port, TCIFLUSH);
usleep (100000); // ** sleep enough to process and reply
char_count = read(port, &buf, 5); // read 5 character response
//printf("GOT %s\n", buf);
if (char_count == 5)
{
// correct number of characters received (>OK[CR][LF])
//
ret_value = 0;
}
return(ret_value);
}
//----------------------------------------------------------------------
// Read digital inputs and return the value (8 bits)
//
// port = serial device handle
//
//----------------------------------------------------------------------
unsigned int TOP16II_readDigital(int port)
{
int char_count = 0;
unsigned int ret_value = 0;
char buf[100];
char sendString[20];
tcflush(port, TCIFLUSH); // Discards old data in the rx buffer
sprintf(sendString, "#x%c\r",0);
write(port, sendString, 4); // send 4 character command
//printf("Write %s\n", sendString);
tcflush(port, TCIFLUSH);
usleep (100000); // ** sleep enough to process and reply
char_count = read(port, &buf, 5); // read 5 character response
//printf("GOT %s\n", buf);
if (char_count == 5)
{
// correct number of characters received
//
buf[3] = 0 ; // null terminate the string
ret_value = atoi(&buf[1]);
}
return(ret_value);
}
// -----------------------------------------------------
int main(int argc, char **argv)
{
int i = 0;
char *portname = DEVICE;
double analog_reading1 = 0.0;
double analog_reading2 = 0.0;
double analog_reading3 = 0.0;
double analog_reading4 = 0.0;
double analog_reading5 = 0.0;
double analog_reading6 = 0.0;
double analog_reading7 = 0.0;
double analog_reading8 = 0.0;
unsigned int digital_in = 0;
int output_count = 1;
int fd = 0 ;
printf("Start\n");
fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
printf("error %d opening %s: %s", errno, portname, strerror (errno));
return -1;
}
set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity)
set_blocking (fd, 0); // set no blocking
// loop
for(i = 0 ; i < 500; i++)
{
analog_reading1 = TOP16II_readAnalog(fd, 1);
analog_reading2 = TOP16II_readAnalog(fd, 2);
analog_reading3 = TOP16II_readAnalog(fd, 3);
analog_reading4 = TOP16II_readAnalog(fd, 4);
analog_reading5 = TOP16II_readAnalog(fd, 5);
analog_reading6 = TOP16II_readAnalog(fd, 6);
analog_reading7 = TOP16II_readAnalog(fd, 7);
analog_reading8 = TOP16II_readAnalog(fd, 8);
digital_in = TOP16II_readDigital(fd);
clrscr(); // clear the screen
printf("Analog readings = %f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n",\
analog_reading1, analog_reading2, analog_reading3, analog_reading4,\
analog_reading5, analog_reading6, analog_reading7, analog_reading8);
printf("Digital inputs: %x\n", digital_in);
// --- output test ---
//
TOP16II_setDigital(fd, output_count, 0); // Turn an output OFF
if (output_count >= 8)
{
// last output reached ?, go back to 1
//
output_count = 0;
}
output_count++; // next output
TOP16II_setDigital(fd, output_count, 1); // Turn an output ON
usleep(100000);
}
close(fd);
return 0;
}