本文整理汇总了C++中SoftwareSerial::read方法的典型用法代码示例。如果您正苦于以下问题:C++ SoftwareSerial::read方法的具体用法?C++ SoftwareSerial::read怎么用?C++ SoftwareSerial::read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SoftwareSerial
的用法示例。
在下文中一共展示了SoftwareSerial::read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: connect
// TODO: This is extremely kludgy right now.
// The delay()'s should be made more robust, faster by periodically checking for a response,
// while still checking for a timeout.
// Checking for just the first char of a response is a questionabl solution.
// Needs more testing. So many unknown states the BlueSMiRF could be in.
// We need some flow chart action here.
int SFE_MetaWatch::connect()
{
char c;
int timeout = CONNECT_TIMEOUT;
bt.print('\r'); // Clear any previous commands
delay(BLUETOOTH_RESPONSE_DELAY);
bt.flush();
// Entering command mode. Should either print "CMD" or "?" if already there
while ((c != 'C') && (c != '?') && (timeout > 0))
{
bt.print("$$$"); // Enter command mode
delay(BLUETOOTH_RESPONSE_DELAY);
c = bt.read(); // Read first character of response
bt.flush();
timeout--;
}
if (timeout == 0)
return -2;
else
timeout = CONNECT_TIMEOUT;
// After sending connect command, should print "TRYING", may also print "ERR-connected"
while ((c != 'T') && (c != 'E') && (timeout > 0))
{
bt.print("C,");
bt.print(watchAddress);
bt.print('\r');
delay(BLUETOOTH_RESPONSE_DELAY);
c = bt.read();
bt.flush();
timeout--;
}
// If there was an error, try to exit command mode
if ((c == 'E') || timeout == 0)
{
bt.print("---"); // Exit command mode
bt.print('\r');
delay(BLUETOOTH_RESPONSE_DELAY);
bt.flush();
}
if (timeout == 0)
return -1; // Return -1 if connect command error
else if (c == 'E')
return 2; // Return 2 if we think we're already connected
else
return 1; // Return 1 if all went according to plan
}
示例2: loop
void loop()
{
sendMsg = "";
recvMsg = "";
while (Serial.available()) {
sendMsg += (char)Serial.read();
delay(2);
}
if(sendMsg.length() > 0)
{
mySerial1.println(sendMsg);
Serial.print("I send: ");
Serial.println(sendMsg);
}
while (mySerial2.available()) {
recvMsg += (char)mySerial2.read();
delay(2);
}
if(recvMsg.length() > 0)
{
Serial.print("I recv: ");
Serial.println(recvMsg);
}
//delay(20);
}
示例3: loop
void loop()
{
if (Serial.available())
if (mySerial.available())
mySerial.println(Serial.read());
Serial.write(mySerial.read());
}
示例4: loop
void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}
示例5: readRFID
boolean readRFID() {
//if (RFID.available()) RFID.flush();
digitalWrite(RFID_ENABLE, LOW); // Activate the RFID reader
for (byte i = 0; i < RFID_LENGTH; i++)
rfidCode[i] = 0;
if (RFID.available()) {
int val = RFID.read();
Serial.print(val);
if (val == RFID_START_CHAR) {
RFID.readBytes(rfidCode, RFID_LENGTH);
Serial.print("RFID Read: ");
Serial.println(rfidCode);
digitalWrite(RFID_ENABLE, HIGH); // deactivate the RFID reader for a moment so it will not flood
RFID.flush(); // clear the buffer
delay(1500); // wait for a bit
return true;
}
}
return false;
}
示例6: setName
/* This function sets the name of an RN-42 module
name should be an up to 20-character value. It MUST BE TERMINATED by a
\r character */
uint8_t makeyMateClass::setName(char * name)
{
if (bluetooth.available())
bluetooth.flush(); // Get rid of any characters in the buffer, we'll need to check it fresh
bluetooth.print("SN,");
for (int i=0; i<20; i++)
{
if (name[i] != '\r')
bluetooth.write(name[i]);
else
break;
}
bluetooth.write('\r');
delay(BLUETOOTH_RESPONSE_DELAY);
bluetoothReceive(rxBuffer);
/* Double check the setting, output results in Serial monitor */
bluetooth.flush();
bluetooth.print("GN");
bluetooth.write('\r');
delay(BLUETOOTH_RESPONSE_DELAY);
Serial.print("Name set to: ");
while (bluetooth.available())
Serial.write(bluetooth.read());
return bluetoothCheckReceive(rxBuffer, "AOK", 3);
}
示例7: toSerial
void toSerial()
{
while(gprsSerial.available()!=0)
{
Serial.write(gprsSerial.read());
}
}
示例8: loop
void loop() // run over and over
{
int i = 0;
bool found = false;
raw_packet com;
while(usb.available()){
found = true;
if(i < 8){
com.raw[i] = usb.read();
} else{
break;
}
i++;
}
if(!found){
return;
}
packet curpacket = com.nice;
if(curpacket.cmd = 1){
usb.print("hi");
usb.print("\n");
}
}
示例9: sendData
String sendData(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(esp8266.available())
{
// The esp has data so display its output to the serial window
char c = esp8266.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
示例10: sendPacket
/* sendPacket() is called by just about every other member function. It calculates
some CRC bytes, then sends the message string.
If a response is requested, it'll return that in the response array. Otherwise
that and the responseLength variable should be 0.
If you're using a bluetooth module that's not the RN-42, this'd be the place
to modify.
*/
void SFE_MetaWatch::sendPacket(unsigned char * data, int length, unsigned char * response, int responseLength)
{
int crc = ComputeCRC(data, length - 2); // Get the crc values for our string
data[length-1] = (crc & 0xFF00) >> 8; // LSB goes first
data[length-2] = crc & 0xFF; // the MSB
// If you want a response, let's flush out the bt buffer first.
if (responseLength > 0)
bt.flush();
// Send the data out to the BlueSMiRF
for (int i=0; i<length; i++)
{
bt.write(data[i]);
}
// If a response was requested, read that into the response array.
if (responseLength > 0)
{
delay(BLUETOOTH_RESPONSE_DELAY);
int i=0;
while (bt.available() && (i < responseLength))
{
response[i++] = bt.read();
}
}
}
示例11: ncArduinoPollGetData
void ncArduinoPollGetData()
{
int c;
while (mySerial.available() > 0)
{
c = mySerial.read();
byteStreamToPackageStream((unsigned char) c);
}
}
示例12: setupBlueToothConnection
void setupBlueToothConnection()
{
blueToothSerial.begin(38400);
blueToothSerial.print("\r\n+STWMOD=1\r\n");
blueToothSerial.print("\r\n+STNA=SeeedBTMaster\r\n");
blueToothSerial.print("\r\n+STAUTO=0\r\n");
delay(2000);
blueToothSerial.flush();
blueToothSerial.print("\r\n+INQ=1\r\n");
Serial.println("inquiring");
delay(2000);
char recvChar;
/* while(1){ //this part is for auto detecting and connecting to a slave but something is wrong with the parsing of the address, use this to print out the address and then put it in manulely below
if(blueToothSerial.available()){
recvChar = blueToothSerial.read();
recvBuf += recvChar;
nameIndex = recvBuf.indexOf(slaveName);
if ( nameIndex != -1 ){
addrIndex = (recvBuf.indexOf(retSymb,(nameIndex - retSymb.length()- 18) ) + retSymb.length());
slaveAddr = recvBuf.substring(addrIndex, nameIndex);
break;
}
}
}*/
connectCmd += slaveAddr;
connectCmd += "\r\n";
int connectOK = 0;
Serial.print("Connecting to slave:");
Serial.print(slaveAddr);
Serial.println(slaveName);
do{
blueToothSerial.print("/*put slave address here*/");
recvBuf = "";
while(1){
if(blueToothSerial.available()){
recvChar = blueToothSerial.read();
recvBuf += recvChar;
if(recvBuf.indexOf("CONNECT:OK") != -1){
connectOK = 1;
Serial.println("Connected!");
blueToothSerial.print("Connected!");
break;
}else if(recvBuf.indexOf("CONNECT:FAIL") != -1){
Serial.println("Connect again!");
break;
}
}
}
}while(0 == connectOK);
}
示例13: connect
/* This function will attempt a connection to the stored remote address
The first time you connect the the RN-42 HID, the master device will
need to initiate the connection. The first time a connection is made
the bluetooth address of the master device will be stored on the RN-42.
If no remote address is stored, a connection will not be made. */
uint8_t makeyMateClass::connect()
{
freshStart(); // Get the module disconnected, and out of command mode
while (!enterCommandMode())
{ // Enter command mode
delay(BLUETOOTH_RESPONSE_DELAY);
}
delay(BLUETOOTH_RESPONSE_DELAY);
bluetooth.flush();
/* get the remote address and print it in the serial monitor */
bluetooth.print("GR"); // Get the remote address
bluetooth.write('\r');
delay(BLUETOOTH_RESPONSE_DELAY);
if (bluetooth.peek() == 'N') // Might say "No remote address stored */
{ // (bluetooth address is hex values only, so won'te start with 'N'.
Serial.println("Can't connect. No paired device!");
bluetooth.flush();
bluetooth.print("---"); // exit command mode
bluetooth.write('\r');
return 0; // No connect is attempted
}
else if (bluetooth.available() == 0)
{ // If we can't communicate with the module at all, print error
Serial.println("ERROR!");
return 0; // return error
}
/* otherwise print the address we're trying to connect to */
Serial.print("Attempting to connect to: ");
while (bluetooth.available())
Serial.write(bluetooth.read());
/* Attempt to connect */
bluetooth.print("C"); // The connect command
bluetooth.write('\r');
delay(BLUETOOTH_RESPONSE_DELAY);
while (bluetooth.available())
Serial.write(bluetooth.read()); // Should print "TRYING"
return 1;
}
示例14: read
String read()
{
String msg = "";
while (bluetoothModule->available() > 0)
{
char c = (char)bluetoothModule->read();
if(c != '\n')
msg += c;
}
return msg;
}
示例15: loop
void loop()
{
SoftwareSerial RFID = SoftwareSerial(rxPin,txPin);
RFID.begin(RFID_SPEED);
digitalWrite(LED,LOW);
if((val = RFID.read()) == 10)
{ // check for header
bytesread = 0;
while(bytesread<10)
{ // read 10 digit code
val = RFID.read();
if((val == 10)||(val == 13))
{ // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
// if 10 digit read is complete
if(bytesread == 10)
{
strCode.clear();
strCode.append(code);
if( strCode.equals(strOldCode.getChars()) ) {
// nop
}else{
Serial.print(code); // print the TAG code
digitalWrite(LED,HIGH);
strOldCode = strCode;
MsTimer2::start(); // Timer start
}
}
bytesread = 0;
delay(WAIT_INTERVAL); // wait for a second
}
}