Commit 1e8500ca by zxw

充电台集成索力得读写器

温湿度接入
parent 3803bfa9
......@@ -111,7 +111,7 @@
this.ddb_warehouse.ImageTransparentColor = System.Drawing.Color.Magenta;
this.ddb_warehouse.Name = "ddb_warehouse";
this.ddb_warehouse.ShowDropDownArrow = false;
this.ddb_warehouse.Size = new System.Drawing.Size(204, 20);
this.ddb_warehouse.Size = new System.Drawing.Size(203, 20);
this.ddb_warehouse.Text = "当前仓库:未获取云端配置";
//
// btn_clear
......@@ -180,6 +180,7 @@
this.txt_log.Font = new System.Drawing.Font("幼圆", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.txt_log.Location = new System.Drawing.Point(0, 72);
this.txt_log.Name = "txt_log";
this.txt_log.ReadOnly = true;
this.txt_log.Size = new System.Drawing.Size(863, 395);
this.txt_log.TabIndex = 45;
this.txt_log.Text = "";
......
......@@ -152,37 +152,45 @@ namespace JmpServiceMgr
}
}
private class ColorLog
{
public string Text { get; set; }
public Color Color { get; set; }
}
private LinkedList<ColorLog> logList = new LinkedList<ColorLog>();
private readonly object _lockObj = new object();
public void SetLogs(string msg, Color color = default)
{
if (color == default)
{
color = Color.Black;
}
this.Invoke((Action)delegate
lock (_lockObj)
{
if (txt_log.Text.Length > 30000)
logList.AddFirst(new ColorLog
{
txt_log.ResetText();
}
var txt = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} - {msg}\r\n";
//txt_log.Text = txt_log.Text.Insert(0, txt);
AppendTextColorful(txt_log, txt, color, false);
Color = color,
Text = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} - {msg}\r\n"
});
Application.DoEvents();
if (logList.Count > 100)
{
logList.RemoveLast();
}
private void AppendTextColorful(RichTextBox rtBox, string text, Color color, bool addNewLine)
}
this.Invoke((Action)delegate
{
if (addNewLine)
txt_log.ResetText();
foreach (var colorLog in logList)
{
text += Environment.NewLine;
txt_log.SelectionColor = colorLog.Color;
txt_log.AppendText(colorLog.Text);
}
rtBox.SelectionStart = rtBox.TextLength;
rtBox.SelectionLength = 0;
rtBox.SelectionColor = color;
rtBox.AppendText(text);
rtBox.SelectionColor = rtBox.ForeColor;
txt_log.SelectionStart = 0;
txt_log.ScrollToCaret();
});
Application.DoEvents();
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
......

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30104.148
# Visual Studio Version 17
VisualStudioVersion = 17.4.33110.190
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JmpServiceMgr", "JmpServiceMgr\JmpServiceMgr.csproj", "{442DABC1-9869-4B57-BFCC-287106799248}"
EndProject
......@@ -15,6 +15,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LightCtrl", "LightCtrl\Ligh
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LiteChannelWinXP", "LiteChannelWinXP\LiteChannelWinXP.csproj", "{48900448-82F0-4876-9987-0AAB845027D5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransmitterService", "TransmitterService\TransmitterService.csproj", "{FB7E4EB3-F9C2-42DA-89CC-D8831C462CB5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -45,12 +47,16 @@ Global
{48900448-82F0-4876-9987-0AAB845027D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{48900448-82F0-4876-9987-0AAB845027D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{48900448-82F0-4876-9987-0AAB845027D5}.Release|Any CPU.Build.0 = Release|Any CPU
{FB7E4EB3-F9C2-42DA-89CC-D8831C462CB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB7E4EB3-F9C2-42DA-89CC-D8831C462CB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB7E4EB3-F9C2-42DA-89CC-D8831C462CB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB7E4EB3-F9C2-42DA-89CC-D8831C462CB5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
VisualSVNWorkingCopyRoot = .
SolutionGuid = {F1BB736E-801B-4BAD-8BC5-A7BA50F023D7}
VisualSVNWorkingCopyRoot = .
EndGlobalSection
EndGlobal
......@@ -124,6 +124,7 @@ namespace LiteChannel
ShelfAddress = "http://192.168.3.128:38080/api/BoundDocuments";
IsAutoUpdate = true;
AutoUpdateTime = 0;
ReaderType = 0;
}
/// <summary>
/// 接口域名
......@@ -162,6 +163,13 @@ namespace LiteChannel
/// </summary>
public int AutoUpdateTime { get; set; }
/// <summary>
/// 读写器类型
/// 0 钧普
/// 1 索力得
/// </summary>
public int ReaderType { get; set; }
[JsonIgnore]
public static new bool IsInDesignModeStatic { get; }
}
......
......@@ -41,6 +41,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
......@@ -50,6 +51,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>1.ico</ApplicationIcon>
......@@ -123,6 +125,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Commons\ReaderHelper.cs" />
<Compile Include="SelectPoliceWindow.xaml.cs">
<DependentUpon>SelectPoliceWindow.xaml</DependentUpon>
</Compile>
......@@ -259,6 +262,18 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="ThirdPackages\plugin\hik\GdiPlus.lib" />
<None Include="ThirdPackages\plugin\hik\HCCore.lib" />
<None Include="ThirdPackages\plugin\hik\HCNetSDK.lib" />
<None Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCAlarm.lib" />
<None Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCGeneralCfgMgr.lib" />
<None Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCPreview.lib" />
<None Include="ThirdPackages\plugin\hik\LocalXml.zip" />
<None Include="ThirdPackages\plugin\hik\log4cxx.properties" />
<None Include="ThirdPackages\plugin\hik\PlayCtrl.lib" />
<None Include="ThirdPackages\tts\common.jet" />
<None Include="ThirdPackages\tts\xiaofeng.jet" />
<None Include="ThirdPackages\tts\xiaoyan.jet" />
<Resource Include="Font\iconfont.ttf" />
<None Include="config.json" />
<None Include="packages.config" />
......@@ -298,6 +313,43 @@
<ItemGroup>
<Resource Include="Resources\2.png" />
</ItemGroup>
<ItemGroup>
<Content Include="ThirdPackages\msc.dll" />
<Content Include="ThirdPackages\msc_x64.dll" />
<Content Include="ThirdPackages\plugin\hik\AudioRender.dll" />
<Content Include="ThirdPackages\plugin\hik\EagleEyeRender.dll" />
<Content Include="ThirdPackages\plugin\hik\gdiplus.dll" />
<Content Include="ThirdPackages\plugin\hik\HCCore.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDK.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\AnalyzeData.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\AudioIntercom.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCAlarm.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCCoreDevCfg.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCDisplay.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCGeneralCfgMgr.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCIndustry.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCPlayBack.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCPreview.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\HCVoiceTalk.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\libiconv2.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\msvcr90.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\OpenAL32.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\StreamTransClient.dll" />
<Content Include="ThirdPackages\plugin\hik\HCNetSDKCom\SystemTransform.dll" />
<Content Include="ThirdPackages\plugin\hik\hlog.dll" />
<Content Include="ThirdPackages\plugin\hik\hpr.dll" />
<Content Include="ThirdPackages\plugin\hik\HXVA.dll" />
<Content Include="ThirdPackages\plugin\hik\libeay32.dll" />
<Content Include="ThirdPackages\plugin\hik\MP_Render.dll" />
<Content Include="ThirdPackages\plugin\hik\MP_VIE.dll" />
<Content Include="ThirdPackages\plugin\hik\NPQos.dll" />
<Content Include="ThirdPackages\plugin\hik\PlayCtrl.dll" />
<Content Include="ThirdPackages\plugin\hik\ssleay32.dll" />
<Content Include="ThirdPackages\plugin\hik\SuperRender.dll" />
<Content Include="ThirdPackages\plugin\hik\YUVProcess.dll" />
<Content Include="ThirdPackages\plugin\hik\zlib1.dll" />
<Content Include="ThirdPackages\USBUHFPlatform.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\MaterialDesignThemes.3.1.3\build\MaterialDesignThemes.targets" Condition="Exists('..\packages\MaterialDesignThemes.3.1.3\build\MaterialDesignThemes.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
......
......@@ -51,5 +51,5 @@ using System.Windows;
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.12.12070")]
[assembly: AssemblyFileVersion("1.2.12.12070")]
[assembly: AssemblyVersion("1.2.13.12290")]
[assembly: AssemblyFileVersion("1.2.13.12290")]
......@@ -6,7 +6,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:LiteChannel"
mc:Ignorable="d"
Title="系统配置" Height="460" Loaded="OnWindow_Loaded" Width="800" Icon="1.ico" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
Title="系统配置" Height="500" Loaded="OnWindow_Loaded" Width="800" Icon="1.ico" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
......@@ -18,6 +18,7 @@
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
<RowDefinition Height="10"/>
<RowDefinition Height="40"/>
<RowDefinition Height="40"/>
......@@ -43,9 +44,6 @@
<TextBlock Grid.Row="1" Grid.Column="3" Text="登录接口:" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox Grid.Row="1" Grid.Column="4" x:Name="txt_login" Width="auto" Height="25" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
<TextBlock Grid.Row="2" Grid.Column="3" Text="串口编号:" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<ComboBox Grid.Row="2" Grid.Column="4" x:Name="cbo_com" Width="auto" Height="25" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="程序标题:" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<TextBox Grid.Row="2" Grid.Column="2" x:Name="txt_title" Width="auto" Height="25" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
......@@ -123,10 +121,21 @@
<ComboBoxItem>23:00</ComboBoxItem>
</ComboBox>
<TextBlock Grid.Row="9" Grid.Column="1" Text="读写器类型:" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<ComboBox Grid.Row="9" Grid.Column="2" x:Name="cbo_readerType" Width="auto" Height="25"
HorizontalAlignment="Stretch" VerticalAlignment="Center"
pu:ComboBoxHelper.HoverBackground="#1EB5B5B5"
pu:ComboBoxHelper.SelectedBackground="#32B5B5B5"
pu:ComboBoxHelper.IsSearchTextBoxVisible="False" SelectionChanged="cbo_readerType_SelectionChanged">
<ComboBoxItem>钧普</ComboBoxItem>
<ComboBoxItem>索力得</ComboBoxItem>
</ComboBox>
<TextBlock Grid.Row="9" Grid.Column="3" x:Name="tb_com" Text="串口编号:" HorizontalAlignment="Right" VerticalAlignment="Center"/>
<ComboBox Grid.Row="9" Grid.Column="4" x:Name="cbo_com" Width="auto" Height="25" HorizontalAlignment="Stretch" VerticalAlignment="Center"/>
<Button Grid.Row="10" Grid.ColumnSpan="6" Click="OnSave_Click" HorizontalAlignment="Right" Width="90" Margin="0,0,110,0">保存配置</Button>
<Button Grid.Row="11" Grid.ColumnSpan="6" Click="OnSave_Click" HorizontalAlignment="Right" Width="90" Margin="0,0,110,0">保存配置</Button>
<!--<Button Grid.Row="14" Grid.ColumnSpan="6" Click="OnRefresh_Click" HorizontalAlignment="Right" Width="90" Margin="0,0,110,0">刷新仓库</Button>-->
<Button Grid.Row="10" Grid.ColumnSpan="6" Click="OnCancel_Click" HorizontalAlignment="Right" Background="#C8FF7F00" pu:ButtonHelper.HoverBrush="#FF7F00" Width="90" Margin="0,0,10,0">关闭</Button>
<Button Grid.Row="11" Grid.ColumnSpan="6" Click="OnCancel_Click" HorizontalAlignment="Right" Background="#C8FF7F00" pu:ButtonHelper.HoverBrush="#FF7F00" Width="90" Margin="0,0,10,0">关闭</Button>
</Grid>
</pu:WindowX>
......@@ -39,7 +39,6 @@ namespace LiteChannel
LiteCaChe.SysConfig.DomainUrl = txt_domain.Text;
//LiteCaChe.SysConfig.AppTitle = txt_title.Text;
//LiteCaChe.SysConfig.AppVersion = ver;
LiteCaChe.SysConfig.ComMouth = cbo_com.Text;
//LiteCaChe.SysConfig.WarehouseID = cbo_warehouse.SelectedValue?.ToString() ?? "";
LiteCaChe.SysConfig.FaceAddress = txt_ip.Text;
......@@ -57,6 +56,12 @@ namespace LiteChannel
LiteCaChe.SysConfig.IsAutoUpdate = cb_autoUpdate.IsChecked == true;
LiteCaChe.SysConfig.AutoUpdateTime = cbb_autoUpdate.SelectedIndex;
LiteCaChe.SysConfig.ReaderType = cbo_readerType.SelectedIndex;
if (LiteCaChe.SysConfig.ReaderType == 0)
{
LiteCaChe.SysConfig.ComMouth = cbo_com.Text;
}
LiteCaChe.SaveSystemStep(LiteCaChe.SysConfig);
MessageBox.Show("配置保存成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
this.Close();
......@@ -86,7 +91,7 @@ namespace LiteChannel
txt_domain.Text = LiteCaChe.SysConfig.DomainUrl;
txt_title.Text = LiteCaChe.SysConfig.AppTitle;
txt_ver.Text = LiteCaChe.SysConfig.AppVersion.ToString();
cbo_com.Text = LiteCaChe.SysConfig.ComMouth;
//cbo_warehouse.SelectedValue = LiteCaChe.SysConfig.WarehouseID;
txt_ip.Text = LiteCaChe.SysConfig.FaceAddress;
......@@ -113,6 +118,19 @@ namespace LiteChannel
cbb_autoUpdate.SelectedIndex = LiteCaChe.SysConfig.AutoUpdateTime;
IsLoaded = true;
cbo_readerType.SelectedIndex = LiteCaChe.SysConfig.ReaderType;
if (LiteCaChe.SysConfig.ReaderType == 0)
{
tb_com.Visibility = Visibility.Visible;
cbo_com.Visibility = Visibility.Visible;
cbo_com.Text = LiteCaChe.SysConfig.ComMouth;
}
else
{
tb_com.Visibility = Visibility.Hidden;
cbo_com.Visibility = Visibility.Hidden;
}
}
//private void cbo_warehouse_SearchTextChanged(object sender, Panuon.UI.Silver.Core.SearchTextChangedEventArgs e)
......@@ -142,5 +160,20 @@ namespace LiteChannel
cbb_autoUpdate.IsEnabled = false;
}
}
private void cbo_readerType_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cbo_readerType.SelectedIndex == 0)
{
tb_com.Visibility = Visibility.Visible;
cbo_com.Visibility = Visibility.Visible;
cbo_com.Text = LiteCaChe.SysConfig.ComMouth;
}
else
{
tb_com.Visibility = Visibility.Hidden;
cbo_com.Visibility = Visibility.Hidden;
}
}
}
}
......@@ -29,7 +29,7 @@ namespace LiteChannel
/// </summary>
public partial class StoreInWindow : WindowX, IComponentConnector
{
private JmpRfidLp.Reader reader;
private IReaderHelper reader;
private List<scan_info> SourcesList = new List<scan_info>();
private List<scan_info> BoxMarkList = new List<scan_info>();
private Queue<string> _epcQueue = new Queue<string>();
......@@ -57,7 +57,19 @@ namespace LiteChannel
{
try
{ //启动读写器
reader = new Reader(LiteCaChe.SysConfig.ComMouth);
switch (LiteCaChe.SysConfig.ReaderType)
{
case 0:
{
reader = new JmpRfidLpReaderHelper(LiteCaChe.SysConfig.ComMouth);
break;
}
case 1:
{
reader = new USBRFIDReaderHelper();
break;
}
}
reader.OnInventory += OnRadioInventory;
var state = reader.Open();
if (state)
......@@ -93,7 +105,7 @@ namespace LiteChannel
return false;
}
}
private void OnRadioInventory(TagInfo args)
private void OnRadioInventory(TagInfoData args)
{
if (!BoxMarkList.Any(t => t.epcList.Contains(args.EPC)) &&
args.EPC.Length == 24 && args.EPC.Substring(0, 4) == "5842")
......
......@@ -29,7 +29,7 @@ namespace LiteChannel
/// </summary>
public partial class StoreOutWindow : WindowX, IComponentConnector
{
private JmpRfidLp.Reader reader;
private IReaderHelper reader;
private List<scan_info> SourcesList = new List<scan_info>();
private List<scan_info> BoxMarkList = new List<scan_info>();
private Queue<string> _epcQueue = new Queue<string>();
......@@ -119,7 +119,19 @@ namespace LiteChannel
{
try
{ //启动读写器
reader = new Reader(LiteCaChe.SysConfig.ComMouth);
switch (LiteCaChe.SysConfig.ReaderType)
{
case 0:
{
reader = new JmpRfidLpReaderHelper(LiteCaChe.SysConfig.ComMouth);
break;
}
case 1:
{
reader = new USBRFIDReaderHelper();
break;
}
}
reader.OnInventory += OnRadioInventory;
var state = reader.Open();
if (state)
......@@ -155,7 +167,7 @@ namespace LiteChannel
}
}
private void OnRadioInventory(TagInfo args)
private void OnRadioInventory(TagInfoData args)
{
if (!BoxMarkList.Any(t => t.epcList.Contains(args.EPC)) &&
args.EPC.Length == 24 && args.EPC.Substring(0, 4) == "5842")
......
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<appSettings>
<add key="ip" value="192.168.0.18"/>
<add key="port" value="502"/>
<add key="index" value="01"/>
</appSettings>
</configuration>
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace TransmitterService
{
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
static void Main()
{
//ServiceBase[] ServicesToRun;
//ServicesToRun = new ServiceBase[]
//{
// new Service1()
//};
//ServiceBase.Run(ServicesToRun);
new Service1().RunTest();
Console.ReadKey();
}
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("TransmitterService")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TransmitterService")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("fb7e4eb3-f9c2-42da-89cc-d8831c462cb5")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
namespace TransmitterService
{
partial class Service1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Configuration;
namespace TransmitterService
{
public partial class Service1 : ServiceBase
{
private TcpClient myTcpClient; //声明TCP客户端
private NetworkStream netStream; //声明网络数据流
private Thread receiveDataThread; //声明接收数据进程
private bool lostFlag;
private CancellationToken token;
private string ip = ConfigurationManager.AppSettings["ip"];
private string port = ConfigurationManager.AppSettings["port"];
private string index = ConfigurationManager.AppSettings["index"];
public Service1()
{
InitializeComponent();
}
#region tcp
private void Init()
{
//创建并实例化IP终结点
IPEndPoint iPEndPoint = new IPEndPoint(IPAddress.Parse(ip), Convert.ToInt32(port));
//实例化TCP客户端
myTcpClient = new TcpClient();
try
{
//发起TCP连接
myTcpClient.Connect(iPEndPoint);
//获取绑定的网络数据流
netStream = myTcpClient.GetStream();
//实例化接收数据进程
receiveDataThread = new Thread(ReceiveMsg);
receiveDataThread.Start();
lostFlag = false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// 接收消息线程
/// </summary>
private void ReceiveMsg()
{
Console.WriteLine("客户端已连接服务器!");
while (true)
{
try
{
//获取数据
byte[] getData = new byte[1024];
netStream.Read(getData, 0, getData.Length);
//todo 处理返回消息 00 00 00 00 00 07 01 03 04 0B 3E 70 BC
if (getData[0] == 0 && getData[1] == 0 && getData[2] == 0 && getData[3] == 0 && getData[7] == 3)
{
var sd = Convert.ToInt32("0x" + getData[9].ToString("X2") + getData[10].ToString("X2"), 16) / 100.0;
var wd = (Convert.ToInt32("0x" + getData[11].ToString("X2") + getData[12].ToString("X2"), 16) - 27315) / 100.0;
Console.WriteLine($"温度:{wd} 湿度:{sd}");
}
}
catch (ThreadAbortException)
{
}
catch (System.Exception ex)
{
lostFlag = true;
Console.WriteLine(ex.Message);
//释放相关资源
if (netStream != null)
{
netStream.Dispose();
}
break;
}
}
}
private void Reconnect()
{
Task.Run(() =>
{
while (!token.IsCancellationRequested)
{
try
{
if (lostFlag)
{
Init();
}
Thread.Sleep(10 * 1000);
}
catch (Exception e)
{
Console.WriteLine(e);
Thread.Sleep(10 * 1000);
}
}
}, token);
}
#endregion
public void RunTest()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
token = new CancellationToken();
Init();
Reconnect();
Task.Run(() =>
{
while (!token.IsCancellationRequested)
{
try
{
//发送查询命令 00 00 00 00 00 06 01 03 00 00 00 02
var index = Convert.ToInt32(this.index);
var msg = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, Convert.ToByte(index), 0x03, 0x00, 0x00, 0x00, 0x02 };
netStream.Write(msg, 0, msg.Length);
Thread.Sleep(30 * 1000); //30秒获取一次
}
catch (Exception e)
{
lostFlag = true;
Console.WriteLine(e);
Thread.Sleep(30 * 1000); //30秒获取一次
}
}
}, token);
}
protected override void OnStop()
{
//销毁TCP客户端和网络数据流的实例
myTcpClient.Close();
netStream.Dispose();
receiveDataThread.Abort();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{FB7E4EB3-F9C2-42DA-89CC-D8831C462CB5}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>TransmitterService</RootNamespace>
<AssemblyName>TransmitterService</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Service1.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Service1.Designer.cs">
<DependentUpon>Service1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论