Language - C SBC Linux GCC: Difference between revisions

From Phidgets Support
(Redirected page to Language - C)
 
(17 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{NoTitle}}
#REDIRECT [[Language - C]]
{{Language_-_C_Dev_Environment_Table}}
{|
|style="vertical-align:middle; width: 60%;"|
<font size=6>'''Language - C'''
 
'''SBC Linux with GCC'''</font>
 
Welcome to using Phidgets with C! By using C, you will have access to the complete Phidget22 API, including events.
 
GCC is a compiler system for originally written for GNU, and is the standard compiler on unix-like operating systems like Linux. It allows compilation of C programs from the command line.
|{{TOC limit|2}}
|}
==Getting Started With the Phidget SBC==
Welcome to using the Phidget SBC. If you haven't already, check out the [[SBC3003 User Guide#Getting Started | user guide]] in order to set up the following:
* Networking
* Administrator password
 
 
If you are ready to go, the first step will be deciding how you will use the SBC:
*Use a more powerful external computer to develop your code, and then simply copy the files to the SBC.
*Use the SBC like any other Linux computer, simply connect a monitor and a keyboard and begin your development.
 
 
This guide will cover development using an external machine. For development using the SBC itself, check the [[Language_-_C_Linux_GCC|GCC (Linux)]] page.
 
To begin, this video will help you get started:
<center>{{#ev:youtube|8vv8kx4743c}}</center>
 
 
===Developing with an External Computer===
There are two main ways in which you can access your SBC from an external computer:
*SBC Web Interface
*Secure Shell (SSH)
 
The [[SBC3003 User Guide#Getting Started | user guide]] covers the SBC Web Interface in detail, so we will review SSH below.
 
====SSH====
If you are unfamiliar with SSH, it is a simple yet powerful tool that allows you to log into a remote machine in order to execute commands. You can also transfer files using the associated SCP tool.
 
In order to use SSH, you need to know the following things about the SBC:
*IP address (e.g. 192.168.3.195) or the link local address (e.g. phidgetsbc.local)
*The administrator password
 
You must also enable SSH on the SBC. You can do this via the SBC Web Interface which is shown in the image below:
[[File:phidgetsbc_enableSSH.PNG|link=|alt=|center]]
 
=====SSH on Windows=====
To use SSH on Windows, we recommend [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html PuTTY]. Use the images below as a guide for configuring PuTTY (use the IP address or the link local address interchangeably):
[[File:Windows_PuttyIP.PNG|link=|alt=|left|425x411px]]
[[File:Windows_PuttyLL.PNG|link=|alt=|425x411px]]
 
 
After clicking open, simply login as root and provide the administrator password:
[[File:Windows_PuttyLogin.PNG|link=|alt=|center]]
 
 
To transfer files between your SBC and Windows machine, we recommend either of these programs:
* [https://winscp.net/eng/download.php WinSCP]
* [http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html PuTTY PSCP]
 
You will follow a similar process to access the SBC as described for SSH.
 
=====SSH on Linux and macOS=====
SSH is available on Linux and macOS by default. To run SSH, simply open the terminal and type the following:
<syntaxhighlight lang=bash>
ssh root@phidgetsbc.local
</syntaxhighlight>
Or, something like this (you will need to know the IP address of your SBC):
<syntaxhighlight lang=bash>
ssh root@192.168.3.195
</syntaxhighlight>
 
You will then be prompted for the password in order to gain access to the SBC:
[[File:macOS_SSH.png|link=|alt=|center]]
 
 
To copy a file from the SBC to your development machine using SCP, simply open the terminal and type the following:
<syntaxhighlight lang=bash>
scp root@phidgetsbc.local:/path/to/source /path/to/destination
</syntaxhighlight>
You can reverse this if you want to transfer a file from your development machine to your SBC:
<syntaxhighlight lang=bash>
scp  /path/to/source root@phidgetsbc.local:/path/to/destination
</syntaxhighlight>
 
===Installing Packages for Development===
At this point you have connected to the SBC through one or more these three options:
*SBC Web Interface
*SSH
*Directly via monitor and keyboard
 
Now that you are connected, you may want to start developing on/for the SBC. Before you do this, you need to install some packages.
 
The simplest way to install C support on the SBC is via the install buttons on located on the SBC Web Interface (System->Packages). Check ''Include full Debian Package Repository'' before installing.
 
 
[[File:phidgetsbc_installpackages.PNG|link=|alt=|center]]
 
You're now ready to begin programming! Continue through this guide for code examples and directions on where to go next.
 
 
==Use Our Examples==
'''''For simplicity, if you have not used Phidgets before, we recommend trying them out directly on an external development machine like a desktop computer. For C development, check the [[Language_-_C_Windows_GCC|GCC (Windows)]], [[Language_-_C_macOS_GCC|GCC (MacOS)]], or [[Language_-_C_Linux_GCC|GCC (Linux)]] depending on your operating system.'''''
 
One of the best ways to start programming with Phidgets is to use our example code as a guide.
 
Select an example that will work with your Phidget:
*{{SampleCode|C|C Examples}}
 
When developing on an external computer, you will write, compile, and test your programs on that machine. When you are ready, you will then upload your programs to the SBC to compile and run them.
 
Follow these steps to get our examples running on an SBC:
 
1. Download the example files onto the development machine.
 
2. Using the SBC Web Interface, create a new project:
[[File:Phidgetsbc_createproject.PNG|link=|alt=|center]]
 
3. Transfer all the example files from the development machine to the SBC, either using the SBC Web Interface or a tool like [https://winscp.net/eng/download.php WinSCP].
 
4. Use SSH to access the SBC terminal
 
5. Navigate to the project folder using the command:
 
<syntaxhighlight lang=bash>
cd /usr/userapps/ProjectName
</syntaxhighlight>
 
6. Navigate to the example folder using the command:
 
<syntaxhighlight lang=bash>
cd ExampleName
</syntaxhighlight>
 
7. Compile the example by entering the following command in the terminal:
 
<syntaxhighlight lang='bash'>
gcc example.c ../Common/PhidgetHelperFunctions.c -o example -I../Common -lphidget22
</syntaxhighlight>
 
8. You can now run the program by clicking the Start button on the SBC web interface.
[[File:Phidgetsbc_runprogram.PNG|link=|alt=|center]]
 
 
9. You'll note that as it runs, there are two links below the Stop button:
*stdout: view the program output like you would in a terminal or command prompt
*stderr: view the program error output
 
 
Success! The example is running on your SBC. If you aren't interested in developing directly on the Phidget SBC, jump ahead to [[#Running a program automatically|running a program automatically]].
 
{{Edit_the_Examples}}
 
{{Language_-_C_Editing_The_Examples}}
 
{{Language_-_C_Write_Code}}
 
==Setting up a New Project==
When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget C library.
 
To include the Phidget C library, add the following line to your code:
<syntaxhighlight lang='C'>
#include <phidget22.h>
</syntaxhighlight>
 
You can now compile the file as shown in the [[#Use Our Examples|Use Our Examples]] section.
 
The project now has access to Phidgets.
 
{{SBC_Running_A_Program_Automatically}}
 
{{Language_-_C_Further_Reading}}

Latest revision as of 16:35, 15 March 2021

Redirect to: