Git Product home page Git Product logo

Comments (6)

iqfareez avatar iqfareez commented on September 28, 2024 5

Is this is what you guys are trying to achieve?

The counter is from the Arduino.

2022-12-22.19-39-18.mp4

Super simple Arduino code:

int i = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(i);
  i++;
  delay(1000);
}

Flutter (Dart) code:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_libserialport/flutter_libserialport.dart';

class ScorePage extends StatefulWidget {
  const ScorePage({super.key});

  @override
  State<ScorePage> createState() => _ScorePageState();
}

class _ScorePageState extends State<ScorePage> {
  SerialPort port = SerialPort('COM9');
  late SerialPortReader reader;

  @override
  void initState() {
    super.initState();
    var res = port.openRead();

    if (!res) {
      print('Error opening port:${port.name}');
      // handle to close reconnet
    }
    var portConfig = SerialPortConfig()
      ..baudRate = 9600
      ..bits = 8
      ..stopBits = 1;
    port.config = portConfig;

    reader = SerialPortReader(port);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StreamBuilder<Uint8List>(
          stream: reader.stream,
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Text('Error: ${snapshot.error}');
            }
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Text('Awaiting result...');
            }

            var decodedString = String.fromCharCodes(snapshot.data!);
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'Counter from Arduino:',
                ),
                Text(
                  decodedString,
                  style: Theme.of(context).textTheme.headline1,
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    port.close();
    super.dispose();
  }
}

Sometimes you may need to disconnect and reconnect the USB cable.

from flutter_libserialport.

LiveRock avatar LiveRock commented on September 28, 2024

I am interested in the answer too. I am trying to use this for a Flutter desktop app with ESP32

from flutter_libserialport.

jiangweihu avatar jiangweihu commented on September 28, 2024

Have you solved this yet? I want use this for a Flutter desktop app with STM32.

from flutter_libserialport.

nightlan1015297 avatar nightlan1015297 commented on September 28, 2024

Having same issue.
But open the serial port in Arduino IDE first then open the port with this package everything works fine.

Did anyone solve this problem?

from flutter_libserialport.

lucafabbri avatar lucafabbri commented on September 28, 2024

Is this is what you guys are trying to achieve?

The counter is from the Arduino.

2022-12-22.19-39-18.mp4
Super simple Arduino code:

int i = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(i);
  i++;
  delay(1000);
}

Flutter (Dart) code:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_libserialport/flutter_libserialport.dart';

class ScorePage extends StatefulWidget {
  const ScorePage({super.key});

  @override
  State<ScorePage> createState() => _ScorePageState();
}

class _ScorePageState extends State<ScorePage> {
  SerialPort port = SerialPort('COM9');
  late SerialPortReader reader;

  @override
  void initState() {
    super.initState();
    var res = port.openRead();

    if (!res) {
      print('Error opening port:${port.name}');
      // handle to close reconnet
    }
    var portConfig = SerialPortConfig()
      ..baudRate = 9600
      ..bits = 8
      ..stopBits = 1;
    port.config = portConfig;

    reader = SerialPortReader(port);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StreamBuilder<Uint8List>(
          stream: reader.stream,
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Text('Error: ${snapshot.error}');
            }
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Text('Awaiting result...');
            }

            var decodedString = String.fromCharCodes(snapshot.data!);
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'Counter from Arduino:',
                ),
                Text(
                  decodedString,
                  style: Theme.of(context).textTheme.headline1,
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    port.close();
    super.dispose();
  }
}

Sometimes you may need to disconnect and reconnect the USB cable.

This closes the inquiry, thanks @iqfareez

from flutter_libserialport.

Zacchaeus-Oluwole avatar Zacchaeus-Oluwole commented on September 28, 2024

Is this is what you guys are trying to achieve?

The counter is from the Arduino.

2022-12-22.19-39-18.mp4
Super simple Arduino code:

int i = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(i);
  i++;
  delay(1000);
}

Flutter (Dart) code:

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_libserialport/flutter_libserialport.dart';

class ScorePage extends StatefulWidget {
  const ScorePage({super.key});

  @override
  State<ScorePage> createState() => _ScorePageState();
}

class _ScorePageState extends State<ScorePage> {
  SerialPort port = SerialPort('COM9');
  late SerialPortReader reader;

  @override
  void initState() {
    super.initState();
    var res = port.openRead();

    if (!res) {
      print('Error opening port:${port.name}');
      // handle to close reconnet
    }
    var portConfig = SerialPortConfig()
      ..baudRate = 9600
      ..bits = 8
      ..stopBits = 1;
    port.config = portConfig;

    reader = SerialPortReader(port);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: StreamBuilder<Uint8List>(
          stream: reader.stream,
          builder: (context, snapshot) {
            if (snapshot.hasError) {
              return Text('Error: ${snapshot.error}');
            }
            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Text('Awaiting result...');
            }

            var decodedString = String.fromCharCodes(snapshot.data!);
            return Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  'Counter from Arduino:',
                ),
                Text(
                  decodedString,
                  style: Theme.of(context).textTheme.headline1,
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  @override
  void dispose() {
    port.close();
    super.dispose();
  }
}

Sometimes you may need to disconnect and reconnect the USB cable.

No, i need to perform write and read. read is working but write isn't working

from flutter_libserialport.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.