Thursday 16 June 2016

Particle Photon: Serial communication

I have known for a while that you can send data from the Photon to your computer via the serial connection and view it using the particle serial monitor. But I only recently found out that you can send data from your computer to the Photon too. For some reason I associated 'serial' with 'one-way' but it's really just 'one bit at a time'.

For example here is a small program that will echo back the data you send from your computer to the Photon if you have the serial monitor open:


 void setup() {  
   
   Serial.begin(9600);  
 }  
   
 void loop() {  
     
   int availableBytes = Serial.available();  
     
   if (availableBytes > 0) {  
      
     char message[availableBytes];  
      
     for(int i = 0 ; i < availableBytes ; i++) {  
        
       message[i] = Serial.read();  
     }  
   
     Serial.println(message);  
   }  
     
   delay(1000);  
 }  

You cannot send data to the Photon using the serial monitor as it can only receive data but you can send data using the CLI instead. On Linux you can run:


 $> echo -e "hello world" > /dev/ttyACM0  


This will send the characters 'hello world' over the serial connection to the Photon who will then read in the characters and print them back to the serial monitor.

After reading a bit about serial connections I found out:

1. Data is sent one bit at a time so it is pretty slow but adequate for talking to the Photon

2. Serial communication comes in 3 flavours:

  • Simplex - data is only sent in one direction

  • Half-duplex - data can be sent in 2 directions, but only one direction at a time
  • Full-duplex - data can be sent in 2 directions simultaneously


3. USB is a form of serial communication. USB 3.0 is full-duplex but earlier versions are only half-duplex. Full duplex data transfer helps USB 3.0 achieve it's super fast transfer speeds as it has dedicated wires for each communication direction.

No comments:

Post a Comment