• C# TCP Server服务端多线程监听RFID读卡器客户端上传的读卡数据


    本示例使用设备介绍:液显WIFI无线网络HTTP协议RFID云读卡器可编程实时可控开关TTS语-淘宝网 (taobao.com) 

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Net;
    8. using System.Net.Sockets;
    9. using System.Text;
    10. using System.Threading;
    11. using System.Threading.Tasks;
    12. using System.Windows.Forms;
    13. namespace WindowsFormsApplication1
    14. {
    15. public partial class Form13 : Form
    16. {
    17. public Form13()
    18. {
    19. InitializeComponent();
    20. Control.CheckForIllegalCrossThreadCalls = false;
    21. }
    22. Thread listenThread = null; //负责监听客户端的线程
    23. Socket listenSocket = null; //负责监听客户端的套接字
    24. Socket interactSocket = null;//创建一个负责和客户端通信的套接字
    25. Dictionary<string, Socket> dicSockets = new Dictionary<string, Socket>(); //套接字集合
    26. Dictionary<string, Thread> dicThreads = new Dictionary<string, Thread>(); //线程集合
    27. List<string> list_SealedClients = new List<string>();//被屏蔽用户的名单
    28. private void Form13_Load(object sender, EventArgs e)
    29. {
    30. GetIp();
    31. }
    32. private void GetIp()
    33. {
    34. Form1 f1;
    35. f1 = (Form1)this.Owner;
    36. f1.Refresh();
    37. for (int i = 0; i < f1.comboBox4.Items.Count; i++)
    38. {
    39. comboBox4.Items.Add(f1.comboBox4.Items[i]);
    40. }
    41. if (comboBox4.Items.Count > 0)
    42. {
    43. comboBox4.SelectedIndex = 0;
    44. btn_conn.PerformClick();
    45. }
    46. }
    47. private void btn_conn_Click(object sender, EventArgs e)
    48. {
    49. try
    50. {
    51. if (btn_conn.Text == "开启服务器")
    52. {
    53. //(IPv4协议,双向,TCP协议)
    54. listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    55. //网络结点endpoint需要IP和端口号
    56. //port是 与 address 关联的端口号,或为 0 以指定任何可用端口。port 以主机顺序排列。
    57. //IPEndPoint m_Endpoint = new IPEndPoint(IPAddress.Parse(txb_ip.Text.Trim()), int.Parse(txb_port.Text.Trim()));
    58. string ipadd = comboBox4.Text.Trim();
    59. IPEndPoint m_Endpoint = new IPEndPoint(IPAddress.Parse(ipadd), int.Parse(txb_port.Text.Trim()));
    60. //绑定网络结点
    61. listenSocket.Bind(m_Endpoint);
    62. //将监听Socket置于侦听状态,(backlog):挂起连接队列的最大长度。
    63. listenSocket.Listen(10);
    64. //创建一个线程执行的委托,无返回值
    65. ThreadStart threadFun = new ThreadStart(ListenConnectingRequest);
    66. //创建一个监听线程
    67. listenThread = new Thread(threadFun);
    68. //线程设置为后台运行
    69. listenThread.IsBackground = true;
    70. //启动线程
    71. listenThread.Start();
    72. AddLog("开始监听客户端传来的信息!");
    73. btn_conn.Text = "关闭服务器";
    74. }
    75. else
    76. {
    77. //关闭监听
    78. listenSocket.Close();//关闭监听套接字,并释放
    79. listenThread.Abort();//强行中断
    80. AddLog("结束监听客户端传来的信息!");
    81. AddLog("关闭服务器,断开所有客户端用户的连接!");
    82. //切断所有客户端的连接
    83. for (int i = 0; i < listBox1.Items.Count; i++)
    84. {
    85. string str = listBox1.Items[i].ToString();
    86. Socket m_socket = dicSockets[str];
    87. m_socket.Close();
    88. }
    89. btn_conn.Text = "开启服务器";
    90. }
    91. }
    92. catch (Exception f)
    93. {
    94. MessageBox.Show("错误:" + f.ToString(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
    95. AddLog("服务端启动服务失败!");
    96. }
    97. }
    98. public void AddLog(string str)
    99. {
    100. if (listBox2.Items.Count > 50)
    101. {
    102. listBox2.Items.Clear();
    103. }
    104. listBox2.Items.Add(str);
    105. listBox2.SelectedIndex = listBox2.Items.Count - 1;
    106. }
    107. ///
    108. /// 监听客户端发来的请求
    109. ///
    110. private void ListenConnectingRequest()
    111. {
    112. while (true) //持续不断监听客户端发来的请求
    113. {
    114. try
    115. {
    116. interactSocket = listenSocket.Accept();//阻塞,直到接收到客户端连接请求
    117. // 向列表控件中添加客户端的IP信息;
    118. listBox1.Items.Add(interactSocket.RemoteEndPoint.ToString());
    119. // 将与客户端连接的套接字对象添加到集合中;
    120. dicSockets.Add(interactSocket.RemoteEndPoint.ToString(), interactSocket);
    121. AddLog(DateTime.Now.ToString("hh:mm:ss.fff") + " 客户端" + interactSocket.RemoteEndPoint.ToString() + "连接服务器成功! "); //客户端IP
    122. //创建一个通信线程
    123. Thread threadRecvMsg = new Thread(new ParameterizedThreadStart(ServerRecvMsg));
    124. threadRecvMsg.IsBackground = true;
    125. //启动线程
    126. threadRecvMsg.Start(interactSocket);
    127. dicThreads.Add(interactSocket.RemoteEndPoint.ToString(), threadRecvMsg); // 将新建的线程 添加 到线程的集合中去。
    128. }
    129. catch (Exception)
    130. {
    131. }
    132. }
    133. }
    134. private void ServerRecvMsg(object m_InteractSocket)
    135. {
    136. Socket serverSocket = (Socket)m_InteractSocket; //类型转换 objec->Socket
    137. string str = interactSocket.RemoteEndPoint.ToString();
    138. while (true)
    139. {
    140. //创建一个内存缓冲区 其大小为1024*1024字节 即1M
    141. byte[] arrServerRecMsg = new byte[1024 * 1024];
    142. try
    143. {
    144. //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度
    145. int length = serverSocket.Receive(arrServerRecMsg);
    146. //如果屏蔽名单中有这个用户,屏蔽信息接收
    147. if (!list_SealedClients.Contains(str))
    148. {
    149. //将机器接受到的字节数组转换为人可以读懂的字符串
    150. string strSRecMsg = ""; // Encoding.UTF8.GetString(arrServerRecMsg, 0, length);
    151. for (int i = 0; i < length; i++)
    152. {
    153. strSRecMsg = strSRecMsg + arrServerRecMsg[i].ToString("X2") + " ";
    154. }
    155. if (strSRecMsg.Length != 0)
    156. {
    157. //显示接收的信息
    158. string dispstr = DateTime.Now.ToString("hh:mm:ss.fff") + " FromIP " + interactSocket.RemoteEndPoint.ToString() + " :" + strSRecMsg;
    159. AddLog(dispstr);
    160. //this.BeginInvoke(new Update1(EditUi1), dispstr, arrServerRecMsg, length); //显示接收到的数据包,并根据情况回应设备
    161. AnalyticGetData(arrServerRecMsg, length, str);
    162. }
    163. }
    164. }
    165. catch (Exception)
    166. {
    167. AddLog(DateTime.Now.ToString("hh:mm:ss.fff") + " 客户端" + str + "已断开服务器!");
    168. // 从通信套接字 集合中删除被中断连接的通信套接字;
    169. dicSockets.Remove(str);
    170. // 从通信线程集合中删除被中断连接的通信线程对象;
    171. dicThreads.Remove(str);
    172. // 从列表中移除被中断的连接IP
    173. listBox1.Items.Remove(str);
    174. // 从屏蔽列表从清除
    175. list_SealedClients.Remove(str);
    176. break;//结束该线程
    177. }
    178. }
    179. }
    180. private void ServerSingSenddata(byte[] senddata)
    181. {
    182. string strKey = "";
    183. try
    184. {
    185. strKey = listBox1.Text.Trim();
    186. if (string.IsNullOrEmpty(strKey)) // 判断是不是选择了发送的对象;
    187. {
    188. MessageBox.Show("请选择你要发送的在线客户端!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
    189. }
    190. else
    191. {
    192. if (!list_SealedClients.Contains(strKey))
    193. {
    194. string sendmsg = "";
    195. for (int i = 0; i < senddata.Length; i++)
    196. {
    197. sendmsg = sendmsg + senddata[i].ToString("X2") + " ";
    198. }
    199. dicSockets[strKey].Send(senddata); //显示发送的信息
    200. AddLog(DateTime.Now.ToString("hh:mm:ss.fff") + " SendTo " + strKey + " :" + sendmsg);
    201. AddLog("");
    202. }
    203. else
    204. {
    205. MessageBox.Show(strKey + "已经被屏蔽!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
    206. }
    207. }
    208. }
    209. catch (Exception ex)
    210. {
    211. MessageBox.Show("错误:" + ex.ToString(), "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
    212. AddLog(dicSockets[strKey].RemoteEndPoint.ToString() + "客户端已断开连接,无法发送信息!");
    213. }
    214. }
    215. private void button2_Click(object sender, EventArgs e)
    216. {
    217. listBox2.Items.Clear();
    218. }
    219. private void button3_Click(object sender, EventArgs e)
    220. {
    221. string copystr;
    222. copystr = "";
    223. for (int i = 0; i <= listBox2.Items.Count - 1; i++)
    224. {
    225. copystr = copystr + (string)listBox2.Items[i] + "\r\n";
    226. }
    227. Clipboard.Clear();
    228. Clipboard.SetDataObject(copystr);
    229. MessageBox.Show("显示的数据报文已拷贝到剪切板", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    230. }
    231. private void button4_Click(object sender, EventArgs e)
    232. {
    233. AddLog("接收线程数量:" + dicThreads.Count.ToString());
    234. }
    235. private void button5_Click(object sender, EventArgs e)
    236. {
    237. //1.没有选择退出
    238. if (listBox1.SelectedIndex == -1)
    239. {
    240. MessageBox.Show("请选择要断开的客户端!","警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
    241. return;
    242. }
    243. //2.切断客户端的连接
    244. string RemoteEndPointStr = listBox1.SelectedItem.ToString();
    245. AddLog("强制断开客户端" + RemoteEndPointStr + "的连接!");
    246. listBox1.SelectedIndex = -1;
    247. Socket disconnSocket = dicSockets[RemoteEndPointStr];
    248. disconnSocket.Close();
    249. }
    250. //解析接收到的数据信息,并回应驱动读卡器显示文字、蜂鸣响声、播报语音、开关继电器等
    251. private void AnalyticGetData(byte[] getdata, int length, string IPort)
    252. {
    253. string msginf = "";
    254. uint cardhao;
    255. string cardnumberstr = "";
    256. switch (getdata[0])
    257. {
    258. case 0xc1: //接收到IC读卡器读取的卡号
    259. case 0xcf: //接收到IC离开读卡器
    260. if (getdata[0] == 0xc1)
    261. {
    262. msginf = "数据解析:IC读卡器刷卡数据";
    263. }
    264. else { msginf = "数据解析:IC卡离开读卡器"; }
    265. msginf = msginf + ",IP[" + getdata[1].ToString() + "." + getdata[2].ToString() + "." + getdata[3].ToString() + "." + getdata[4].ToString();
    266. msginf = msginf + ",机号[" + Convert.ToString(getdata[5] + getdata[6] * 256) + "],数据包序号[" + Convert.ToString(getdata[7] + getdata[8] * 256) + "],卡号长度[" + Convert.ToString(getdata[9]) + "],16进制卡号[";
    267. for (int i = 10; i < 10 + getdata[9]; i++)
    268. {
    269. msginf = msginf + getdata[i].ToString("X2");
    270. }
    271. cardhao = (uint)(getdata[13] * 256 * 256 * 256 + getdata[12] * 256 * 256 + getdata[11] * 256 + getdata[10]);
    272. cardnumberstr = "0000000000" + Convert.ToString(cardhao);//卡号
    273. cardnumberstr = cardnumberstr.Substring(cardnumberstr.Length - 10, 10);
    274. msginf = msginf + "],转10进制卡号[" + cardnumberstr;
    275. msginf = msginf + "],唯一设备序号[";
    276. for (int i = 10 + getdata[9]; i < length; i++)
    277. {
    278. msginf = msginf + getdata[i].ToString("X2");
    279. }
    280. msginf = msginf + "]";
    281. AddLog(msginf);
    282. AddLog("");
    283. Response(IPort);
    284. break;
    285. case 0xd1: //接收到ID读卡器读取的卡号
    286. case 0xdf: //接收到ID卡离开读卡器
    287. if (getdata[0] == 0xd1)
    288. {
    289. msginf = "数据解析:ID读卡器刷卡数据";
    290. }
    291. else { msginf = "数据解析:ID卡离开读卡器"; }
    292. msginf = msginf + ",IP[" + getdata[1].ToString() + "." + getdata[2].ToString() + "." + getdata[3].ToString() + "." + getdata[4].ToString();
    293. msginf = msginf + ",机号[" + Convert.ToString(getdata[5] + getdata[6] * 256) + "],数据包序号[" + Convert.ToString(getdata[7] + getdata[8] * 256) + "]";
    294. cardhao = (uint)(getdata[12] * 256 * 256 * 256 + getdata[11] * 256 * 256 + getdata[10] * 256 + getdata[9]);
    295. cardnumberstr = "0000000000" + Convert.ToString(cardhao);//卡号
    296. cardnumberstr = cardnumberstr.Substring(cardnumberstr.Length - 10, 10);
    297. msginf = msginf + ",16进制卡号[" + getdata[9].ToString("X2") + getdata[10].ToString("X2") + getdata[11].ToString("X2") + getdata[12].ToString("X2") + getdata[13].ToString("X2") + "],转10进制卡号[" + cardnumberstr + "]";
    298. msginf = msginf + ",唯一设备序号[";
    299. for (int i = 14; i < length; i++)
    300. {
    301. msginf = msginf + getdata[i].ToString("X2");
    302. }
    303. msginf = msginf + "]";
    304. AddLog(msginf);
    305. AddLog("");
    306. Response(IPort);
    307. break;
    308. case 0xf3: //接收到读卡器心跳包
    309. msginf = "数据解析:读卡器读卡器心跳包,IP[" + getdata[1].ToString() + "." + getdata[2].ToString() + "." + getdata[3].ToString() + "." + getdata[4].ToString();
    310. msginf = msginf + ",机号[" + Convert.ToString(getdata[5] + getdata[6] * 256) + "],数据包序号[" + Convert.ToString(getdata[7] + getdata[8] * 256) + "],心跳包类型[" + Convert.ToString(getdata[9]) + "]";
    311. msginf = msginf + ",长度[" + Convert.ToString(getdata[10]) + "]";
    312. msginf = msginf + ",继电器状态[" + getdata[11].ToString("X2") + "]";
    313. msginf = msginf + ",外部输入状态[" + getdata[12].ToString("X2") + "]";
    314. msginf = msginf + ",随机动态码[" + getdata[13].ToString("X2") + getdata[14].ToString("X2") + getdata[15].ToString("X2") + getdata[16].ToString("X2") + "]";
    315. for (int i = 17; i < length; i++)
    316. {
    317. msginf = msginf + getdata[i].ToString("X2");
    318. }
    319. msginf = msginf + "]";
    320. AddLog(msginf);
    321. AddLog("");
    322. break;
    323. }
    324. }
    325. //回应读卡器驱动显示文字、蜂鸣响声、播报语音等
    326. private void Response(string IPort)
    327. {
    328. byte[] databuf = new byte[280];
    329. int i;
    330. int dispbytes; //显示文字节数
    331. int speakbytes; //语音播报字节表
    332. int sendbytes; //报文数据长度
    333. dispbytes = 34; //双行屏34,四行屏72显示长度,如不显示文字可以为0
    334. string strls = "[v";
    335. if (SYDX.Value <= 16)
    336. {
    337. strls = strls + (SYDX.Value.ToString()).Trim() + "]"; //设置语音大小,在需要发送的语音字符串中任何位置加入[v10],表示将音量调到10级(范围0~16,0表示静音,16最大,每次重开机后,音量重置为10级)!
    338. }
    339. else { strls = strls + "16]"; }
    340. strls = strls + textBox12.Text.Trim();
    341. byte[] SpeakArr = System.Text.Encoding.GetEncoding(936).GetBytes(strls);//TTS语音转换为Ansi
    342. speakbytes = SpeakArr.GetLength(0); //语音长度
    343. sendbytes = 11 + dispbytes + speakbytes + 4; //总计发送数据长度
    344. databuf[0] = 0x5C; //表示驱动蜂鸣器+TTS组合语音+显示文字+继电器
    345. databuf[1] = 0; //机号低位,
    346. databuf[2] = 0; //机号高位,如果高低位都为0表示任意机号,TCP是点对点通讯
    347. if (checkBox2.Checked) //可以同时发出声响
    348. {
    349. databuf[3] = (byte)(comboBox3.SelectedIndex);
    350. if (radioButton2.Checked) //背光灯不变
    351. {
    352. databuf[3] = (byte)(databuf[3] | 0x80);
    353. }
    354. }
    355. else
    356. {
    357. databuf[3] = 0xff;//不发出声响
    358. if (radioButton2.Checked) //背光灯不变
    359. {
    360. databuf[3] = (byte)(databuf[3] & 0x80);
    361. }
    362. }
    363. switch (comboBox2.SelectedIndex) //继电器
    364. {
    365. case 1:
    366. databuf[4] = 0xF1;
    367. break;
    368. case 2:
    369. databuf[4] = 0xF2;
    370. break;
    371. case 3:
    372. databuf[4] = 0xF3;
    373. break;
    374. case 4:
    375. databuf[4] = 0xF4;
    376. break;
    377. case 5:
    378. databuf[4] = 0xF5;
    379. break;
    380. case 6:
    381. databuf[4] = 0xF6;
    382. break;
    383. case 7:
    384. databuf[4] = 0xF7;
    385. break;
    386. case 8:
    387. databuf[4] = 0xF8;
    388. break;
    389. default:
    390. databuf[4] = 0xF0;
    391. break;
    392. }
    393. i = Convert.ToInt32(textBox11.Text); //继电器时长
    394. databuf[5] = (byte)(i % 256);
    395. databuf[6] = (byte)(i / 256);
    396. databuf[7] = (byte)(numericUpDown1.Value);//显示保留时间,单位为秒,为255时表示永久显示
    397. databuf[8] = 0; //在显示屏中的哪个位置开始
    398. databuf[9] = (byte)dispbytes; //显示字符串长度 0-34为全屏
    399. databuf[10] = (byte)speakbytes; //TTS语音长度
    400. strls = textBox12.Text + " ";
    401. byte[] strlsansi = System.Text.Encoding.GetEncoding(936).GetBytes(strls);//显示文字转换为Ansi
    402. for (i = 0; i < speakbytes; i++)
    403. {
    404. databuf[11 + i] = (Byte)strlsansi[i];
    405. }
    406. for (i = 0; i < speakbytes; i++) //连播语音代码
    407. {
    408. databuf[11 + databuf[9] + i] = (Byte)SpeakArr[i];
    409. }
    410. databuf[10 + databuf[9] + speakbytes + 1] = 0x55; //防干扰后缀
    411. databuf[10 + databuf[9] + speakbytes + 2] = 0xAA;
    412. databuf[10 + databuf[9] + speakbytes + 3] = 0x66;
    413. databuf[10 + databuf[9] + speakbytes + 4] = 0x99;
    414. int sendlen = 10 + databuf[9] + speakbytes + 5;
    415. byte[] sendbuf = new byte[sendlen];
    416. string dispstr = DateTime.Now.ToString("hh:mm:ss.fff") + " SendTo " + IPort + " :";
    417. for (i = 0; i < sendlen; i++) //连播语音代码
    418. {
    419. sendbuf[i] = databuf[i];
    420. dispstr = dispstr + databuf[i].ToString("X2") + " ";
    421. }
    422. dicSockets[IPort].Send(sendbuf);
    423. AddLog(dispstr);
    424. AddLog(" ");
    425. }
    426. private void button6_Click(object sender, EventArgs e)
    427. {
    428. byte[] sendbuf = new byte[4];
    429. sendbuf[0] = 0x96; //声响指令
    430. sendbuf[1] = 0; //机号低位,
    431. sendbuf[2] = 0; //机号高位,如果高低位都为0表示任意机号,TCP是点对点通讯
    432. sendbuf[3] = (byte)(comboBox3.SelectedIndex);
    433. ServerSingSenddata(sendbuf);
    434. }
    435. private void button7_Click(object sender, EventArgs e)
    436. {
    437. byte[] sendbuf = new byte[6];
    438. sendbuf[0] = 0x78; //继电器指令
    439. sendbuf[1] = 0; //机号低位,
    440. sendbuf[2] = 0; //机号高位,如果高低位都为0表示任意机号,TCP是点对点通讯
    441. switch (comboBox2.SelectedIndex)
    442. {
    443. case 1:
    444. sendbuf[3] = 0xF1;
    445. break;
    446. case 2:
    447. sendbuf[3] = 0xF2;
    448. break;
    449. case 3:
    450. sendbuf[3] = 0xF3;
    451. break;
    452. case 4:
    453. sendbuf[3] = 0xF4;
    454. break;
    455. case 5:
    456. sendbuf[3] = 0xF5;
    457. break;
    458. case 6:
    459. sendbuf[3] = 0xF6;
    460. break;
    461. case 7:
    462. sendbuf[3] = 0xF7;
    463. break;
    464. case 8:
    465. sendbuf[3] = 0xF8;
    466. break;
    467. default:
    468. sendbuf[3] = 0xF0;
    469. break;
    470. }
    471. int i = Convert.ToInt32(textBox11.Text);//延时
    472. sendbuf[4] = (byte)(i % 256);
    473. sendbuf[5] = (byte)(i / 256);
    474. ServerSingSenddata(sendbuf);
    475. }
    476. private void button8_Click(object sender, EventArgs e)
    477. {
    478. byte[] sendbuf = new byte[6];
    479. sendbuf[0] = 0x78; //继电器指令
    480. sendbuf[1] = 0; //机号低位,
    481. sendbuf[2] = 0; //机号高位,如果高低位都为0表示任意机号,TCP是点对点通讯
    482. switch (comboBox2.SelectedIndex)
    483. {
    484. case 1:
    485. sendbuf[3] = 0xE1;
    486. break;
    487. case 2:
    488. sendbuf[3] = 0xE2;
    489. break;
    490. case 3:
    491. sendbuf[3] = 0xE3;
    492. break;
    493. case 4:
    494. sendbuf[3] = 0xE4;
    495. break;
    496. case 5:
    497. sendbuf[3] = 0xE5;
    498. break;
    499. case 6:
    500. sendbuf[3] = 0xE6;
    501. break;
    502. case 7:
    503. sendbuf[3] = 0xE7;
    504. break;
    505. case 8:
    506. sendbuf[3] = 0xE8;
    507. break;
    508. default:
    509. sendbuf[3] = 0xE0;
    510. break;
    511. }
    512. int i = Convert.ToInt32(textBox11.Text);//延时
    513. sendbuf[4] = (byte)(i % 256);
    514. sendbuf[5] = (byte)(i / 256);
    515. ServerSingSenddata(sendbuf);
    516. }
    517. private void button10_Click(object sender, EventArgs e)
    518. {
    519. string strls;
    520. byte[] sendbuf = new byte[39];
    521. int i;
    522. sendbuf[0] = 0x5a; //显示文字的指令
    523. sendbuf[1] = 0; //机号低位,
    524. sendbuf[2] = 0; //机号高位,如果高低位都为0表示任意机号,TCP是点对点通讯
    525. if (checkBox2.Checked) //可以同时发出声响
    526. {
    527. sendbuf[3] = (byte)(comboBox3.SelectedIndex);
    528. if (radioButton2.Checked) //背光灯不变
    529. {
    530. sendbuf[3] = (byte)(sendbuf[3] | 0x80);
    531. }
    532. }
    533. else
    534. {
    535. sendbuf[3] = 0xff;//不发出声响
    536. if (radioButton2.Checked) //背光灯不变
    537. {
    538. sendbuf[3] = (byte)(sendbuf[3] & 0x80);
    539. }
    540. }
    541. sendbuf[4] = (byte)(numericUpDown1.Value);//显示保留时间,单位为秒,为255时表示永久显示
    542. strls = textBox12.Text + " ";
    543. byte[] strlsansi = System.Text.Encoding.GetEncoding(936).GetBytes(strls);//转换为Ansi //显示文字的ASCII码
    544. for (i = 0; i < 34; i++)
    545. {
    546. sendbuf[5 + i] = (Byte)strlsansi[i];
    547. }
    548. ServerSingSenddata(sendbuf);
    549. }
    550. private void button9_Click(object sender, EventArgs e)
    551. {
    552. byte[] databuf = new byte[280];
    553. int i;
    554. int dispbytes; //显示文字节数
    555. int speakbytes; //语音播报字节表
    556. int sendbytes; //报文数据长度
    557. dispbytes = 34; //双行屏34,四行屏72显示长度,如不显示文字可以为0
    558. string strls = "[v";
    559. if (SYDX.Value <= 16)
    560. {
    561. strls = strls + (SYDX.Value.ToString()).Trim() + "]"; //设置语音大小,在需要发送的语音字符串中任何位置加入[v10],表示将音量调到10级(范围0~16,0表示静音,16最大,每次重开机后,音量重置为10级)!
    562. }
    563. else { strls = strls + "16]"; }
    564. strls = strls + textBox12.Text.Trim();
    565. byte[] SpeakArr = System.Text.Encoding.GetEncoding(936).GetBytes(strls);//TTS语音转换为Ansi
    566. speakbytes = SpeakArr.GetLength(0); //语音长度
    567. sendbytes = 11 + dispbytes + speakbytes + 4; //总计发送数据长度
    568. databuf[0] = 0x5C; //表示驱动蜂鸣器+TTS组合语音+显示文字+继电器
    569. databuf[1] = 0; //机号低位,
    570. databuf[2] = 0; //机号高位,如果高低位都为0表示任意机号,TCP是点对点通讯
    571. if (checkBox2.Checked) //可以同时发出声响
    572. {
    573. databuf[3] = (byte)(comboBox3.SelectedIndex);
    574. if (radioButton2.Checked) //背光灯不变
    575. {
    576. databuf[3] = (byte)(databuf[3] | 0x80);
    577. }
    578. }
    579. else
    580. {
    581. databuf[3] = 0xff;//不发出声响
    582. if (radioButton2.Checked) //背光灯不变
    583. {
    584. databuf[3] = (byte)(databuf[3] & 0x80);
    585. }
    586. }
    587. switch (comboBox2.SelectedIndex) //继电器
    588. {
    589. case 1:
    590. databuf[4] = 0xF1;
    591. break;
    592. case 2:
    593. databuf[4] = 0xF2;
    594. break;
    595. case 3:
    596. databuf[4] = 0xF3;
    597. break;
    598. case 4:
    599. databuf[4] = 0xF4;
    600. break;
    601. case 5:
    602. databuf[4] = 0xF5;
    603. break;
    604. case 6:
    605. databuf[4] = 0xF6;
    606. break;
    607. case 7:
    608. databuf[4] = 0xF7;
    609. break;
    610. case 8:
    611. databuf[4] = 0xF8;
    612. break;
    613. default:
    614. databuf[4] = 0xF0;
    615. break;
    616. }
    617. i = Convert.ToInt32(textBox11.Text); //继电器时长
    618. databuf[5] = (byte)(i % 256);
    619. databuf[6] = (byte)(i / 256);
    620. databuf[7] = (byte)(numericUpDown1.Value);//显示保留时间,单位为秒,为255时表示永久显示
    621. databuf[8] = 0; //在显示屏中的哪个位置开始
    622. databuf[9] = (byte)dispbytes; //显示字符串长度 0-34为全屏
    623. databuf[10] = (byte)speakbytes; //TTS语音长度
    624. strls = textBox12.Text + " ";
    625. byte[] strlsansi = System.Text.Encoding.GetEncoding(936).GetBytes(strls);//显示文字转换为Ansi
    626. for (i = 0; i < speakbytes; i++)
    627. {
    628. databuf[11 + i] = (Byte)strlsansi[i];
    629. }
    630. for (i = 0; i < speakbytes; i++) //连播语音代码
    631. {
    632. databuf[11 + databuf[9] + i] = (Byte)SpeakArr[i];
    633. }
    634. databuf[10 + databuf[9] + speakbytes + 1] = 0x55; //防干扰后缀
    635. databuf[10 + databuf[9] + speakbytes + 2] = 0xAA;
    636. databuf[10 + databuf[9] + speakbytes + 3] = 0x66;
    637. databuf[10 + databuf[9] + speakbytes + 4] = 0x99;
    638. int sendlen = 10 + databuf[9] + speakbytes + 5;
    639. byte[] sendbuf = new byte[sendlen];
    640. for (i = 0; i < sendlen; i++) //连播语音代码
    641. {
    642. sendbuf[i] = databuf[i];
    643. }
    644. ServerSingSenddata(sendbuf);
    645. }
    646. private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
    647. {
    648. try
    649. {
    650. listenSocket.Close();//关闭监听套接字,并释放
    651. listenThread.Abort();//强行中断
    652. AddLog("结束监听客户端传来的信息!");
    653. AddLog("关闭服务器,断开所有客户端用户的连接!");
    654. //切断所有客户端的连接
    655. for (int i = 0; i < listBox1.Items.Count; i++)
    656. {
    657. string str = listBox1.Items[i].ToString();
    658. Socket m_socket = dicSockets[str];
    659. m_socket.Close();
    660. }
    661. btn_conn.Text = "开启服务器";
    662. }
    663. catch (Exception f)
    664. {
    665. btn_conn.Text = "开启服务器";
    666. }
    667. }
    668. private void Form13_FormClosed(object sender, FormClosedEventArgs e)
    669. {
    670. try
    671. {
    672. listenSocket.Close();//关闭监听套接字,并释放
    673. listenThread.Abort();//强行中断
    674. AddLog("结束监听客户端传来的信息!");
    675. AddLog("关闭服务器,断开所有客户端用户的连接!");
    676. //切断所有客户端的连接
    677. for (int i = 0; i < listBox1.Items.Count; i++)
    678. {
    679. string str = listBox1.Items[i].ToString();
    680. Socket m_socket = dicSockets[str];
    681. m_socket.Close();
    682. }
    683. btn_conn.Text = "开启服务器";
    684. }
    685. catch (Exception f)
    686. {
    687. btn_conn.Text = "开启服务器";
    688. }
    689. }
    690. }
    691. }

  • 相关阅读:
    C++学习笔记(Ⅳ):C++提高编程
    编写基础程序:Hello World
    react: zustand数据缓存
    单链表反转
    中国传统美食网页HTML代码 学生网页课程设计期末作业下载 美食大学生网页设计制作成品下载 DW餐饮美食网页作业代码下载
    【LeetCode】 前K个高频单词 两种解法
    C++高精度乘法(高精度乘低精度)
    echarts:graph图表拖拽节点
    AndroidStudio 导入项目模块失败
    管张汇泓:一份邮件把我带到了开源世界|OneFlow U
  • 原文地址:https://blog.csdn.net/zhangjin7422/article/details/134267010