C#에서 GPS를 사용하여 직렬 포트를 통해 GPS 데이터를 가져오고 싶습니다. ParseNMEA 클래스를 만들어 NMEA 데이터를 가져 와서 구문 분석하고 $ GPGAA를 얻습니다. 이 클래스입니다.C#에서 직렬 포트를 통해 GPS 데이터를 읽는 방법?
public class ParseNMEA
{
private SerialPort _port;
private byte[] _buffer;
public string GetGpgga(string portname, int baudrate, Parity parity, int databits, StopBits stopbits)
{
//Set serial-port
_port = new SerialPort();
_port.PortName = portname;
_port.BaudRate = baudrate;
_port.Parity = parity;
_port.DataBits = databits;
_port.StopBits = stopbits;
_port.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);
_port.Open();
//Parse buffer
string sdata = "";
Encoding encoding = ASCIIEncoding.GetEncoding(1252);
if (null != _buffer)
{
sdata = encoding.GetString(_buffer);
}
string[] string_array = sdata.Split('$');
string Gpgga = null;
for (int i = 0; i < string_array.Length; i++)
{
string stringTemp = string_array[i];
string[] line_array = stringTemp.Split(',');
if (line_array[0] == "GPGGA")
{
Gpgga = string.Join(",", string_array[i]);
}
}
return Gpgga;
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort port = (SerialPort)sender;
_buffer = new byte[port.BytesToRead];
port.Read(_buffer, 0, _buffer.Length);
}
}
Form에서이 클래스를 호출하고 싶습니다. 좋아요 :
private ParseNMEA _parse;
_parse = new ParseNMEA();
private void button_start_click(object sender, EventArgs e)
{
string gpgga = _parse.GetGpgga(comport, baudrate, parity, databits, stopbits);
textBox1.Text = gpgga;
}
하지만 이것은 작동하지 않습니다. SerialPort_DataReceived 이벤트에는 문제가 있다고 생각합니다. 너는 어떤 생각이 든다. 도와주세요.
감사합니다.
감사합니다. 나는 데이터를 바이트로 얻는다. – user3021107