C#實(shí)現(xiàn)簡(jiǎn)單獲取掃碼槍信息代碼
一個(gè)掃碼槍遵循TCP協(xié)議,通過(guò)改代碼即可獲取掃碼槍所掃描的信息;(有一個(gè)串口服務(wù)器);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
using System.Net;
namespace Demo_Net
{
//本機(jī)為服務(wù)端
//下午加一個(gè)判斷網(wǎng)絡(luò)是否連接;以及做出相應(yīng)的判斷;
class Program
{
static Socket msock;
static void Main(string[] args)
{
//先判斷是否ping通:
string ips = "10.18.14.111";
string str = NetConnect(ips);
Console.WriteLine(str);
Console.ReadLine();
}
//通過(guò)ping的方法判斷是否連接;
private static string NetConnect(string ip)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = false;
string pingstr;
p.Start();
p.StandardInput.WriteLine("ping -n 1 " + ip);
p.StandardInput.WriteLine("exit");
string strRst = p.StandardOutput.ReadToEnd();
if (strRst.IndexOf("(0% 丟失)") != -1)
{
pingstr = "連接成功";
//定義socket連接 需要的本機(jī)ip以及相應(yīng)的端口;
msock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
var localIP = new IPEndPoint(IPAddress.Parse("10.18.14.23"), 10001);
msock.Bind(localIP);
//自己定義最大網(wǎng)絡(luò)連接數(shù)
msock.Listen(10);
//新建線程處理;
Thread th = new Thread(delegate()
{
Rec();
});
th.IsBackground = true;
th.Start();
}
else
{
pingstr = "連接超時(shí)";
}
p.Close();
return pingstr;
}
//監(jiān)聽(tīng)是否有鏈接,新開(kāi)線程處理
static void Rec()
{
do
{
Socket s = msock.Accept();
Thread th = new Thread(delegate() {
Parse(s);
});
th.IsBackground = true;
th.Start();
} while (true);
}
//有鏈接時(shí)處理獲取的信息
static void Parse(Socket s)
{
do
{
byte[] b = new byte[1000];
int l = s.Receive(b);
b = b.Take(l).ToArray();
string rs = string.Empty;
for (int i = 0; i < b.Length; i++)
{
rs = rs + b[i].ToString();
}
//解碼
Console.WriteLine(Encoding.ASCII.GetString(b, 0, l));
} while (true);
}
}
}
相關(guān)文章
C#控制臺(tái)程序如何發(fā)布到服務(wù)器Linux上運(yùn)行
這篇文章主要給大家介紹了關(guān)于C#控制臺(tái)程序如何發(fā)布到服務(wù)器Linux上運(yùn)行的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-11-11
C#實(shí)現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù)實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的字符串轉(zhuǎn)MD5碼函數(shù),結(jié)合簡(jiǎn)單實(shí)例形式分析了C#字符串的轉(zhuǎn)換、遍歷、加密等操作技巧,需要的朋友可以參考下2016-07-07
C#實(shí)現(xiàn)泛型List分組輸出元素的方法
這篇文章主要介紹了C#實(shí)現(xiàn)泛型List分組輸出元素的方法,涉及C#針對(duì)List的遍歷、排序、輸出等相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
winfrom 在業(yè)務(wù)層實(shí)現(xiàn)事務(wù)控制的小例子
winfrom 在業(yè)務(wù)層實(shí)現(xiàn)事務(wù)控制的小例子,需要的朋友可以參考一下2013-03-03
基于C#制作一個(gè)飛機(jī)大戰(zhàn)小游戲的全過(guò)程
飛機(jī)大戰(zhàn)小游戲詳細(xì)大家都不陌生,下面這篇文章主要給大家介紹了關(guān)于基于C#制作一個(gè)飛機(jī)大戰(zhàn)小游戲的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
C#?EF?Core可視化工具的使用及EF?Core入門(mén)語(yǔ)句操作代碼
EF?Core?可用作對(duì)象關(guān)系映射程序?(O/RM),以便于?.NET?開(kāi)發(fā)人員能夠使用?.NET?對(duì)象來(lái)處理數(shù)據(jù)庫(kù),這樣就不必經(jīng)常編寫(xiě)大部分?jǐn)?shù)據(jù)訪問(wèn)代碼了,接下來(lái)通過(guò)本文給大家介紹C#?EF?Core可視化工具的使用及EF?Core入門(mén)語(yǔ)句,感興趣的朋友一起看看吧2022-02-02
C#ComboBox控件“設(shè)置 DataSource 屬性后無(wú)法修改項(xiàng)集合”的解決方法
這篇文章主要介紹了C#ComboBox控件“設(shè)置 DataSource 屬性后無(wú)法修改項(xiàng)集合”的解決方法 ,需要的朋友可以參考下2019-04-04

