Skip to content

Instantly share code, notes, and snippets.

@siyo
Created August 5, 2012 15:37
Show Gist options
  • Save siyo/3265467 to your computer and use it in GitHub Desktop.
Save siyo/3265467 to your computer and use it in GitHub Desktop.
糞rs232c
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
int main( void )
{
int i;
int fd;
int n_data;
struct termios tio;
int err;
/* char sData[1]; */
unsigned char rData[255];
if ((fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY |
O_NONBLOCK))<0) {
puts("ERROR: can not open /dev/ttyUSB0"); return -1;
}
memset(&tio, 0x00, sizeof(tio));
tio.c_cflag = B1200 | CS8 | CLOCAL | CREAD;
tio.c_iflag = IGNPAR | ICRNL;
tio.c_oflag = 0;
tio.c_lflag = 0;
tio.c_cc[VTIME] = 0;
tio.c_cc[VMIN] = 1;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &tio);
fcntl(fd, F_SETFL, FNDELAY);
/* sData[0] = ' '; */
memset( rData, 0x00, sizeof(rData) );
while(1){
/* // 1キャラクタ送信 */
/* err = write( fd, sData, 1); */
/* sData[0]++; */
/* if( sData[0]>='z' ){ */
/* sData[0] = ' '; */
/* } */
// 受信
n_data = read( fd, rData, sizeof(rData) );
if(n_data == -1)
continue;
printf("\nSIZE:%d\n",n_data);
for(i=0; i<n_data; i++){
printf( "0x%02x\t", rData[i] ); // 受信データ表示
}
/* rData[0] = 0x00; */
// 500 * 10 mSecのWAIT
usleep( 500000 * 10 );
}
close( fd );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment