• 大小: 658KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-01
  • 语言: 其他
  • 标签: Socket  C#  打标机  

资源简介

此demo为激光打标机的客户端代码,发送指令到服务端,使用到tcp通信中的socket来接收发送数据

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Collections;

namespace TcpConnectDemo
{
    public partial class Form1 : Form
    {
        enum WorkState
        {            
            START          
            PUT
            GPRT
            WAIT
        };

        private string gb_strFileName;
        private SocketClient gb_sockClient;

        private string gb_strIp = “192.168.1.100“;
        // 服务端端口
        private Int16 gb_nPort = 3875;
        
        //工作线程
        private Thread gb_wrokThread;

        //工作流程
        private static WorkState gb_State = WorkState.START;

        //线程停止标志位
        private static bool gb_bIsStop = true;

        //线程发送消息标志位,每个流程只发送一次消息,发送后循环接收消息
        private static bool gb_bIsSend = false;

        private static bool gb_bIsGprt = false;

        //存储标刻序列号,
        //文本格式:
        //AABBCC
        //DDEEFF
        //...
        //一行数据:需要PUT的数据
        private static ArrayList gb_arrayMarkData = new ArrayList();


        //线程锁,防止线程改变标志位时冲突
        public static object gb_lockStop = new object();
        public static object gb_lockRecv = new object();
        public static object gb_lockSend = new object();
        public static object gb_lockGprt = new object();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {      
            //初始化Sock类,设置IP、端口号
            gb_sockClient = new SocketClient(gb_strIp gb_nPort);
            //gb_sockClient.SetTexts += new SocketClient.SetText(SetText);

            //设置按钮使能
            btnStart.Enabled = false;
            btnGprt.Enabled = false;
        }

        //打开文本数据,存储到gb_arrayMarkData
        private void 打开文件ToolStripMenuItem_Click(object sender EventArgs e)
        {
            OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.title = “打开“;

            if(openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string strFileName = openFileDialog.FileName;
                if (!File.Exists(strFileName))
                {
                    SetText(“文件不存在!“false);
                    return;
                }
                gb_strFileName = strFileName;
                
                ReadTxtData();
                if (gb_arrayMarkData.Count > 0)
                {
                    SetText(“文件已打开!“ false);
                }
                else
                {
                    SetText(“文件数据为空!“ false);
                }
            }
            else
            {
                //SetText(“已取消打开文件!“ false);
                return;
            }
        }

   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-10-26 10:48  TcpConnectDemo\
     目录           0  2019-10-26 10:48  TcpConnectDemo\.vs\
     目录           0  2019-10-26 10:48  TcpConnectDemo\.vs\TcpConnectDemo\
     目录           0  2019-10-26 10:48  TcpConnectDemo\.vs\TcpConnectDemo\v15\
     文件       49664  2019-11-01 14:56  TcpConnectDemo\.vs\TcpConnectDemo\v15\.suo
     目录           0  2019-10-26 10:48  TcpConnectDemo\.vs\TcpConnectDemo\v15\Server\
     目录           0  2019-10-26 10:48  TcpConnectDemo\.vs\TcpConnectDemo\v15\Server\sqlite3\
     文件           0  2019-10-24 15:01  TcpConnectDemo\.vs\TcpConnectDemo\v15\Server\sqlite3\db.lock
     文件      573440  2019-10-26 09:38  TcpConnectDemo\.vs\TcpConnectDemo\v15\Server\sqlite3\storage.ide
     文件       32768  2019-11-01 08:22  TcpConnectDemo\.vs\TcpConnectDemo\v15\Server\sqlite3\storage.ide-shm
     文件     4140632  2019-11-01 08:22  TcpConnectDemo\.vs\TcpConnectDemo\v15\Server\sqlite3\storage.ide-wal
     目录           0  2019-10-26 10:48  TcpConnectDemo\TcpConnectDemo\
     文件        7412  2019-10-25 15:21  TcpConnectDemo\TcpConnectDemo\Form1.Designer.cs
     文件       33471  2019-10-26 09:47  TcpConnectDemo\TcpConnectDemo\Form1.cs
     文件        6015  2019-10-25 15:21  TcpConnectDemo\TcpConnectDemo\Form1.resx
     文件         495  2019-10-24 15:01  TcpConnectDemo\TcpConnectDemo\Program.cs
     目录           0  2019-10-26 10:48  TcpConnectDemo\TcpConnectDemo\Properties\
     文件        1344  2019-10-24 15:01  TcpConnectDemo\TcpConnectDemo\Properties\AssemblyInfo.cs
     文件        2841  2019-10-24 15:01  TcpConnectDemo\TcpConnectDemo\Properties\Resources.Designer.cs
     文件        5612  2019-10-24 15:01  TcpConnectDemo\TcpConnectDemo\Properties\Resources.resx
     文件        1101  2019-10-24 15:01  TcpConnectDemo\TcpConnectDemo\Properties\Settings.Designer.cs
     文件         249  2019-10-24 15:01  TcpConnectDemo\TcpConnectDemo\Properties\Settings.settings
     文件        4090  2019-10-26 09:17  TcpConnectDemo\TcpConnectDemo\SocketClient.cs
     文件        3475  2019-10-24 16:54  TcpConnectDemo\TcpConnectDemo\TcpConnectDemo.csproj
     目录           0  2019-10-26 10:48  TcpConnectDemo\TcpConnectDemo\bin\
     目录           0  2019-10-26 10:48  TcpConnectDemo\TcpConnectDemo\bin\Debug\
     文件       22016  2019-10-26 09:47  TcpConnectDemo\TcpConnectDemo\bin\Debug\TcpConnectDemo.exe
     文件       54784  2019-10-26 09:47  TcpConnectDemo\TcpConnectDemo\bin\Debug\TcpConnectDemo.pdb
     目录           0  2019-10-26 10:48  TcpConnectDemo\TcpConnectDemo\obj\
     目录           0  2019-10-26 10:48  TcpConnectDemo\TcpConnectDemo\obj\Debug\
     文件        1431  2019-10-25 09:20  TcpConnectDemo\TcpConnectDemo\obj\Debug\DesignTimeResolveAssemblyReferences.cache
............此处省略11个文件信息

评论

共有 条评论