예제 #1
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //
                //serialPort1.WriteLine(txtSend.Text);

                //byte[] rtn=System.Text.Encoding.Default.GetBytes(txtSend.Text);
                //serialPort1.Write(rtn, 0, rtn.Length);
                byte[] errData = new byte[] { 0x23, 0x7f, 0x08, 0xab, 0x7f, 0x7e, 0x00, 0x7f, 0xff, 0x7f };
                //serialPort1.Write(errData, 0, errData.Length);

                //FileTransferProtocol x = new FileTransferProtocol();
                byte[] b = FileTransferProtocol.GetFrameFileInfo(0xFF, @"t.txt");

                serialPort1.Write(b, 0, b.Length);
                byte[] c = FileTransferProtocol.GetFrameFileData(255 + 1 + 2 + 4, System.Text.Encoding.Default.GetBytes("你好,CE文件!"));

                serialPort1.Write(c, 0, c.Length);

                byte[] d = FileTransferProtocol.GetFrameFileSendFinish();

                serialPort1.Write(d, 0, d.Length);
            }
            catch (Exception er)
            {
                txtData.Text += string.Format("[异常]:{0}\r\n", er.Message);
            }
        }
예제 #2
0
파일: Form1.cs 프로젝트: yanzisoft/COMFTP
        /// <summary>
        /// 使用网络向外部发文件数据
        /// </summary>
        /// <param name="stream">网络数据流对象</param>
        /// <param name="rootdir">上传起始目录</param>
        /// <param name="filenamelist">文件列表(含相对路径)</param>
        /// <param name="MN">数采仪MN</param>
        /// <param name="limited">最大允许传输率</param>
        private void SendFileToNet(NetworkStream stream, string rootdir, List <string> filenamelist, string MN, int limited)
        {
            FileStream fs = null;

            try
            {
                foreach (string filename in filenamelist)
                {
                    AddItemToList(listBoxLog, string.Format("{0} 准备传送{1}文件给数采仪:{2}", DateTime.Now.ToString(), rootdir + filename, MN));
                    fs = File.Open(rootdir + filename.ToString(), FileMode.Open, FileAccess.Read, FileShare.Read);

                    AddItemToList(listBoxLog, string.Format("数采仪:{1} 文件长度:{0}", fs.Length, MN));
                    byte[] fi = FileTransferProtocol.GetFrameFileInfo((int)fs.Length, filename.ToString());//System.IO.Path.GetFileName()
                    //AddItemToList(listBoxLog,string.Format("帧数据{0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2} {0:X2}", fi[0], fi[1], fi[2], fi[3], fi[4], fi[5], fi[6], fi[7], fi[8], fi[9], fi[10], fi[11], fi[12], fi[13], fi[14], fi[15]));
                    stream.Write(fi, 0, fi.Length);

                    int bufferSize = FileTransferProtocol.MaxFrameLength - FileTransferProtocol.FrameStructLength;
                    if (limited > 0)
                    {
                        bufferSize = limited / 10;//除以10表示由bit/s转为Byte/s
                    }
                    Byte[] bytes = new Byte[bufferSize];
                    //byte[] bytes = new byte[] { 0x30, 0x31, 0x32, 0x39 ,0x00 };


                    int    fullTime = (int)(fs.Length / (long)bufferSize);
                    int    mod      = (int)(fs.Length % (long)bufferSize);
                    Byte[] modBytes = new Byte[mod];
                    AddItemToList(listBoxLog, string.Format("数采仪:{3} 分块:{0}*{1},尾块:{2}", bufferSize, fullTime, mod, MN));

                    for (int i = 0; i < fullTime; i++)
                    {
                        int rBytes = fs.Read(bytes, 0, bufferSize);
                        AddItemToList(listBoxLog, string.Format("正在传送第{0}块给数采仪:{1}", i + 1, MN));
                        //byte[] data = FileTransferProtocol.GetFrameFileData(i * bufferSize, bytes);
                        byte[] data = FileTransferProtocol.GetFrameFileData(i + 1, bytes);
                        stream.Write(data, 0, data.Length);
                    }
                    if (mod != 0)
                    {
                        int rBytes = fs.Read(modBytes, 0, mod);
                        AddItemToList(listBoxLog, string.Format("正在传送尾块给数采仪:{0}", MN));
                        //byte[] data = FileTransferProtocol.GetFrameFileData(fullTime * bufferSize, modBytes);
                        byte[] data = FileTransferProtocol.GetFrameFileData(-1, modBytes);
                        stream.Write(data, 0, data.Length);
                    }

                    byte[] ff = FileTransferProtocol.GetFrameFileSendFinish();

                    stream.Write(ff, 0, ff.Length);
                    fs.Close();
                    fs = null;
                    AddItemToList(listBoxLog, string.Format("{0} {1} 给数采仪:{2}文件传送完毕。", DateTime.Now.ToString(), filename, MN));
                }
            }
            catch (Exception er)
            {
                AddItemToList(listBoxLog, string.Format("数采仪:{0} {1} {2}", MN, er.Source, er.Message));
            }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
        }
예제 #3
0
        private void btnSendFile_Click(object sender, EventArgs e)
        {
            //byte[] bytes = System.Text.Encoding.Default.GetBytes(txtSend.Text+"\r");


            try
            {
                if (!serialPort1.IsOpen)
                {
                    serialPort1.PortName = cmPorts.SelectedItem.ToString();
                    serialPort1.Open();
                }
                new Thread(new ThreadStart(delegate()
                {
                    try
                    {
                        string filepath = @"d:\EnumCOMS.exe";

                        FileStream fs = File.Open(filepath, FileMode.Open);

                        //FileTransferProtocol ftp = new FileTransferProtocol();


                        byte[] fi = FileTransferProtocol.GetFrameFileInfo((int)fs.Length, System.IO.Path.GetFileName(filepath));
                        serialPort1.DiscardOutBuffer();
                        serialPort1.Write(fi, 0, fi.Length);

                        int bufferSize = serialPort1.WriteBufferSize / 2;
                        Byte[] bytes   = new Byte[bufferSize];
                        //byte[] bytes = new byte[] { 0x30, 0x31, 0x32, 0x39 ,0x00 };


                        int fullTime    = (int)(fs.Length / (long)bufferSize);
                        int mod         = (int)(fs.Length % (long)bufferSize);
                        Byte[] modBytes = new Byte[mod];


                        for (int i = 0; i < fullTime; i++)
                        {
                            fs.Read(bytes, 0, bufferSize);
                            byte[] data = FileTransferProtocol.GetFrameFileData(i * bufferSize, bytes);
                            serialPort1.Write(data, 0, data.Length);
                        }
                        if (mod != 0)
                        {
                            fs.Read(modBytes, 0, mod);
                            byte[] data = FileTransferProtocol.GetFrameFileData(fullTime * bufferSize, modBytes);
                            serialPort1.Write(data, 0, data.Length);
                        }


                        fs.Close();

                        byte[] ff = FileTransferProtocol.GetFrameFileSendFinish();
                        serialPort1.Write(ff, 0, ff.Length);
                    }
                    catch (Exception er)
                    {
                        Trace.TraceError(er.Message);
                    }

                    MessageBox.Show("OK");
                })).Start();
            }
            catch (Exception er)
            {
                this.txtData.Text += er.Message;
            }
        }
예제 #4
0
        private void btnTestCOMTCP_Click(object sender, EventArgs e)
        {
            CRC.CRC8 crc = new CRC.CRC8();

            int dataStartIndex = FileTransferProtocol.CommandDataIndex;
            //测试:准备发送新的文件
            string filename = @"c:\abc.txt";

            byte[] FileInfo = FileTransferProtocol.GetFrameFileInfo(255 + 256, filename);

            //帧头

            Trace.Assert(FileInfo[0] == FileTransferProtocol.FrameHead);
            //crc
            //Trace.Assert(FileInfo[1] == crc);
            //命令字
            Trace.Assert(FileInfo[1] == 0x00);
            //文件长度
            Trace.Assert(FileInfo[2] == 0xFF);
            Trace.Assert(FileInfo[3] == 0x01);
            Trace.Assert(FileInfo[4] == 0);
            Trace.Assert(FileInfo[5] == 0);

            byte[] bs = System.Text.Encoding.Default.GetBytes(filename);
            Trace.Assert(FileInfo[6] == bs[0]);
            Trace.Assert(FileInfo[7] == bs[1]);

            //帧偏移
            Trace.Assert((FileInfo[FileInfo.Length - 4] + FileInfo[FileInfo.Length - 3] * 256) == (FileInfo.Length - 1));

            Trace.Assert(FileInfo[FileInfo.Length - 2] == 0x7F);
            Trace.Assert(FileInfo[FileInfo.Length - 1] == 0xFF);

            byte[] data = new byte[] { 0x01, 0x02, 0xFF, 0xEF, 0xCC, 0xDD, 0x00, 0xAA };

            byte[] FileData = FileTransferProtocol.GetFrameFileData(0x01AABBFF, data);

            //帧头
            Trace.Assert(FileData[0] == FileTransferProtocol.FrameHead);
            //帧偏移
            Trace.Assert((FileData[FileData.Length - 3] * 256 + FileData[FileData.Length - 4]) == (FileData.Length - 1));
            //命令字
            Trace.Assert(FileData[1] == 0x1);

            //文件偏移
            Trace.Assert(FileData[2] == 0xFF);
            Trace.Assert(FileData[3] == 0xBB);
            Trace.Assert(FileData[4] == 0xAA);
            Trace.Assert(FileData[5] == 0x01);

            //文件数据
            Trace.Assert(FileData[6] == 0x01);
            Trace.Assert(FileData[7] == 0x02);
            Trace.Assert(FileData[8] == 0xFF);
            Trace.Assert(FileData[9] == 0xEF);

            Trace.Assert(FileData[10] == 0xCC);
            Trace.Assert(FileData[11] == 0xDD);

            //crc
            //byte[] x = new byte[] { 0x02, 0xFF, 0xBB, 0xAA, 0x01, 0x01, 0x02, 0xFF, 0xEF, 0xCC, 0xDD, 0x00, 0xAA, FileData[FileData.Length - 3], FileData[FileData.Length - 2] };

            //Trace.Assert(FileData[1] == crc.Crc(x, 0, x.Length));
            Trace.Assert(FileData[FileData.Length - 1] == 0x7F);

            //文件发送完毕
            byte[] f = FileTransferProtocol.GetFrameFileSendFinish();

            //帧头
            Trace.Assert(f[0] == FileTransferProtocol.FrameHead);
            //帧偏移
            Trace.Assert((f[f.Length - 3] * 256 + f[f.Length - 4]) == f.Length - 1);
            //命令字
            Trace.Assert(f[1] == 0x02);
            //crc
            //Trace.Assert(f[1] == crc.Crc(f, dataStartIndex, f.Length - 3));
            Trace.Assert(f[f.Length - 1] == 0x7F);


            //表示可以新的文件
            byte[] a = FileTransferProtocol.GetFrameWhatIsYourMNandVersion();
            TestCmdFrame(a, 0xFD, crc);

            //表示已经接收数据
            //byte[] Ac = FileTransferProtocol.GetFrameMNAndVersion(1,"123");
            //TestCmdFrame(Ac, 0xFE, crc);

            //表示已经保存文件完毕
            byte[] s = FileTransferProtocol.GetFrameCloseSocket();

            TestCmdFrame(s, 0xFF, crc);
        }