My setup:
Android device =(WiFi)=> Linksys router (WRT54G) =(serial)=> Arduino
Android device : using sockets IO - connects to router
Linksys router : Linux sockets IO - accepts a client
Arduino : reads data serially from router. I have soldered a serial port on the router.
Serial buad rate is 9600 so things are in sync.
The issue:
I can see the server printing data properly to the console but I see strange characters in the Arduino console. Here is what I see:
From Android device: up
Router console:
<Received : up
Arduino console:
received: BELBELBELBELBELBELBELBELBELBELBELBELBELBELBEL up
received:
I have no idea where this BEL character is coming from?
My experimentation:
If I do this from router console window:
$> echo "hello" > /dev/tts/1
I get proper data on Arduino side i.e. I get this:
received: hello
received:
My code:
Router code
int fd;
char *portname = "/dev/tts/1";
fd = open(portname, O_WRONLY);
if (fd < 0)
{
printf("Error : cannot open port %s\n", portname);
return -1;
}
...
connfd = accept(listenfd, (struct sockaddr*)&serv_addr, &len);
printf("accept OK!\n");
printf("accepted connection from %s\n", inet_ntoa(serv_addr.sin_addr));
char rcvbuf[MAX_BUFF_SIZE] = { '\0' };
while(1)
{
int inData = recv(connfd, rcvbuf, sizeof(rcvbuf), 0);
if (inData <= 0)
{
printf("Client closed!\n");
break;
}
int result = write(fd, rcvbuf, strlen(rcvbuf));
printf("<Received : %s\n", rcvbuf);
// reset memory otherwise we will have data
// from previous data
bzero((char *)rcvbuff, sizeof(rcvbuf));
}
...
Arduino Code:
// the loop routine runs over and over again forever:
void loop() {
while (Serial.available() > 0)
{
char inChar = (char)Serial.read();
if (inChar == '\n' || inChar == '\r' || inChar == '\n\r' || inChar == '\r\n')
{
DATA_READ = true;
break;
}
inputString += inChar;
}
// send data only when you receive data:
if (DATA_READ)
{
if (inputString[0] != ' ' || inputString[0] != '\n')
{
Serial.print("received: ");
Serial.println(inputString + " " + inputString[0]);
process_message(inputString[0]);
// prepare for next round
DATA_READ = false;
Serial.flush();
inputString = "";
}
}
}
Any *pointers will be appreciated :)
EDIT
added baud rate for more info:
Router
root@OpenWrt:/usr/bin# stty -F /dev/tts/1 -a
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ;
eol2 = ; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;
werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
Arduino:
Serial.begin(9600);
delay(50);
BEL
or'\7'
smells like mis-matched Baud. – chux - Reinstate MonicainputString += inChar;
does not look like C to me. Maybe you are using C++ ? – wildplasser