Project

General

Profile

Revision 1224

Added by Rich Hong about 15 years ago

Parallelized Master and Server

View differences:

server.c
11 11
#define RD  3
12 12
#define FORWARD 1
13 13
#define BACKWARD 0
14
#define SENDER 1
15
#define RECEIVER 0
14 16

  
15
int main()
17
void setAttrib(int file)
16 18
{
17
	int serialFile = open("/dev/ttyUSB0", O_RDWR);
18
	if(serialFile < 1)
19
	{
20
		printf("Error opening serial\n");
21
		return -1;
22
	}
23
    	
24 19
	struct termios attributes;
25
    	tcgetattr(serialFile, &attributes);
20
    	tcgetattr(file, &attributes);
26 21

  
27 22
	cfsetispeed(&attributes, B115200);
28 23
	cfsetospeed(&attributes, B115200);
......
39 34
//	attributes.c_cc[VTIME] = 50;
40 35

  
41 36

  
42
	if (tcsetattr(serialFile, TCSANOW, &attributes) < 0){
37
	if (tcsetattr(file, TCSANOW, &attributes) < 0){
43 38
		perror("tcsetattr failed");
44 39
		exit(-1);
45 40
	}
41
}
42

  
43
int main()
44
{
45
	int serialFileIn = open("/dev/ttyUSB0", O_RDWR);
46
	int serialFileOut = open("/dev/ttyUSB1", O_RDWR);
47
	if(serialFileIn < 1 || serialFileOut < 1)
48
	{
49
		printf("Error opening serial\n");
50
		return -1;
51
	}
52
    	
53
	setAttrib(serialFileOut);
54
	setAttrib(serialFileIn);
46 55
        
47 56
	unsigned char encoders[4] = {1,1,1,1};
48
///	while(1)
49
//		write(serialFile, encoders, 1);
57
	
58
	char senderNum = SENDER;
59
	char receiverNum = RECEIVER;
60
	
61
	write(serialFileIn, &receiverNum, 1);
62
	sleep(1);
63
	write(serialFileOut, &senderNum, 1);
64
	
50 65
	//Sending velocity as LS LD RS RD
51 66
	int tempCount = 0;
52 67
	
......
62 77
		int count = 0;
63 78
		do
64 79
		{
65
			tcflush(serialFile, TCIOFLUSH);
66
			temp = write(serialFile, encoders + count, 1);	
80
//			tcflush(serialFile, TCIOFLUSH);
81
			temp = write(serialFileOut, encoders + count, 1);	
67 82
			if(temp < 0)
68 83
				perror("Write Error");
69 84
			count += temp;
70 85
			printf("sent: %d\n", count);
71
			usleep(1000);
86
			usleep(100);
72 87
		}while(count < 4);
73 88
		count = 0;
74 89
		printf("READING DATA\n");
75 90
		do
76 91
		{
77
			count += read(serialFile, encoders, 4);
92
			count += read(serialFileIn, encoders, 4);
78 93
		}while(count < 4);
79 94
		
80 95
		int leftEncoder = (encoders[0] << 8) | encoders[1];

Also available in: Unified diff