Hi!
I need your help again. I'm trying to send data characters from C program through serial communication to Arduino.
But, it seems that microchip reads values L(low) and H(high) just for 5 seconds and then it gets silent or stucked. Do I need to set a buffer in the Arduino code?
Here I attach samples of the codes. Please take a look...
Thanx,
Tatjana
....
//Arduino code
int ledPin = 13; //select the pin for LED
int controlPin = 12;
int val;
int serialGet = 0; //variable to store the value coming from the serial port
int delaytime = 1; //delaytime for the main loop
//int timer;
void setup() {
pinMode(ledPin, OUTPUT); //declared LED as output
beginSerial(9600);
pinMode(controlPin, OUTPUT);
}
void loop()
{
// if (Serial.available() > 0)
serialGet = serialRead(); //read the serial port
if (serialGet != -1) { //if the input is -1, then there is no data
val = serialGet; //otherwise store it
//Serial.print("Nothing");
}
if (val == 'H') {
digitalWrite(ledPin, HIGH);
Serial.print("I received: ");
Serial.println(val);
val=0;
} else if(val == 'L') {
digitalWrite(ledPin, LOW);
Serial.print("I received: ");
Serial.println(val);
val=0;
}
digitalWrite(controlPin, HIGH);
delay(1000);
digitalWrite(controlPin, LOW);
delay(1000);
delay(delaytime);
}
......
//C code
/* Standard input/output definitions /
#include <stdio.h>
/ String function definitions /
#include <string.h>
/ UNIX standard function definitions /
#include <unistd.h>
/ File control definitions /
#include <fcntl.h>
/ Error number definitions /
#include <errno.h>
/ POSIX terminal control definitions */
#include <termios.h>
#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyUSB0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
int fd;
int n;
//FILE *input;
int output;
char readBuf[255]; //buffer for where data is put
int main(void)
{
//init serial stuff
struct termios oldtio, newtio;
/* open the device to be non-blocking (read will return immediatly) */
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (fd <0) {
perror(MODEMDEVICE);
return(-1);
}
tcgetattr(fd,&oldtio); /* save current port settings /
/ set new port settings for canonical input processing */
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
//fd_set input;
fd_set output;
struct timeval timeout;
int i;
int res;
int paramValue;
FD_ZERO(&output);
FD_SET(fd, &output);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
for( i = 0; i < 500; i++){
if (i % 2){
printf("H\n");
write(fd, "H", 1);
} else {
printf("L\n");
write(fd, "L", 1);
}
sleep(2);
}
FD_ZERO(&output);
FD_SET(fd, &output);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
return 0;
}