<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="pl-pl">
<link rel="self" type="application/atom+xml" href="https://forum.atnel.pl/feed.php?f=48&amp;t=7212&amp;mode" />

<title>ATNEL tech-forum</title>
<link href="https://forum.atnel.pl/index.php" />
<updated>2014-05-27T19:41:40+01:00</updated>

<author><name><![CDATA[ATNEL tech-forum]]></name></author>
<id>https://forum.atnel.pl/feed.php?f=48&amp;t=7212&amp;mode</id>
<entry>
<author><name><![CDATA[vaffanculo]]></name></author>
<updated>2014-05-27T19:41:40+01:00</updated>
<published>2014-05-27T19:41:40+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82562#p82562</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82562#p82562"/>
<title type="html"><![CDATA[Re: C# (WPF) Problem z odbieraniem danych (COM) w nowym wątk]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82562#p82562"><![CDATA[
Spróbuj tak:<br /><br />[syntax=csharp]ChatContent.Invoke(new Action(delegate()<br />{<br /><br />       MainWindow.myMainWindow.ChatContent.AppendText(message); <br /><br />}));[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=1784">vaffanculo</a> — 27 maja 2014, o 19:41</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[jogproof]]></name></author>
<updated>2014-05-27T17:24:24+01:00</updated>
<published>2014-05-27T17:24:24+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82550#p82550</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82550#p82550"/>
<title type="html"><![CDATA[Re: C# (WPF) Problem z odbieraniem danych (COM) w nowym wątk]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82550#p82550"><![CDATA[
Spróbowałem to zrobić tak jak opisane to jest w tym poradniku, ale niestety podczas kompilacji wyrzuca wyjątek: <br /><br />&quot;An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in PresentationFramework.dll<br />Additional information: Obiekt docelowy wywołania zgłosił wyjątek.&quot;<br /><br />W tym poradniku aplikacja jest w Windows Forms, ja natomiast używam WPF, no i okazuje się, że u mnie to nie działa<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=4536">jogproof</a> — 27 maja 2014, o 17:24</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[Sonix]]></name></author>
<updated>2014-05-27T16:21:52+01:00</updated>
<published>2014-05-27T16:21:52+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82547#p82547</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82547#p82547"/>
<title type="html"><![CDATA[Re: C# (WPF) Problem z odbieraniem danych (COM) w nowym wątk]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82547#p82547"><![CDATA[
Nie znam się za bardzo na C# ale kiedyś popełniłem małą aplikację z obsługą RS232 która wyświetlała przychodzące znaki (coś jak terminal).<br />Korzystałem z tego kursu, może coś Ci podpowie:<br /><br /><!-- m --><a class="postlink" href="http://www.forbot.pl/forum/topics20/programowanie-port-szeregowy-i-interfejs-usart-czyli-komunikacja-mikrokontrolera-z-komputerem-vt6118.htm" >http://www.forbot.pl/forum/topics20/pro ... vt6118.htm</a><!-- m --><p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=127">Sonix</a> — 27 maja 2014, o 16:21</p><hr />
]]></content>
</entry>
<entry>
<author><name><![CDATA[jogproof]]></name></author>
<updated>2014-05-27T15:51:01+01:00</updated>
<published>2014-05-27T15:51:01+01:00</published>
<id>https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82545#p82545</id>
<link href="https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82545#p82545"/>
<title type="html"><![CDATA[C# (WPF) Problem z odbieraniem danych (COM) w nowym wątku]]></title>

<content type="html" xml:base="https://forum.atnel.pl/viewtopic.php?t=7212&amp;p=82545#p82545"><![CDATA[
Witam serdecznie,<br />Podczas pisania aplikacji w C# mającej za zadanie wysyłać/odbierać dane (tekst, pliki) poprzez port COM natknąłem się na pewną przeszkodę, mianowicie nie wiem jak rozwiązać kwestię nasłuchiwania na porcie i automatycznego wypisania odebranych danych na elemencie WPF (TextView). Chodzi tu o stworzenie swego rodzaju chatu przy użyciu transmisji szeregowej. Wydaje mi się, że problemem jest fakt, iż nie można odwołać się do wątku głównego (z UI) z poziomu innego wątku. Znalazłem kilka rozwiązań, m.in. wykorzystanie BackgroundWorker'a czy też Dispatcher.BeginInvoke, aczkolwiek nie potrafię poradzić sobie z ich poprawnym użyciem. Poniżej wklejam kod programu. Bardzo proszę o jakieś wskazówki.<br /><br />MainWindow.xaml.cs<br />[syntax=csharp]using System;<br />    using System.Collections.Generic;<br />    using System.Linq;<br />    using System.Text;<br />    using System.Threading.Tasks;<br />    using System.Windows;<br />    using System.Windows.Controls;<br />    using System.Windows.Data;<br />    using System.Windows.Documents;<br />    using System.Windows.Input;<br />    using System.Windows.Media;<br />    using System.Windows.Media.Imaging;<br />    using System.Windows.Navigation;<br />    using System.Windows.Shapes;<br />    using System.IO.Ports;<br />    using System.Threading;<br />    using System.IO;<br /><br />    namespace Telekomunikacja___XModem<br />    {<br />        /// &lt;summary&gt;<br />        /// Interaction logic for MainWindow.xaml<br />        /// &lt;/summary&gt;<br />        public partial class MainWindow : Window<br />        {<br />            static Port port;<br />            static bool _continue = false;<br />            static Thread readThread;<br />            public static MainWindow myMainWindow;<br />            public MainWindow()<br />            {<br />                myMainWindow = this;<br />                readThread = new Thread(ReceiveMessages);<br />                List&lt;int&gt; predkosci = new List&lt;int&gt; <br />                {<br />                    75, 110, 134, 150, 300, 600, 1200, 1800, 2400, 4800, 7200,<br />                    9600, 14400, 19200, 38400, 57600, 115200, 128000<br />                };<br />                List&lt;int&gt; bityDanych = new List&lt;int&gt;<br />                {<br />                    4, 5, 6, 7, 8<br />                };<br />                InitializeComponent();<br />                foreach (int i in predkosci)<br />                {<br />                    baudRateCB.Items.Add(i);<br />                }<br />                foreach (string s in SerialPort.GetPortNames())<br />                {<br />                    portNameCB.Items.Add(s);<br />                }<br />                foreach (int i in bityDanych)<br />                {<br />                    dataBitsCB.Items.Add(i);<br />                }<br />                foreach (Parity p in Enum.GetValues(typeof(Parity)))<br />                {<br />                    parityCB.Items.Add(p);<br />                }<br />                foreach (StopBits s in Enum.GetValues(typeof(StopBits)))<br />                {<br />                    stopBitsCB.Items.Add(s);<br />                }<br />                foreach (Handshake h in Enum.GetValues(typeof(Handshake)))<br />                {<br />                    handshakeCB.Items.Add(h);<br />                }<br />                portNameCB.SelectedIndex = 0;<br />                baudRateCB.SelectedIndex = 15;<br />                dataBitsCB.SelectedIndex = 4;<br />                parityCB.SelectedIndex = 0;<br />                stopBitsCB.SelectedIndex = 1;<br />                handshakeCB.SelectedIndex = 0;<br />                sendBT.IsEnabled = false;<br />                wyslijPlikBT.IsEnabled = false;<br />                odbierzPlikBT.IsEnabled = false;<br />            }<br />            public void ReceiveMessages()<br />            {<br />                while (_continue)<br />                {<br />                    try<br />                    {<br />                        string message = port.SerialPort.ReadLine();<br />                        MainWindow.myMainWindow.Dispatcher.BeginInvoke(new Action(delegate() { MainWindow.myMainWindow.ChatContent.AppendText(message); }));<br />                    }<br />                    catch (TimeoutException) { }<br />                }<br />            }<br />            private void otwieraniePortu(object sender, RoutedEventArgs e)<br />            {<br />                if (_continue == false)<br />                {<br />                    port = new Port();<br />                    port.Init((string)portNameCB.SelectedValue, (int)baudRateCB.SelectedValue, (Parity)parityCB.SelectedValue, (int)dataBitsCB.SelectedValue, (StopBits)stopBitsCB.SelectedValue, (Handshake)handshakeCB.SelectedValue);<br />                    port.SerialPort.Open();<br />                    initPortBT.Content = &quot;Zamknij port&quot;;<br />                    _continue = true;<br />                    sendBT.IsEnabled = true;<br />                    wyslijPlikBT.IsEnabled = true;<br />                    odbierzPlikBT.IsEnabled = true;<br />                    portNameCB.IsEnabled = false;<br />                    baudRateCB.IsEnabled = false;<br />                    dataBitsCB.IsEnabled = false;<br />                    parityCB.IsEnabled = false;<br />                    stopBitsCB.IsEnabled = false;<br />                    handshakeCB.IsEnabled = false;<br />                    readThread.Start();<br />                }<br />                else<br />                {<br />                    initPortBT.Content = &quot;Otwórz port&quot;;<br />                    _continue = false;<br />                    sendBT.IsEnabled = false;<br />                    wyslijPlikBT.IsEnabled = false;<br />                    odbierzPlikBT.IsEnabled = false;<br />                    portNameCB.IsEnabled = true;<br />                    baudRateCB.IsEnabled = true;<br />                    dataBitsCB.IsEnabled = true;<br />                    parityCB.IsEnabled = true;<br />                    stopBitsCB.IsEnabled = true;<br />                    handshakeCB.IsEnabled = true;<br />                    readThread.Join();<br />                    port.SerialPort.Close();<br />                }<br />            }<br /><br />            private void wyslanieWiadomosci(object sender, RoutedEventArgs e)<br />            {<br />                string tekst = uzytkownik.Text.ToString() + &quot;: &quot; + wiadomosc.Text.ToString();<br />                port.SerialPort.WriteLine(tekst);<br />                ChatContent.AppendText(tekst);<br />                wiadomosc.Clear();<br />            }<br /><br />            private void wybieraniePliku(object sender, RoutedEventArgs e)<br />            {<br />                var okno = new Microsoft.Win32.OpenFileDialog();<br />                var pokaz = okno.ShowDialog();<br />                if (pokaz == false) return;<br />                sciezkaPliku.Text = okno.FileName;<br />            }<br /><br />            private void wysylaniePliku(object sender, RoutedEventArgs e)<br />            {<br />                string path = sciezkaPliku.Text.ToString();<br />                using (StreamReader sr = File.OpenText(path))<br />                {<br />                    string s = &quot;&quot;;<br />                    while ((s = sr.ReadLine()) != null)<br />                    {<br />                        port.SerialPort.WriteLine(s);<br />                    }<br />                }<br />            }<br /><br />            private void odbieraniePliku(object sender, RoutedEventArgs e)<br />            {<br /><br />            }<br />        }<br />    }[/syntax]<br /><br />Port.cs<br />[syntax=csharp]using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />using System.Threading.Tasks;<br />using System.IO.Ports;<br />using System.Windows.Controls;<br /><br />namespace Telekomunikacja___XModem<br />{<br />    class Port<br />    {<br />        private SerialPort _serialPort;<br />        public Port()<br />        {<br />            _serialPort = new SerialPort();<br />        }<br />        public void Init(string name, int rate, Parity par, int data, StopBits stop, Handshake hand)<br />        {<br />            _serialPort.PortName = name;<br />            _serialPort.BaudRate = rate;<br />            _serialPort.Parity = par;<br />            _serialPort.DataBits = data;<br />            _serialPort.StopBits = stop;<br />            _serialPort.Handshake = hand;<br />            _serialPort.ReadTimeout = 500;<br />            _serialPort.WriteTimeout = 500;<br />        }<br />        public SerialPort SerialPort<br />        {<br />            get { return _serialPort; }<br />        }<br />    }<br />}[/syntax]<p>Statystyki: Napisane przez <a href="https://forum.atnel.pl/memberlist.php?mode=viewprofile&amp;u=4536">jogproof</a> — 27 maja 2014, o 15:51</p><hr />
]]></content>
</entry>
</feed>