Description

bool() – indicates if the can2ser link is ready. This should only be used if we expect handshake messages from our peer otherwise it will not return true. The function can typical be found in sketches that like to ‘talk’ to the PC-can2ser-monitor or if two devices maintain a link and want to make sure that the partner is available.. Be aware that begin() has to be called on the instance before using the bool() function. (for Serial this is not necessary since it is already be called in the start-up routine).

Syntax

if (Serial)

Parameters

None

Returns

true: if handshake was received from the peer.
false: if no handshake was received yet from the peer.

Example Code

void setup()
{  
  // Wait for the PC can2ser-monitor to send handshakes
  while(!Serial) {;}
  Serial.println("Hello. Here is Serial");
}
void loop () 
{
  if(Serial) // LED indicates the PC handshake is OK
    digitalWrite(LED_BUILTIN, HIGH);
  else       // LED indicates the PC handshake got lost (10 sec timeout)
    digitalWrite(LED_BUILTIN, LOW);
}

Notes and Warnings

If your sketch makes use of this method and you plan to use the application in situations without a peer (e.g. without PC can2ser monitor connected), then make sure that your sketch does not get stuck due to not receiving any handshake messages from e.g. the PC-can2ser monitor.

The CAN communication is handled by the core code which needs to regularly been given processing time. This happens during ‘delay(), ‘yield()’ and at the end of a ‘loop()’ iteration. So always use one of these functions when in a for or while loop.

while(1);               don't
while(1) { yield(); }   do

Note: As a fall back system the CAN2SER class has the i_HANDSHAKE_TIMEOUT setting. This property defines the time that may pass waiting for handshake messages from the peer. After this time passed by we resume the communication without considering handshake from the peer.

Compatibility

At least during development most Arduino sketches use Serial to send and receive text to the user (PC). ‘Normal’ Arduinos are using a USB-Serial converter on their boards. Since the PC needs time to enumerate the USB device, the sketch would wait for this to happen by making use of the bool method. For the CAN2SER object this is similar. It gives the can2ser monitor time to be started so that the user never misses out any data from the device. This the reason why the can2ser monitor starts with sending ‘handshake’ messages by default.

CAN2SER monitor sending handshakes by default

See also

Example to create a serial link between two devices
Example to create an additional link to the PC
CAN2SER Class
begin()
i_HANDSHAKE_TIMEOUT