端口串行TCPIP [英] Port Serial over TCPIP

查看:74
本文介绍了端口串行TCPIP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发一个通信程序,使用TCPIP从计算机的多个串行端口发送所有数据。为此,我们将使用PortSerial类,BackgroundWorket和Socket。



我已经通过多个串行端口接收数据。我以前做了一个FIFO队列,然后提取它们,然后提取不同串口接收到的所有数据。



想法是将通过TCPIP收到的每个语句发送到另一台电脑。所以我必须实现服务器和客户端部分。服务器端在捕获所有数据的同一程序中实现。



套接字服务器将所有数据发送给客户,客户将发送一个名称以了解谁正在连接。



我发现的第一个poblema是,当我试图在一个线程中运行服务器时,我卡住了整个程序。



Hello, I am developing a communications program to send all data from multiple serial ports of a computer using TCPIP. To do this we will use PortSerial classes, BackgroundWorket and Socket.

I have already made the reception of data by multiple serial ports. I used to do a FIFO queue in which they mentiendo all data received by the different serial ports are then extracted.

The idea is to send each statement received through TCPIP to another computer. So I have to implement a server and a client part. The server side is implemented in the same program that capture all the data.

The socket server will send all data to customers, and customers will send a name to know who is connecting.

The first poblema I''ve found is that when trying to run the server in a thread I stuck the whole program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;

namespace RIT_Communicator
{
    public class RIT_SocketServer
    {
        private const int sampleTcpPort = 13000;
        public Thread sampleTcpThread;

        public RIT_SocketServer()
        {
            try
            {
                //Starting the TCP Listener thread.
                sampleTcpThread = new Thread(new ThreadStart(StartListen2));
                sampleTcpThread.Start();
                Console.WriteLine("Started SampleTcpUdpServer's TCP Listener Thread!\n");
            }
            catch (Exception e)
            {
                Console.WriteLine("An TCP Exception has occurred!" + e.ToString());
                sampleTcpThread.Abort();
            }        
        }

        private void StartListen2()
        {
            //Create an instance of TcpListener to listen for TCP connection.
            TcpListener tcpListener = new TcpListener(sampleTcpPort);
            try
            {
                while (true)
                {
                    tcpListener.Start();
                    //Program blocks on Accept() until a client connects.
                    Socket soTcp = tcpListener.AcceptSocket();
                    Console.WriteLine("SampleClient is connected through TCP.");
                    Byte[] received = new Byte[512];
                    int bytesReceived = soTcp.Receive(received, received.Length, 0);
                    String dataReceived = System.Text.Encoding.ASCII.GetString(received);
                    Console.WriteLine(dataReceived);
                    String returningString = "The Server got your message through TCP: " +
                    dataReceived;
                    Byte[] returningByte = System.Text.Encoding.ASCII.GetBytes
                    (returningString.ToCharArray());
                    //Returning a confirmation string back to the client.
                    soTcp.Send(returningByte, returningByte.Length, 0);
                    tcpListener.Stop();
                }
            }
            catch (SocketException se)
            {
                Console.WriteLine("A Socket Exception has occurred!" + se.ToString());
            }
        }

    }
}





这是第一次使用套接字,并在网上看几个例子,这是我发现的最接近我需要的东西。



想法是推出一个服务器套接字线程,因为在其他线程中捕获串行数据并且还必须在主屏幕上显示一组数据。



It is the first time working with Sockets, and watching several examples online, this is the closest thing I''ve found to what I would need.

The idea is to launch a server socket thread, since in other threads are doing catching the serial data and also have to display a set of data on the main screen.

推荐答案

参见异步服务器套接字示例 [ ^ ]和异步客户端套接字示例 [ ^ ]


嗨谢谢你的回复,我已经调整了异步programmi的代码ng。



我创建了一个backgrounworkers列表,每个都与一个串口相关联。我创建了一个函数来打开所有并关闭所有特别是一个特别是我想打开或关闭一个特别是因为没有在正在运行的进程中找到。你能用一个值或一个值进行一个过程来传递BackgroundWorker并且全都要求吗?



Hi thanks for the reply, I have adapted the code for asynchronous programming.

I created a list of backgrounworkers each associated with a serial port. I created a function to open all and close all but one in particular I want to open or close one in particular as not locate within the processes that are running. Can you make a process with a value, or a value to pass BackgroundWorker and go all ask for?

        private List<backgroundworker> _workers = new List<backgroundworker>();

        ......

        public void OpenAll()
        {

            string[] portNames = SerialPort.GetPortNames();

            foreach (string name in portNames)
            {
                BackgroundWorker bw = new BackgroundWorker();
                bw.WorkerSupportsCancellation = true;
                bw.WorkerReportsProgress = true;
                bw.DoWork += new DoWorkEventHandler(bw_DoWork);
                bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
                // Add the worker to the list of workers
                _workers.Add(bw);
                bw.RunWorkerAsync(name);
            }           
        }

</backgroundworker></backgroundworker>





我遇到的另一个问题是取消所有进程让我陷入困境(while(bw.IsBusy){}:





Another problem I have is that by canceling all processes stuck me here (while (bw.IsBusy){}:

public void CancelAllWorkers()
{
    foreach (BackgroundWorker bw in _workers)
    {
        // If the worker is busy - cancel it
        if (bw.IsBusy)
        {
            bw.CancelAsync();

            while (bw.IsBusy) {  }

            bw.DoWork -= new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted -= new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            bw.Dispose();

            _workers.Remove(bw);
        }
    }
}


这篇关于端口串行TCPIP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆