The SoftwareSerial library is used to send the GPS data to the Arduino board and the Arduino board streams the same data to the PC (via the COM port). This data on the system's COM port is the one that is displayed when the Serial Monitor window is opened using the Arduino IDE.
Thus, to fetch the required data from Arduino, one will have to just read from the COM port to which the Arduino is connected. You can follow the procedure mentioned below fetch the data from the GPS sensor using Arduino-
- You will, thus, have to declare a serial port using the serial command and set the specified properties.
- Also, you can change the properties of the serial port using the set(serial) function.
- The following step will be to connect the serial port object to the device using fopen(serial) function.
- The below function fgets(serial) will help you fetch the file string, i.e., the NMEA strings that are obtained from the GPS sensor.
- The next step will be to disconnect the serial port object from the device using the fclose(serial) function.
- The final step will be to delete the serial object from the workspace using the delete(serial) function.
An example code snippet that will yield the NMEA strings on MATLAB is as below.
s=serial('COM4', 'InputBufferSize', 600, 'BaudRate', 9600);
data = cell(0);
Note that, the COM port that was allocated to the Arduino was 'COM4' on my machine. This parameter value will change depending on the COM port that Arduino is assigned on the user's system.
Since, from the link provided, the number of lines at the output of the Serial Monitor is seventeen, the counter has been set to 17 (This includes the blank lines). However, this code can be customized depending on the requirement.
Thus, the 'data' cell array will hold the strings that are fetched by reading each line from the 'COM4' port.