Hello eried
Thanks but I'm on Linux so your Win prog won't work here.
Anyways If someone can benefit from the source here it is.
I'm not familiar with the library but everything seems okay except for the first line issue.
Just change the name of the serial port in the serialport_init function(fd=open...) to the one your adruino is on in /dev
The baud rate is 9600
It will listen on that port and print whatever the arduino sent in the console.
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <stdint.h> /* Standard types */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <sys/ioctl.h>
#include <getopt.h>
int serialport_init(void);
void clear_string();
int serialport_read_until(int fd,char until);
int serialport_write(int fd, const char* str);
char b[1];
char buf[256];
char sendbuf[256];
int i=0;
int len;
int w;
int main()
{
int fd = 0;
int StatusRec=3;
//sendbuf[0]='2';
// sendbuf[0]='9';
// sendbuf[1]='9';
//sendbuf[2]='\n';
// sendbuf[3]='\0';
fd = serialport_init();
usleep(200);
while(1){
do{
StatusRec=serialport_read_until(fd,'\n');
//usleep(10);
}while(StatusRec==-1||StatusRec==2);
//printf("__%d__",StatusRec);
printf("%s",buf);
/* Send data to the arduino with this... If using sketch below just un-comment this
printf("What shall be sent?");
scanf("%s",&sendbuf);
sendbuf[strlen(sendbuf)]='\n';
sendbuf[strlen(sendbuf)+1]='\0';
printf("%s\n",sendbuf);
serialport_write(fd,sendbuf);
*/ clear_string();
usleep(1000);
}
}
int serialport_init(void)
{
struct termios toptions;
int fd;
int baud=B9600;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("init_serialport: Unable to open port ");
return -1;
}
if (tcgetattr(fd, &toptions) < 0) {
perror("init_serialport: Couldn't get term attributes");
return -1;
}
speed_t brate = baud;
brate=baud;
cfsetispeed(&toptions, B9600);
cfsetospeed(&toptions, B9600);
// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~CRTSCTS;
toptions.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
toptions.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // make raw
toptions.c_oflag &= ~OPOST; // make raw
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;
if( tcsetattr(fd,TCSANOW , &toptions) < 0) { //Was TCSAFLUSH
perror("init_serialport: Couldn't set term attributes");
return -1;
}
return fd;
}
void clear_string()
{
int f;
for(f=0;f<256;f++){
buf[f]=0;
}
i=0;
}
int serialport_read_until(int fd,char until)
{
do {
int n = read(fd, b, 1); // read a char at a time
if( n==-1) return -1; // couldn't read
if( b[0]==0||n==0 ) {
usleep( 10 * 1000 ); // wait 10 msec try again
printf("^^");
continue;
}
else{
buf[i] = b[0]; i++;
}
} while( b[0] != until );
if(b[0]==until){
buf[i] = 0; // null terminate the string
return 0;
i=0;
b[0]=0;
}
else{
return 2;
}
}
int serialport_write(int fd, const char* str)
{
int len = strlen(str);
int n = write(fd, str, len);
if( n!=len )
return -1;
return 0;
}
As for the arduino code you can add just a few Serial.println()'s
The code below will listen for input(int) when it receives input it will delay that much and then send the input back to the pc.
If you input 99 it will blink led 13 twce and then send back the input
Just make sure to un-comment the send part in the code above.
#include <HardwareSerial.h>
void init_process_string(void);
char STRING[128];
int serial_count;
int delaytime=0;
int ledpin=13;
void setup(){
pinMode(ledpin,OUTPUT);
delay(500);
Serial.begin(9600);
delay(700);
Serial.println("start");
Serial.flush();
delay(500);
}
void loop()
{
char c;
if (Serial.available() > 0)
{
c = Serial.read();
if (c != '\n')
{
STRING[serial_count] = c;
serial_count++;
}
}
//mark no data.
else
{
delayMicroseconds(100);
}
//if theres a pause or we got a real command, do it
if (serial_count && (c == '\n'))
{
delaytime=atoi(STRING);
delay(delaytime);
if(delaytime==99){
digitalWrite(ledpin,LOW);
delay(1000);
digitalWrite(ledpin,HIGH);
delay(5000);
digitalWrite(ledpin,LOW);
delay(1000);
digitalWrite(ledpin,HIGH);
delay(5000);
digitalWrite(ledpin,LOW);
}
Serial.println(delaytime,DEC);
Serial.flush();
//clear command.
init_process_string();
}
}
void init_process_string()
{
//init our command
for (byte i=0; i<128; i++)
STRING[i] = 0;
serial_count = 0;
}