该Demo尝试利用C#来读取PLC触摸屏的报警信息和操作日志写入工控机并保存
通过访问FTP的链接,在FTP当中将所需要的文件进行拷贝,复制到指定位置,然后
通过CMD来对触摸屏进行格式转换
该方法可直接实现将FTP里面db文件、evt文件等直接转换成CSV文档,至于可以转换哪些格式,请翻阅触摸屏关于存储ftp文件格式的说明
这份代码是以威纶通触摸屏为模板写的,有需要的朋友可以看看
下面展示一些 内联代码片
。
// An highlighted block
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace cmd
{
class Program
{
#region FTP
private static string host = null;
private static string user = null;
private static string pass = null;
private static FtpWebRequest ftpRequest = null;
private static FtpWebResponse ftpResponse = null;
private static Stream ftpStream = null;
private static int bufferSize = 8192;
public static bool BinaryMode = true;
//创建对象
public Program(string hostIP, string userName, string password)
{
host = hostIP;
user = userName;
pass = password;
}
//从FTP获取
public static void get(string remoteFile, string localFile)
{
try
{
//创建FTP请求,将ftp的链接和文件名传入
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
//使用提供的用户名和密码登录FTP服务器 ,这个用户名和密码在触摸屏的说明文档里
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = BinaryMode;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
//配置FTP请求的类型
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
//与FTP服务器建立返回通信
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
//获取FTP服务器的响应流
ftpStream = ftpResponse.GetResponseStream();
//打开文件流来写入下载的文件
FileStream localFileStream = new FileStream(localFile, FileMode.Create);
//下载数据的缓冲区
byte[] byteBuffer = new byte[bufferSize];
int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
//通过写入缓冲数据来下载文件,直到传输完成
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer