Error reading data from USB ports on Linux

Hi everyone,

I am running into debugging problems with my C++ code. For some reason when I try to read data from a port the output is blank and the code stops on the spot. I wrote udev rules for the program, so the names I assigned to the ports are valid. Maybe someone can tell me why it comes up with this odd error. Here is the code (problem is on line 121):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <sstream>
 #include <cmath>
 #include <iostream>

 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdio.h>
 #include <limits.h>
 #include <string.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <termios.h>
 #include <unistd.h>

 #define PI 3.14159265358979323846264338327950288419716

 const double CIRC=0.7853981633974483, R=0.55, RATIO=127, VAL=2.0;		// initialize constants

 int main(int argc, char **argv)
 {
	//declare all variables
	int ms1, ms2, ms3, ms4, omnibotPoseInt[4];				
	int i=0, j=0, k=0, wr, rd, x=0, y=0, z=0, fd0, fd1, fd2, numSent=0;
 	char parseChar[1], stringIn[50], mc_char, *temp;


	//set settings for HCS12 microcontroller

 	system("stty -F /dev/HCS12 9600 cs8 -cstopb -parity -icanon hupcl -crtscts min 1 time 1");	


 	//attempt to open serial port connected to the microprocessor

 	fd0 = open("/dev/HCS12",O_RDWR | O_NOCTTY | O_NDELAY);	


 	//check for errors opening port	

 	if (fd0 == -1 )

 	{

		printf("open_port: Unable to open /dev/HCS12\n");

 	}

 
 	else //if no error

 	{

 		fcntl(fd0,F_SETFL,0);

		printf("Test Port HCS12 has been successfully opened and %d is the file description\n",fd0);	

 	}

	//settings for motor drivers
	system("stty -F /dev/Driver12 9600 cs8 -cstopb -parity -icanon hupcl -crtscts min 1 time 1");	

	//open serial port connected to motor driver for motors 1 and 2
	fd1 = open("/dev/Driver12",O_RDWR | O_NOCTTY | O_NDELAY);		
	
	if (fd1 == -1 )

	{

		perror("open_port: Unable to open /dev/Driver12\n");

	}		

	else // if no error

 	{

 		fcntl(fd1,F_SETFL,0);

		printf("Test Port Driver12 has been successfully opened and %d is the file description\n",fd1);

 	}
	
	//settings for motor drivers
	system("stty -F /dev/Driver34 9600 cs8 -cstopb -parity -icanon hupcl -crtscts min 1 time 1");

	//open serial port connected to motor driver for motors 3 and 4
	fd2 = open("/dev/Driver34",O_RDWR | O_NOCTTY | O_NDELAY);		

	if (fd2 == -1 )

	{

		perror("open_port: Unable to open /dev/Driver34\n");

	}

	else // if no error

 	{

 		fcntl(fd2,F_SETFL,0);

		printf("Test Port Driver34 has been successfully opened and %d is the file description\n",fd2);

 	}

	while (joyTalker.ok())
	{

		parseChar[0]=0;	  

		for(j=0; j<50; j++)
		{
			stringIn[j]=0;					//initialize the string
		}

		i=0;

		while(parseChar[0] != 10)
		{
			rd=read(fd0,parseChar,1);
			printf("%s ",parseChar);
			if ((parseChar[0] > 43) && (parseChar[0] < 58) || (parseChar[0]==32))
			{
				stringIn[i]=parseChar[0];
				i++;
			}
		}

		// Parse the msg from the node
		temp = strtok(stringIn,",");
			
		k=0;

		while(temp != NULL)
		{
			omnibotPoseInt[k] = atoi(temp);
			temp = strtok(NULL,",");
			k++;
		}
		
		//conversion to valid motor speeds
		x = omnibotPoseInt[0]/VAL;
		y = omnibotPoseInt[1]/VAL;
		z = omnibotPoseInt[2]/VAL;  	

		//printf("omnibotPoseInt[0]: %d, omnibotPoseInt[1]: %d, omnibotPoseInt[2]: %d", omnibotPoseInt[0], omnibotPoseInt[1], omnibotPoseInt[2]);			
	
		//kinematics of the omnimaxbot according to mecanum wheels
		ms1 = ((x/sqrt(2))+y+(R*tan((z/RATIO)*(2*PI))))/CIRC;
		ms2 = (-(x/sqrt(2))+y+(R*tan((z/RATIO)*(2*PI))))/CIRC;
		ms3 = ((x/sqrt(2))+y-(R*tan((z/RATIO)*(2*PI))))/CIRC;
		ms4 = (-(x/sqrt(2))+y-(R*tan((z/RATIO)*(2*PI))))/CIRC;

		//motor controller language, specified by an exclamation mark, a character (a or A is channel 1, b or B is channel 2), and then the speed

		if(ms1<0)				//Motor Controller 1, Channel 1, Motor 1
		{
			ms1=-ms1;
			mc_char = 'a';			//move backwards
		}
		else
		{
			mc_char = 'A';			//move forwards 
		}

		//write/send data to the specified port
		numSent = sprintf(stringIn, "!%c%d", mc_char, ms1);	
		wr = write(fd1,stringIn,numSent);

		if(ms2<0)				//Motor Controller 1, Channel 2, Motor 2
		{
			ms2=-ms2;
			mc_char = 'b';			//move backwards			
		}
		else
		{
			mc_char = 'B';			//move forwards
		}

		numSent = sprintf(stringIn, "!%c%d", mc_char, ms2);
		wr = write(fd1,stringIn,numSent);

		if(ms3<0)				//Motor Controller 2, Channel 1, Motor 3
		{
			ms3=-ms3;
			mc_char = 'a';					
		}
		else
		{
			mc_char = 'A';		
		}

		numSent = sprintf(stringIn, "!%c%d", mc_char, ms3);
		wr = write(fd2,stringIn,numSent);

		if(ms4<0)				//Motor Controller 2, Channel 2, Motor 4
		{
			ms4=-ms4;
			mc_char = 'b';			
		}
		else
		{
			mc_char = 'B';
		}			

		numSent = sprintf(stringIn, "!%c%d", mc_char, ms4);
		wr = write(fd2,stringIn,numSent);			       

		printf("got this string %s\n",stringIn);
	}
 	
 return 0;
 }
It's because I'm using udev rules. I used the default ttyUSB* names and now it works.
Topic archived. No new replies allowed.