Read Serial Data Arduino

Active3 years, 5 months ago

Reading Serial data from an Arduino using C# In this example we will use analog pin 0 to obtain a value and send it via the com port (USB), we will read this with a C# app and display the value in a progress bar and label. Arduino and Visual C# - Read data from Arduino ChillyCoding. C# to Arduino Communication. Send data and commands from Computer to an Arduino. Receive Serial Data From Arduino - Duration. I am trying to receive data through the serial port from my SBC (a TS-7200), i am working with linux and handle serial communication using C/C.To send data to the SBC i'am using an arduino uno. So basically the arduino is printing ASCII text data through Pin1 (Hardware Serial) in a loop with a 3ms delay. So, now we have got the Serial Port on Arduino UNO which we know are at pin # 0 and pin # 1, now in the next part, we are gonna have a look at How to use Arduino Serial Read and get data from this Serial Port. How to use Arduino Serial Read? Arduino Serial read command is used for reading any data available at the Serial Port.

Serial data is slow by Arduino standards When anything sends serial data to the Arduino it arrives into the Arduino input buffer at a speed set by the baud rate. At 9600 baud about 960 characters arrive per second which means there is a gap of just over 1 millisecond between characters.

in the datasheet, it's stated that I should apply 32 serial clocks to get the 32 bits of data. How can I program this is Arduino?

Lorenz ArdienteLorenz Ardiente

3 Answers

You call SPI.transfer() four times, each time saving the returned value to a variable after proper bit shifting.

I assume the least significant byte is received first. Make sure the SPI mode is the right one, as indicated in your datasheet.

If the slave can't handle being deasserted between bytes (which is what SPI.transfer() will do at the end of the transaction), then you can either try st2000's hardware SPI approach or use bitbanged SPI with shiftIn().

TisteAndiiTisteAndii
2,2822 gold badges8 silver badges18 bronze badges

Sparkfun has a good explaination of what SPI is:
https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi

The Arduino IDE has a SPI library that comes with the IDE.
http://www.arduino.cc/en/Reference/SPI

The library comes with two examples:
Using SPI to read a Barometric Pressure Sensor
Controlling a Digital Potentiometer Using SPI

sa_leinadsa_leinad
2,3211 gold badge14 silver badges39 bronze badges

Objective is to read 32 bits using the (unknown) device's SPI port.

If the device will tolerate the SPI Chip Select line activity (going from inactive to active to inactive for each 8 bit byte read) you should be able to get the desired 32 bits of data by performing 4 consecutive 8 bit SPI reads.

However, if the device will not tolerate the above SPI Chip Select line activity (that is, if the device requires the SPI Chip Select to be active for the entire 32 bit SPI transaction), you can not separate the 32 bit transaction into 4 individual 8 bit SPI transactions with out controlling the SPI Chip Select line. The following code example illustrates how this is done:

The above example comes from the Arduino SPI library web page and is a 16 bit (not 32 bit) transfer. To code up a 32 bit transfer, continue to call the SPI.transfer method using the SPI_CONTINUE parameter.

added later..

It appears people are discouraging use of the DueExtendedSPI methods. And SPISettings and SPI.beginTransaction() are to be used? If so this page show example code where the code is explicitly controlling the SPI Chip Select line (look for slaveAPin & slaveBPin). Note how the code is reading 24 bits from slave A and writing 8 bits to slave B. To read 32 instead of 24 bits, we need to alter a segment of the code (along with a few other supporting changes) in this fashion:

st2000st2000
5,1062 gold badges6 silver badges16 bronze badges

Not the answer you're looking for? Browse other questions tagged spi or ask your own question.

Read Serial Port Data Arduino

After creating the dropControllerBT app and realizing how much easier controlling the dropController device is through the app I started to think about creating a PC app. I haven’t done any PC programming for many years and so I looked at what various options are currently available. Visual Basic kept being recommended for ease of use and quick development. Visual Basic comes as part of Microsoft’s Visual Studio Suite and I initially download and played with Visual Studio Express which in turn lead to Visual Studio Community. Both are free for personal use. Instagram for windows 10.

Visual Studio Express is a striped down version of the larger packages and has some major limitations. Visual Studio 2013 Community, on the other hand, is a full featured IDE and development system free to use for students, open source contributors and small development teams. It includes several languages but for now I am only interested in Visual Basic.

Visual Studio 2013 Community is available for download at https://www.visualstudio.com/en-us/products/visual-studio-community-vs.aspx. The download is just the installer which will download the main program from the internet. If, like me, you prefer an off line installer, you can get one at http://go.microsoft.com/?linkid=9863609

The main download page is at https://www.visualstudio.com/downloads/download-visual-studio-vs

After installing the software it took me a while and many Google searches before I started to figure out the IDE. For me, fully learning the IDE is beyond what I want and have time for but over the course of a weekend I managed to create my first working program. A simple example of receiving data from the Arduino.

Read Serial Data From Arduino Using Matlab

Arduino to Visual Basic 2013 Communication

The example uses a very simply form and shows what ever it recieves from the Arduino in a text box. Hulk ultimate destruction gamecube iso 2.

The Arduino Sketch

The Arduino Sketch sends the string “1234” over the serial connection once every second. At the same time it blinks the built in LED on pin 13.

To test that the sketch is working you can open the serial monitor:
You are likely to be using a different COM port.

The Visual Basic Program

Here is the form:

The form includes:
– a drop down list that contains the available COM ports
– a Connect / Dis-connect button.
– a label to show if the timer is active or not
– a text box where the received data is displayed.
– a CLEAR button that clears the contents of the text box.

And here is the program:

The program in Detail

I am using two global variables; comPORT and receivedData. comPORT is the COM port selected by the user and should be the one the Arduino is connected to. receivedData is the data received on the selected COM port


When the program is first run, the Form1_Load() subroutine populates the COM port combo box / drop down list with the available COM ports. The program then waits for the user to pick one.


When the user selects a COM port, the value is copied to the variable comPORT. This is not really necessary as the selected value can be read from the combo box but I like to keep this kind of data in easy to use variables.


Sub connect_BTN_Click() triggers when the user clicks on the Connect button. The first thing the routine does is determine if the user is connecting or dis-connecting. The same button is used for both.
If connecting, and comPORT is not empty, then the serial port properties are set, the serial port is opened and the timer is started. To show that the timer is active the timer label is updated to “Timer: ON”.
If comPORT is empty a message is displayed telling the user to select a COM port first.
If dis-connecting, the serial port is closed, the timer is stopped and the timer label is updated to “Timer: OFF”.

The Timer label is there purely for debugging.


A timer is used to check for incoming data. The timer is set to trigger every 500ms or half a second and when triggered it calls the Timer1_Tick() routine. For this example 500ms is fast enough. For more complex tasks the timing may need to be adjusted.


Timer1_Tick() calls a second subroutine that checks to see if there is any serial data and if there is it then copies the incoming data to the receivedData variable.
receivedData is then added to the textbox.


The final subroutine, Sub clear_BTN_Click(), simply resets the contents of the text box.


Trouble Shooting

If you are not receiving data in the VB program but the Arduinos serial monitor works then on the serial port within VB, set “DtrEnable = true” and “RtsEnable = true”. Thank you Banause for the tip. This seems to be required for the Arduino Leonardo.


This is a very simple example that I used to learn the basics on making a serial connection between the Arduino and a computer. As such the code can be made much better.
– The COM port is left open all the time and for more complex applications it may be better to open and close the port as required.
– The application simply displays what ever data is received. There is no error checking. To make it more reliable and to ensure you have all the data it would be better if the data was enclosed in start and end tags and then parsed.

For more information on serial communication with the Arduino and using start and end makers take a look at Robin2’s Serial Input Basics on the Arduino forum.


Download

Download the Visual Basic project files and the Arduino sketch

ArduinoVisualBasicSerial.zip 87.82 KB

Processing Read Serial Data From Arduino


You will need to have Visual Studio installed to use the VB project files.

Labview Read Serial Data Arduino


Next Steps

In part 2 we take this further. And in part 3 we start to control an Arduino