CodeDocu.com

 

 

 

In this example, the Calliope learning computer sends data to a connected PC via a serial port.

The PC receives the data and displays the data in a console program.

 

Calliope : Sending text via serial interface to a pc

 

The code serial.writeLine("a text to send") is sent to the computer's COM interface via the built-in interface.

    serial. writeLine(Messwert + "")   //\r \n

 

 

Calliope Beispiel Code

Sending text over the serial interface

The code is in javascript

The code in the Caliope for sending data to the serial interface is described here

basic. forever(() => {

    //-----< MainLoop >------

 

    Reading temperature

    let measured value =  input. compassHeading()

 

    serial. writeLine(Messwert + "")   //\r \n

 

    wait 1 second

    basic. pause(1000)

 

    //-----</ MainLoop >------

})

 

 

 

 

Calliope is programmed here and loaded into the hardware

https://makecode.calliope.cc/

Calliope : Sending text via serial interface to a pc

 

 

Receiving via the COM interface of the PC

 

Console Programm

 

The serial interface is first set

        < Global Variables >

        static SerialPort serial_Caliope;

 

            --< create serial >--

            Defining serial interface

            serial_Caliope = new SerialPort

            {

                PortName = "COM7",

                BaudRate = 115200,

                Parity = Parity.None,

                DataBits = 8,

                Handshake = Handshake.None,

                RtsEnable = true,

                StopBits = StopBits.One

            };

 

Then the COM interface is connected or opened for querying

 

            Start serial interface

            serial_Caliope.Open();

            --</ create serial >--

 

Values are then read via the command

string comErgebnis = serial_Caliope.ReadLine();

 

And then displayed in the console

Console.WriteLine(comErgebnis);

Calliope : Sending text via serial interface to a pc

 

A small console program reads the data cyclically at the serial port

using System;

using System.Collections.Generic;

using System.IO.Ports;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Timers;

 

namespace _02_Console_Net

{

    class Program

    {

        < Global Variables >

        static SerialPort serial_Caliope;

 

        static void Main(string[] args)

        {

            //--------------< Main >---------------

            --< create serial >--

            Defining serial interface

            serial_Caliope = new SerialPort

            {

                PortName = "COM7",

                BaudRate = 115200,

                Parity = Parity.None,

                DataBits = 8,

                Handshake = Handshake.None,

                RtsEnable = true,

                StopBits = StopBits.One

            };

 

            Start serial interface

            serial_Caliope.Open();

            --</ create serial >--

 

 

            //-< Timer >-

            Timer timerA = new Timer(1000);

            timerA.Elapsed += Timer_Elapsed;

            timerA.Start();

            //-</ Timer >-

 

 

            Spend

            Console.ReadKey();

            //

 

 

            //--------------</ Main >---------------

        }

 

 

        #region  Events

        //----< Events >-------

        private static void Timer_Elapsed(object sender, ElapsedEventArgs e)

        {

            string comErgebnis = serial_Caliope.ReadLine();

            Console.WriteLine(comErgebnis);

        }

        //----</ Events >-------

        #endregion  /Events

 

    }

}

 

 


Software Entwicklung Stuttgart NĂ¼rtingen
Suche Projekte C#, WPF, Windows App,ASP.Net, vb.Net, WinForms, SQL Server, Access, Excel