You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO.Ports;
|
|
|
|
namespace RS232Test
|
|
{
|
|
class Temp
|
|
{
|
|
private const char STX = (char)0x02;
|
|
private const char ETX1 = (char)0x0D;
|
|
private const char ETX2 = (char)0x0A;
|
|
private string inStream = "";
|
|
|
|
private SerialPort serialPort = new SerialPort();
|
|
|
|
private void DataReceived(object sender, EventArgs e)
|
|
{
|
|
string data = serialPort.ReadExisting();
|
|
|
|
//printText.AppendText(data);
|
|
|
|
char[] startCheckArr = data.ToCharArray();
|
|
if (startCheckArr.Length < 1)
|
|
return;
|
|
|
|
if (startCheckArr[0] == STX)
|
|
inStream = data;
|
|
|
|
string[] splited = inStream.Split(STX);
|
|
foreach (string content in splited)
|
|
{
|
|
char[] endCheckArr = content.ToCharArray();
|
|
if (endCheckArr.Length < 1)
|
|
continue;
|
|
|
|
int etxIndex = Array.IndexOf(endCheckArr, ETX1);
|
|
if (etxIndex < 1)
|
|
continue;
|
|
|
|
etxIndex = Array.IndexOf(endCheckArr, ETX2);
|
|
if (etxIndex < 1)
|
|
continue;
|
|
|
|
string message = content.Substring(0, etxIndex);
|
|
//DoWork(message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|