| 145 | */ |
| 146 | |
| 147 | int drcSetupSerial (const int pinBase, const int numPins, const char *device, const int baud) |
| 148 | { |
| 149 | int fd ; |
| 150 | int ok, tries ; |
| 151 | time_t then ; |
| 152 | struct wiringPiNodeStruct *node ; |
| 153 | |
| 154 | if ((fd = serialOpen (device, baud)) < 0) |
| 155 | return false ; |
| 156 | |
| 157 | delay (10) ; // May need longer if it's an Uno that reboots on the open... |
| 158 | |
| 159 | // Flush any pending input |
| 160 | |
| 161 | while (serialDataAvail (fd)) |
| 162 | (void)serialGetchar (fd) ; |
| 163 | |
| 164 | ok = false ; |
| 165 | for (tries = 1 ; (tries < 5) && (!ok) ; ++tries) |
| 166 | { |
| 167 | serialPutchar (fd, '@') ; // Ping |
| 168 | then = time (NULL) + 2 ; |
| 169 | while (time (NULL) < then) |
| 170 | if (serialDataAvail (fd)) |
| 171 | { |
| 172 | if (serialGetchar (fd) == '@') |
| 173 | { |
| 174 | ok = true ; |
| 175 | break ; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (!ok) |
| 181 | { |
| 182 | serialClose (fd) ; |
| 183 | return false ; |
| 184 | } |
| 185 | |
| 186 | node = wiringPiNewNode (pinBase, numPins) ; |
| 187 | |
| 188 | node->fd = fd ; |
| 189 | node->pinMode = myPinMode ; |
| 190 | node->pullUpDnControl = myPullUpDnControl ; |
| 191 | node->analogRead = myAnalogRead ; |
| 192 | node->digitalRead = myDigitalRead ; |
| 193 | node->digitalWrite = myDigitalWrite ; |
| 194 | node->pwmWrite = myPwmWrite ; |
| 195 | |
| 196 | return true ; |
| 197 | } |
no test coverage detected