Django
New member
- Joined
- Nov 24, 2019
- Messages
- 1
- Programming Experience
- 1-3
I use VS2017 , .net 4.7 in windows 7. I have written a C# program to polling data from a serial port. But I could not get the data input from the serial port some time .
The serial port receive" start "from outside and return " INITOK".
The problem is sometimes after run for some time , after sending " start " to this program, the " INITOK", could not be returned. Seems like sometimes polling the serial port could not work well for my program.
C#:
using System;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Net.Sockets;
using System.Web;
using System.IO.Ports;
using System.Text.RegularExpressions;
namespace Socket_server_gangprogrammer
{
public partial class Form1 : Form
{
public Socket clientSocket = null;
public Socket clientSocket_2 = null;
public Socket clientSocket_3 = null;
public Socket clientSocket_4 = null;
public static Socket myClientSocket = null;
public static Socket myClientSocket_2 = null;
public static Socket myClientSocket_3 = null;
public static Socket myClientSocket_4 = null;
static Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static Socket socket_2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static Socket socket_3 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
static Socket socket_4 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
private static byte[] result = new byte[1024];
private static byte[] result_2 = new byte[1024];
private static byte[] result_3 = new byte[1024];
private static byte[] result_4 = new byte[1024];
string result_str = null;
string result_str_2 = null;
string result_str_3 = null;
string result_str_4 = null;
string[] buf_str = new String[20];
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
comboBox4.Items.Add("9600");
textBox8.AppendText("8");
textBox10.AppendText("None");
textBox12.AppendText("one");
try
{
//添加串口项目
foreach (string s in SerialPort.GetPortNames())
{//获取有多少个COM口
comboBox3.Items.Add(s);
}
//串口设置默认选择项
comboBox3.SelectedIndex = 1;
comboBox4.SelectedIndex = 0;
}
catch (Exception ex)
{
textBox2.AppendText(ex.ToString());
}
}
public void SocketServie()
{
//服务端已启动
string host = "127.0.0.1";//IP地址
int port = 8021;//端口
int port_2 = 8022;
int port_3 = 8023;
int port_4 = 8024;
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); //port reuse
socket_2.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket_3.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket_4.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket.Bind(new IPEndPoint(IPAddress.Parse(host), port));
socket_2.Bind(new IPEndPoint(IPAddress.Parse(host), port_2));
socket_3.Bind(new IPEndPoint(IPAddress.Parse(host), port_3));
socket_4.Bind(new IPEndPoint(IPAddress.Parse(host), port_4));
socket.Listen(100);//设定最多100个排队连接请求
socket_2.Listen(100);
socket_3.Listen(100);
socket_4.Listen(100);
Thread SocketThread = new Thread(this.ListenClientConnect);//通过多线程监听客户端连接
SocketThread.IsBackground = true;
SocketThread.Start();
}
private void ListenClient()
{
try
{
DateTime now = DateTime.Now;
//int nb = serialPort1.BytesToRead;
int nb = 0;
while (true)
{
nb = serialPort1.BytesToRead;
if (nb > 0)
{
byte[] buffer = new byte[nb];
serialPort1.Read(buffer, 0, nb);
if (Encoding.UTF8.GetString(buffer).Contains("START"))
{
textBox2.AppendText(now.ToString("F") + Encoding.UTF8.GetString(buffer) + "\r\n");
serialPort1.WriteLine("<<INITIALOK>>\r");
}
if (System.Text.Encoding.UTF8.GetString(buffer).Contains("NEXT"))
{
textBox2.AppendText(now.ToString("F") + Encoding.UTF8.GetString(buffer) + "\r\n");
string sendMessage = "start burning " + DateTime.Now;
try
{
clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
try
{
clientSocket_2.Send(Encoding.ASCII.GetBytes(sendMessage));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
try
{
clientSocket_3.Send(Encoding.ASCII.GetBytes(sendMessage));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
try
{
clientSocket_4.Send(Encoding.ASCII.GetBytes(sendMessage));
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
/*string sendMessage_2 = "waiting";
for (int i = 0; i < 15; i++)
{
clientSocket.Send(Encoding.ASCII.GetBytes(sendMessage_2));
clientSocket_2.Send(Encoding.ASCII.GetBytes(sendMessage_2));
clientSocket_3.Send(Encoding.ASCII.GetBytes(sendMessage_2));
clientSocket_4.Send(Encoding.ASCII.GetBytes(sendMessage_2));
//Thread.Sleep(1000);
}*/
}
}
}
}
catch (Exception ex)
{
textBox2.AppendText(ex.ToString());
}
}
/// <summary>
/// 监听客户端连接
/// </summary>
private void ListenClientConnect()
{
while (true)
{
this.clientSocket = socket.Accept();
this.clientSocket_2 = socket_2.Accept();
this.clientSocket_3 = socket_3.Accept();
this.clientSocket_4 = socket_4.Accept();
textBox2.AppendText("socket client accepted!\r\n");
Thread receiveThread = new Thread(new ParameterizedThreadStart(ReceiveMessage));
receiveThread.IsBackground = true;
receiveThread.Start(this);
Thread.Sleep(1000);
}
}
/// <summary>
/// 接收消息
/// </summary>
/// <param name="clientSocket"></param>
//public void ReceiveMessage(object clientSocket, object Form1)
public void ReceiveMessage(object Form1)
{
Socket myClientSocket = (Socket)clientSocket;
Socket myClientSocket_2 = (Socket)clientSocket_2;
Socket myClientSocket_3 = (Socket)clientSocket_3;
Socket myClientSocket_4 = (Socket)clientSocket_4;
while (true)
{
try
{
if (result_str == null)
{
int receiveNumber = myClientSocket.Receive(result); //通过clientSocket接收数据
//Console.WriteLine(receiveNumber);
result_str = Encoding.UTF8.GetString(result, 0, receiveNumber);
//Console.WriteLine(result_str);
//textBox2.AppendText("result_str: " + result_str + " ");
}
else
{
textBox2.AppendText("***result_str: " + result_str + " ");
}
if (result_str_2 == null)
{
int receiveNumber_2 = myClientSocket_2.Receive(result_2);
//Console.WriteLine(receiveNumber_2);
result_str_2 = Encoding.UTF8.GetString(result_2, 0, receiveNumber_2);
//Console.WriteLine(result_str_2);
//textBox2.AppendText("result_str_2: " + result_str_2 + " ");
}
else
{
textBox2.AppendText("***result_str_2: " + result_str_2 + " ");
}
if (result_str_3 == null)
{
int receiveNumber_3 = myClientSocket_3.Receive(result_3);
// Console.WriteLine(receiveNumber_3);
result_str_3 = Encoding.UTF8.GetString(result_3, 0, receiveNumber_3);
//Console.WriteLine(result_str_3);
//textBox2.AppendText("result_str_3: " + result_str_3 + " ");
}
else
{
textBox2.AppendText("***result_str_3: " + result_str_3 + " ");
}
if (result_str_4 == null)
{
int receiveNumber_4 = myClientSocket_4.Receive(result_4);
//Console.WriteLine(receiveNumber_4);
result_str_4 = Encoding.UTF8.GetString(result_4, 0, receiveNumber_4);
//Console.WriteLine(result_str_4);
//textBox2.AppendText("result_str_4: " + result_str_4 + " ");
}
else
{
textBox2.AppendText("***result_str_4: " + result_str_4 + " ");
}
if (result_str != null && result_str_2 != null && result_str_3 != null && result_str_4 != null)
{
serialPort1.WriteLine("<<" + result_str[0] + result_str[2] + result_str[4] + result_str[6] + result_str[8] + result_str[10] + result_str[12] + result_str[14] + result_str_2[0] + result_str_2[2] + result_str_2[4] + result_str_2[6] + result_str_2[8] + result_str_2[10] + result_str_2[12] + result_str_2[14] + result_str_3[0] + result_str_3[2] + result_str_3[4] + result_str_3[6] + result_str_3[8] + result_str_3[10] + result_str_3[12] + result_str_3[14] + result_str_4[0] + result_str_4[2] + result_str_4[4] + result_str_4[6] + result_str_4[8] + result_str_4[10] + ">>" + "\r");
//Console.WriteLine("<<" + result_str[0] + result_str[2] + result_str[4] + result_str[6] + result_str[8] + result_str[10] + result_str[12] + result_str[14] + result_str_2[0] + result_str_2[2] + result_str_2[4] + result_str_2[6] + result_str_2[8] + result_str_2[10] + result_str_2[12] + result_str_2[14] + result_str_3[0] + result_str_3[2] + result_str_3[4] + result_str_3[6] + result_str_3[8] + result_str_3[10] + result_str_3[12] + result_str_3[14] + result_str_4[0] + result_str_4[2] + result_str_4[4] + result_str_4[6] + result_str_4[8] + result_str_4[10] + ">>" + "\r\n");
result_str = null;
result_str_2 = null;
result_str_3 = null;
result_str_4 = null;
}
}
catch (Exception ex)
{
textBox2.AppendText("error!!!" + ex + "\r\n");
//myClientSocket.Shutdown(SocketShutdown.Both);//禁止发送和上传
//myClientSocket.Close();//关闭Socket并释放资源
break;
}
}
}
private void button7_Click(object sender, EventArgs e)
{
try
{
string text = this.button7.Text;
this.button7.Text = text == "connected" ? "disconnected" : "connected";
if (serialPort1.IsOpen)
{
serialPort1.Close();
serialPort1 = null;
}
else
{
serialPort1.BaudRate = 9600;
serialPort1.PortName = comboBox3.Text;
serialPort1.DataBits = 8;
try
{
if (!serialPort1.IsOpen)
{
serialPort1.Open();//打开串口
}
}
catch (Exception e1)
{
textBox2.AppendText(e1.Message);
}
}
}
catch (Exception e1)
{
textBox2.AppendText(e1.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string text = this.button1.Text;
this.button1.Text = text == "started" ? "stop" : "started";
textBox2.AppendText("--------started !!!--------\r\n");
//Start Socket
SocketServie();
Thread WorkThread = new Thread(this.ListenClient);
WorkThread.IsBackground = true;
WorkThread.Start();
}
catch (Exception e2)
{
textBox2.AppendText(e2.Message);
}
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.Close();
serialPort1 = null;
}
}
}
}
The problem is sometimes after run for some time , after sending " start " to this program, the " INITOK", could not be returned. Seems like sometimes polling the serial port could not work well for my program.
Last edited by a moderator: