当前位置: 首页 > wzjs >正文

网站开发的心得与体会一般注册公司多少钱

网站开发的心得与体会,一般注册公司多少钱,免费的企业品牌策划公司,wordpress 内容管理系统WPF 上位机开发模板 WPF上位机开发模板,集成了基础操作菜单、海康视觉实时图像界面、串口通讯、网口通讯、主流PLC通讯、数据存储、图片存储、参数配置、权限管理、第三方webapi接口接入、数据追溯与查询等功能。 一、项目结构 WpfSupervisor/ ├── Models/ …

WPF 上位机开发模板

WPF上位机开发模板,集成了基础操作菜单、海康视觉实时图像界面、串口通讯、网口通讯、主流PLC通讯、数据存储、图片存储、参数配置、权限管理、第三方webapi接口接入、数据追溯与查询等功能。

一、项目结构

WpfSupervisor/
├── Models/                  # 数据模型
│   ├── DeviceModels.cs
│   ├── ImageModel.cs
│   ├── LogModel.cs
│   ├── ParameterModel.cs
│   └── UserModel.cs
├── Services/                # 服务层
│   ├── Communication/
│   │   ├── ComService.cs
│   │   ├── EthernetService.cs
│   │   ├── PlcService.cs
│   │   └── WebApiService.cs
│   ├── Database/
│   │   ├── DatabaseService.cs
│   │   └── ImageStorage.cs
│   ├── HikVision/
│   │   └── HikVisionService.cs
│   ├── Security/
│   │   ├── AuthService.cs
│   │   └── PermissionService.cs
│   └── Utility/
│       ├── ConfigManager.cs
│       └── Logger.cs
├── ViewModels/              # 视图模型
│   ├── MainViewModel.cs
│   ├── CommunicationViewModel.cs
│   ├── ImageViewModel.cs
│   ├── ParameterViewModel.cs
│   └── UserViewModel.cs
├── Views/                   # 视图
│   ├── MainWindow.xaml
│   ├── CommunicationView.xaml
│   ├── ImageView.xaml
│   ├── ParameterView.xaml
│   └── LoginView.xaml
├── Helpers/                 # 辅助类
│   ├── RelayCommand.cs
│   └── EnumExtensions.cs
└── App.xaml.cs              # 应用程序入口

二、核心代码实现

1. 数据模型 (Models/)

// DeviceModels.cs
public class PlcDevice
{public string Id { get; set; }public string Name { get; set; }public string IpAddress { get; set; }public int Port { get; set; }public string Protocol { get; set; } // Modbus, S7, etc.
}public class SerialDevice
{public string Id { get; set; }public string Name { get; set; }public string PortName { get; set; }public int BaudRate { get; set; }public Parity Parity { get; set; }public int DataBits { get; set; }public StopBits StopBits { get; set; }
}// ImageModel.cs
public class CapturedImage
{public string Id { get; set; }public byte[] ImageData { get; set; }public DateTime CaptureTime { get; set; }public string DeviceId { get; set; }public string FilePath { get; set; }
}// LogModel.cs
public class SystemLog
{public string Id { get; set; }public DateTime Timestamp { get; set; }public string Level { get; set; } // Info, Warning, Errorpublic string Message { get; set; }public string UserId { get; set; }
}// ParameterModel.cs
public class SystemParameter
{public string Id { get; set; }public string Key { get; set; }public string Value { get; set; }public string Description { get; set; }public string Category { get; set; }
}// UserModel.cs
public class User
{public string Id { get; set; }public string Username { get; set; }public string PasswordHash { get; set; }public string FullName { get; set; }public string Role { get; set; } // Admin, Operator, etc.public DateTime LastLogin { get; set; }
}

2. 服务层 (Services/)

2.1 通信服务
// ComService.cs
public class ComService : IDisposable
{private SerialPort _serialPort;public event Action<string> DataReceived;public bool IsOpen => _serialPort?.IsOpen ?? false;public void Open(SerialDevice device){_serialPort = new SerialPort(device.PortName, device.BaudRate, device.Parity, device.DataBits, device.StopBits);_serialPort.DataReceived += (s, e) => {try{var data = _serialPort.ReadExisting();DataReceived?.Invoke(data);}catch (Exception ex){Logger.LogError($"串口数据接收错误: {ex.Message}");}};_serialPort.Open();}public void Close() => _serialPort?.Close();public void Send(string data) => _serialPort?.Write(data);public void Dispose() => Close();
}// EthernetService.cs
public class EthernetService : IDisposable
{private TcpClient _tcpClient;private NetworkStream _stream;public event Action<string> DataReceived;public bool IsConnected => _tcpClient?.Connected ?? false;public async Task ConnectAsync(string ipAddress, int port){_tcpClient = new TcpClient();await _tcpClient.ConnectAsync(ipAddress, port);_stream = _tcpClient.GetStream();_ = ReceiveDataAsync();}private async Task ReceiveDataAsync(){try{var buffer = new byte[1024];while (_stream != null && _stream.CanRead){int bytesRead = await _stream.ReadAsync(buffer, 0, buffer.Length);var data = Encoding.ASCII.GetString(buffer, 0, bytesRead);DataReceived?.Invoke(data);}}catch (Exception ex){Logger.LogError($"以太网数据接收错误: {ex.Message}");}}public void Send(string data){if (_stream == null || !_stream.CanWrite) return;var bytes = Encoding.ASCII.GetBytes(data);_stream.Write(bytes, 0, bytes.Length);}public void Disconnect() => _tcpClient?.Close();public void Dispose(){Disconnect();_stream?.Close();}
}// PlcService.cs (使用S7.Net库示例)
public class PlcService : IDisposable
{private S7.Net.PLC _plc;public event Action<string> DataReceived;public bool IsConnected => _plc?.IsConnected ?? false;public async Task ConnectAsync(PlcDevice device){_plc = new S7.Net.PLC(device.Protocol == "S7" ? S7.Net.CpuType.S71200 : S7.Net.CpuType.S7300, device.IpAddress, device.Port);await Task.Run(() => _plc.Open());}public async Task<T> ReadAsync<T>(string address){if (!IsConnected) throw new InvalidOperationException("PLC未连接");return await Task.Run(() => (T)_plc.Read(address));}public async Task WriteAsync<T>(string address, T value){if (!IsConnected) throw new InvalidOperationException("PLC未连接");await Task.Run(() => _plc.Write(address, value));}public void Dispose() => _plc?.Close();
}// WebApiService.cs
public class WebApiService
{private readonly HttpClient _httpClient;private readonly string _baseUrl;public WebApiService(string baseUrl){_baseUrl = baseUrl.EndsWith("/") ? baseUrl : baseUrl + "/";_httpClient = new HttpClient();}public async Task<T> GetAsync<T>(string endpoint){var response = await _httpClient.GetAsync(_baseUrl + endpoint);response.EnsureSuccessStatusCode();return await response.Content.ReadAsAsync<T>();}public async Task PostAsync<T>(string endpoint, object data){var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");var response = await _httpClient.PostAsync(_baseUrl + endpoint, content);response.EnsureSuccessStatusCode();}
}
2.2 数据库服务
 
// DatabaseService.cs
public class DatabaseService
{private readonly string _connectionString;private SQLiteConnection _connection;public DatabaseService(string dbPath){_connectionString = $"Data Source={dbPath};Version=3;";InitializeDatabase();}private void InitializeDatabase(){_connection = new SQLiteConnection(_connectionString);_connection.Open();// 创建表ExecuteNonQuery(@"CREATE TABLE IF NOT EXISTS SystemLogs (Id TEXT PRIMARY KEY,Timestamp TEXT,Level TEXT,Message TEXT,UserId TEXT);CREATE TABLE IF NOT EXISTS SystemParameters (Id TEXT PRIMARY KEY,Key TEXT,Value TEXT,Description TEXT,Category TEXT);CREATE TABLE IF NOT EXISTS Users (Id TEXT PRIMARY KEY,Username TEXT,PasswordHash TEXT,FullName TEXT,Role TEXT,LastLogin TEXT);");}public void ExecuteNonQuery(string sql, params object[] parameters){using (var cmd = new SQLiteCommand(sql, _connection)){for (int i = 0; i < parameters.Length; i++){cmd.Parameters.AddWithValue($"@p{i}", parameters[i]);}cmd.ExecuteNonQuery();}}public T ExecuteScalar<T>(string sql, params object[] parameters){using (var cmd = new SQLiteCommand(sql, _connection)){for (int i = 0; i < parameters.Length; i++){cmd.Parameters.AddWithValue($"@p{i}", parameters[i]);}return (T)cmd.ExecuteScalar();}}public DataTable ExecuteQuery(string sql, params object[] parameters){using (var cmd = new SQLiteCommand(sql, _connection)){for (int i = 0; i < parameters.Length; i++){cmd.Parameters.AddWithValue($"@p{i}", parameters[i]);}var adapter = new SQLiteDataAdapter(cmd);var table = new DataTable();adapter.Fill(table);return table;}}public void Dispose(){_connection?.Close();}
}// ImageStorage.cs
public class ImageStorage
{private readonly string _imageFolderPath;private readonly DatabaseService _dbService;public ImageStorage(string folderPath, DatabaseService dbService){_imageFolderPath = folderPath;Directory.CreateDirectory(_imageFolderPath);_dbService = dbService;}public async Task SaveImageAsync(CapturedImage image){// 保存到数据库var imageId = Guid.NewGuid().ToString();var parameters = new object[]{imageId,image.ImageData != null ? Convert.ToBase64String(image.ImageData) : null,image.CaptureTime.ToString("o"),image.DeviceId,image.FilePath};_dbService.ExecuteNonQuery(@"INSERT INTO Images (Id, Data, CaptureTime, DeviceId, FilePath)VALUES (@p0, @p1, @p2, @p3, @p4);", parameters);// 保存文件if (image.ImageData != null){var filePath = Path.Combine(_imageFolderPath, $"{imageId}.jpg");await File.WriteAllBytesAsync(filePath, image.ImageData);// 更新数据库中的文件路径_dbService.ExecuteNonQuery(@"UPDATE Images SET FilePath = @p0 WHERE Id = @p1;", filePath, imageId);}}public async Task<CapturedImage> GetImageAsync(string id){var row = _dbService.ExecuteQuery("SELECT * FROM Images WHERE Id = @p0;", id).Rows[0];return new CapturedImage{Id = row["Id"].ToString(),ImageData = row["Data"] != DBNull.Value ? Convert.FromBase64String(row["Data"].ToString()) : null,CaptureTime = DateTime.Parse(row["CaptureTime"].ToString()),DeviceId = row["DeviceId"].ToString(),FilePath = row["FilePath"]?.ToString()};}
}
2.3 海康视觉服务
// HikVisionService.cs
public class HikVisionService
{private readonly HttpClient _httpClient;private readonly string _baseUrl;private readonly string _username;private readonly string _password;public event Action<byte[]> ImageReceived;public HikVisionService(string baseUrl, string username, string password){_baseUrl = baseUrl.EndsWith("/") ? baseUrl : baseUrl + "/";_username = username;_password = password;_httpClient = new HttpClient();// 登录Login();}private void Login(){var loginData = new Dictionary<string, string>{{"action", "login"},{"user", _username},{"password", _password}};var content = new FormUrlEncodedContent(loginData);var response = _httpClient.PostAsync(_baseUrl + "login", content).Result;response.EnsureSuccessStatusCode();}public async Task StartRealTimeImage(){// 启动实时图像流var streamResponse = await _httpClient.GetAsync($"{_baseUrl}stream");streamResponse.EnsureSuccessStatusCode();var stream = await streamResponse.Content.ReadAsStreamAsync();// 处理图像流数据using (var reader = new BinaryReader(stream)){while (true){// 实际实现需要解析海康威视的流协议// 这里简化处理var buffer = reader.ReadBytes(1024);if (buffer.Length > 0){// 解码图像数据var imageData = DecodeHikVisionImage(buffer);ImageReceived?.Invoke(imageData);}}}}private byte[] DecodeHikVisionImage(byte[] buffer){// 实际实现需要根据海康威视的图像编码格式解码// 这里简化处理,直接返回原始数据return buffer;}
}
2.4 安全服务
 
// AuthService.cs
public class AuthService
{private readonly DatabaseService _dbService;public AuthService(DatabaseService dbService){_dbService = dbService;}public async Task<User> LoginAsync(string username, string password){var userRow = _dbService.ExecuteQuery("SELECT * FROM Users WHERE Username = @p0;", username).Rows[0];var user = MapUserFromRow(userRow);// 验证密码if (VerifyPassword(password, user.PasswordHash)){user.LastLogin = DateTime.UtcNow.ToString("o");await UpdateUserAsync(user);return user;}return null;}private bool VerifyPassword(string inputPassword, string storedHash){// 实际实现应使用安全的密码哈希验证// 这里简化处理return inputPassword == storedHash; }public async Task<User> GetUserAsync(string userId){var row = _dbService.ExecuteQuery("SELECT * FROM Users WHERE Id = @p0;", userId).Rows[0];return MapUserFromRow(row);}private User MapUserFromRow(DataRow row){return new User{Id = row["Id"].ToString(),Username = row["Username"].ToString(),PasswordHash = row["PasswordHash"].ToString(),FullName = row["FullName"].ToString(),Role = row["Role"].ToString(),LastLogin = row["LastLogin"]?.ToString()};}private async Task UpdateUserAsync(User user){_dbService.ExecuteNonQuery(@"UPDATE Users SET LastLogin = @p0 WHERE Id = @p1;",user.LastLogin, user.Id);}
}// PermissionService.cs
public class PermissionService
{private readonly DatabaseService _dbService;public PermissionService(DatabaseService dbService){_dbService = dbService;}public async Task<bool> HasPermissionAsync(string userId, string permission){// 从数据库查询用户权限var hasPermission = await _dbService.ExecuteScalarAsync<bool>("SELECT COUNT(*) > 0 FROM UserPermissions WHERE UserId = @p0 AND Permission = @p1;",userId, permission);return hasPermission;}public async Task<IEnumerable<string>> GetUserPermissionsAsync(string userId){var permissions = await _dbService.ExecuteQueryAsync("SELECT Permission FROM UserPermissions WHERE UserId = @p0;", userId);return permissions.Select(r => r["Permission"].ToString());}
}

3. 视图模型 (ViewModels/)

// MainViewModel.cs
public class MainViewModel : INotifyPropertyChanged
{private readonly IEventAggregator _eventAggregator;private readonly AuthService _authService;private readonly PermissionService _permissionService;private object _currentView;private User _currentUser;public object CurrentView{get => _currentView;set { _currentView = value; OnPropertyChanged(); }}public User CurrentUser{get => _currentUser;private set { _currentUser = value; OnPropertyChanged(); }}public ICommand LoginCommand { get; }public ICommand LogoutCommand { get; }public MainViewModel(IEventAggregator eventAggregator,AuthService authService,PermissionService permissionService){_eventAggregator = eventAggregator;_authService = authService;_permissionService = permissionService;LoginCommand = new RelayCommand(Login);LogoutCommand = new RelayCommand(Logout, CanLogout);}private async void Login(){// 实际实现应显示登录对话框var loginView = new LoginView();if (loginView.ShowDialog() == true){var user = await _authService.LoginAsync(loginView.Username, loginView.Password);if (user != null){CurrentUser = user;CurrentView = new ShellViewModel(_eventAggregator, user).View;_eventAggregator.Publish(new UserLoggedInEvent(user));}}}private void Logout(){CurrentUser = null;CurrentView = new LoginView();_eventAggregator.Publish(new UserLoggedOutEvent());}private bool CanLogout() => CurrentUser != null;public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged([CallerMemberName] string propertyName = null){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}
}// CommunicationViewModel.cs
public class CommunicationViewModel : INotifyPropertyChanged
{private readonly ComService _comService;private readonly EthernetService _ethernetService;private readonly PlcService _plcService;private readonly WebApiService _webApiService;private SerialDevice _selectedSerialDevice;private PlcDevice _selectedPlcDevice;public ObservableCollection<SerialDevice> SerialDevices { get; } = new();public ObservableCollection<PlcDevice> PlcDevices { get; } = new();public SerialDevice SelectedSerialDevice{get => _selectedSerialDevice;set { _selectedSerialDevice = value; OnPropertyChanged();OpenSerialPort();}}public PlcDevice SelectedPlcDevice{get => _selectedPlcDevice;set { _selectedPlcDevice = value; OnPropertyChanged();ConnectPlc();}}public ICommand RefreshDevicesCommand { get; }public ICommand SendSerialCommand { get; }public ICommand ReadPlcCommand { get; }public ICommand WritePlcCommand { get; }public CommunicationViewModel(ComService comService,EthernetService ethernetService,PlcService plcService,WebApiService webApiService){_comService = comService;_ethernetService = ethernetService;_plcService = plcService;_webApiService = webApiService;RefreshDevicesCommand = new RelayCommand(RefreshDevices);SendSerialCommand = new RelayCommand(SendSerialData, CanSendSerial);ReadPlcCommand = new RelayCommand(ReadPlcData, CanReadPlc);WritePlcCommand = new RelayCommand(WritePlcData, CanWritePlc);LoadDevices();}

文章转载自:

http://kdRcmN1h.gtkyr.cn
http://B6rnFeCh.gtkyr.cn
http://RjvqYMPL.gtkyr.cn
http://OiYrBkDF.gtkyr.cn
http://e17IltVy.gtkyr.cn
http://i6XrNFBq.gtkyr.cn
http://YZ7efV3f.gtkyr.cn
http://inBy1kkZ.gtkyr.cn
http://GCB1mrJN.gtkyr.cn
http://1ZGPnlTy.gtkyr.cn
http://oAYVm6N0.gtkyr.cn
http://7vEDkqXf.gtkyr.cn
http://099DH8P4.gtkyr.cn
http://OUR63848.gtkyr.cn
http://ToHG6QM9.gtkyr.cn
http://dI0toiN2.gtkyr.cn
http://dMgLNm77.gtkyr.cn
http://OQ6wTC4u.gtkyr.cn
http://Jz1sBdI1.gtkyr.cn
http://8V8F2Ntb.gtkyr.cn
http://nsjQsVDv.gtkyr.cn
http://ch6eAiz6.gtkyr.cn
http://uesa4gwm.gtkyr.cn
http://KvgDGnHF.gtkyr.cn
http://ydp5zKD6.gtkyr.cn
http://XqF9oNBH.gtkyr.cn
http://MA7ImG39.gtkyr.cn
http://TU4NrBTl.gtkyr.cn
http://lqklFLLR.gtkyr.cn
http://Jq8CiexL.gtkyr.cn
http://www.dtcms.com/wzjs/622280.html

相关文章:

  • 做一个简单的网站怎么做网站如何报备
  • 班级网站素材下载电子商务网站开发 pdf
  • 网站开发的整个流程电商网站建设推广
  • 个人网站有必要备案吗wordpress怎么可以出现表格
  • 帝国cms做电影网站免费推广引流
  • 福州手游网站建设最便宜建站
  • 西安市高新区建设规划局网站wordpress 后台被锁定
  • 温州公司建设网站制作个人网站建设的花费
  • 哪个网站做ppt赚钱响应式视频网站模板
  • 商城站黑群晖可以做网站吗
  • 做书的网站有哪些内容吗企业邮箱免费版注册
  • 西安是哪个省哪个市网站打开速度优化
  • wordpress建站哪里好中国建设监理协会网站个人会员系统
  • 酒店网站建设必要性wordpress 自动跳转
  • 网站页面设计素材金色金融公司网站源码
  • 个人做的网站能备案吗深圳企业排名100强
  • 南昌企业做网站设计网站大量死链
  • 网站如何做双语言php网站开发实战视频
  • 网站建设的报价单龙岩网站建设套餐报价
  • 网站建设氺金手指排名11公司域名是什么意思
  • 服务器网站网站专用新开传奇网站排行
  • 网站前期定位京津冀协同发展建议
  • 安徽响应式网站建设哪家有单页面网站制作技术
  • 通过网站做跳板分销系统开发多少钱
  • 中国建设银行三峡分行网站可以上传视频的网站建设
  • 网站百度不到验证码怎么办啊创造一个网页
  • 网站建设分金手指专业六榆林市城乡建设规划局网站
  • 保定网站建设平台分析专门做详情页的网站
  • 兰州网站建设哪家公司好网站建设总结ppt
  • 网站建设广州网站建设写作网站推荐