[vc_row][vc_column width=”1/1″][vc_column_text]Spyder is is designed to transparently move serial data over the power line network at a speed of 2kbps, and achieves the target of replacing serial cables by the ubiquitous power line network.
[/vc_column_text][vc_tour interval=”0″ title=”Tips”][vc_tab title=”Set up” tab_id=”1387434322-1-0″][vc_column_text]Power up both spyder and pcDuino. Please use a RS232 to USB converter to connect spyder’s RS232 interface and a USB port on pcDuino.
[/vc_column_text][/vc_tab][vc_tab title=”Simple Code” tab_id=”1387434322-2-38″][vc_column_text]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#define FALSE -1
#define TRUE 0
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t wlock = PTHREAD_MUTEX_INITIALIZER;
void set_speed(int fd, int speed){
struct termios opt;
tcgetattr(fd,&opt);
tcflush(fd,TCIOFLUSH);
cfsetispeed(&opt,speed);
cfsetospeed(&opt,speed);
tcflush(fd,TCIOFLUSH);
}
int set_parity(int fd,int databits,int stopbits,int parity){
struct termios options;
if(tcgetattr(fd,&options)!=0){
perror(“Serial 1″);
return (FALSE);
}
tcflush(fd,TCIFLUSH);
options.c_cflag |= (CLOCAL| CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag &= CS8;
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
options.c_oflag &= ~OPOST;
options.c_oflag & ~(ONLCR | OCRNL);
options.c_iflag &= ~(ICRNL|INLCR);
options.c_iflag &= ~(IXON|IXOFF|IXANY);
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150;
options.c_cc[VMIN] = 0;
if(tcsetattr(fd,TCSANOW,&options)!= 0){
perror(“tcsetattr fd”);
return FALSE;
}
return TRUE;
}
int opendev(char *dev){
int fd= open(dev,O_RDWR|O_NONBLOCK);
if(FALSE ==fd){
perror(“open”);
return (FALSE);
}
else return fd;
}
void *recFun(void *args){
pthread_mutex_lock(&lock);
printf(“recFun_thread begin!\n”);
char buff[512];
int nread;
int *fd = (int *)(((void**)args)[0]);
// printf(“fd = %d”,*fd);
while(1){
if((nread = read(*fd,buff,512))> 0){
// printf(“\nLen %d\n”,nread);
buff[nread+1] = ”;
printf(“\n———————–\n”);
printf(“%s”,buff);
printf(“\n———————–\n”);
memset(buff,0,sizeof(char)*512);
sleep(1);
}
}
pthread_mutex_unlock(&lock);
}
void *sendFun(void *args){
pthread_mutex_lock(&wlock);
printf(“send_thread begin!\n”);
int *fd = (int *)(((void**)args)[0]);
write(*fd,”+++”,3);
printf(“+++ have sent\n”);
sleep(1);
write(*fd,”ATSN\n”,5);
printf(“ATSN have sent\n”);
sleep(1);
write(*fd,”520linksprite!\n”,15);
printf(“520linksprite have send!\n”);
pthread_mutex_unlock(&wlock);
}
int main(){
int fd;
char dev[50] = ”/dev/ttyUSB0″;
fd = opendev(dev);
printf(“fd = %d\n”,fd);
set_speed(fd,9600);
if(set_parity(fd,8,1,’N’) == FALSE){
printf(“Set Parity Error\n”);
exit(0);
}
void *arg[] = {&fd};
pthread_t pid1,pid2;
pthread_create(&pid1,NULL,recFun,arg);
pthread_create(&pid2,NULL,sendFun,arg);
pthread_join(pid1,NULL);
pthread_join(pid2,NULL);
close (fd);
pthread_mutex_destroy(&lock);
pthread_mutex_destroy(&wlock);
return 0;
}
[/vc_column_text][/vc_tab][vc_tab title=”Tips” tab_id=”1387435171426-2-3″][vc_column_text]
- What the dev array saves in main function in the code is the name of the device.
- Different USB port is related to different device name, we can check all USB device file by entering “ls /dev/ttyUSB*”in the terminal.
- Unplug spyder from pcDuino USB port, re enter “ls /dev/ttyUSB*” and see which USB device is gone.
- The disappeared is just the device file for spyder.
[/vc_column_text][/vc_tab][vc_tab title=”Results and explaination” tab_id=”1387435837252-3-9″][vc_column_text]You will get AT command mode after sending “+++”, the spyder will return a “ok”.
Send “ATSN”in AT command mode, check the spyder device name, it’s 633 for this sample project.
Try to send a wrong command “520linksprite”, spyder will return a ”invalid cmd”.
If there is no command within 15 seconds, spyder will return “exited” and exit AT command mode.
Leave a Reply
You must be logged in to post a comment.