net2.0下实现串口接收GPS数据读写(-)

原创 2006年06月08日 15:00:00
最近写了个简单的串口接收GPS数据的程序,下面主程序中的代码,主要利用BackGroundWork组件来实现数据的接收。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Collections;

namespace GPSSerialPortData
...
{
    
public partial class MainFrom : Form
    ...
{
        
private System.IO.Ports.SerialPort[] ports = null;
        
private bool IsShowData = false;
        StringBuilder f_SB 
= new StringBuilder();    

        
public MainFrom()
        ...
{            
            InitializeComponent(); 
        }


        
private string newline()
        ...
{
           
return " ";
        }


        
/**//// <summary>
        
/// 打开串口并开始数据接收
        
/// </summary>

        private void OpenPort()
        ...
{
            
for (int i = 0; i < SerialPortConfig.Ports.Count; i++)
            ...
{
                
try
                ...
{
                    ports[i].Open();
                    ports[i].DataReceived 
+= new System.IO.Ports.SerialDataReceivedEventHandler(SPort_DataReceived);
                    tbPortState.Text 
+= "串口" + ports[i].PortName + "已经打开" + newline();
                }

                
catch
                ...
{
                    tbPortState.Text 
+= "串口" + ports[i].PortName + "不存在或已占用" + newline();
                }

            }

        }


        
/**//// <summary>
        
/// 停止数据接收并关闭串口
        
/// </summary>

        private void ClosePort()
        ...
{
            
for (int i = 0; i < SerialPortConfig.Ports.Count; i++)
            ...
{
                
try
                ...
{
                    
if (ports[i].IsOpen)
                    ...
{
                        ports[i].DataReceived 
-= new System.IO.Ports.SerialDataReceivedEventHandler(SPort_DataReceived);
                        ports[i].Close();                        
                    }

                    tbPortState.Text 
+= "串口" + ports[i].PortName + "已经关闭" + newline();
                }

                
catch
                ...
{
                    tbPortState.Text 
+= "串口" + ports[i].PortName + "不存在或已占用" + newline();
                }

            }

        }

       
          
        
private void AddReceiveData(byte[] bytes)
        ...
{
            
lock (f_SB)
            ...
{
                
foreach (byte b in bytes)
                ...
{
                    
if (b < 16)
                    ...
{
                        f_SB.Append( 
"0"+Convert.ToString(b, 16).ToUpper());
                    }

                    
else
                    ...
{
                        f_SB.Append(Convert.ToString(b, 
16).ToUpper());
                    }

                }

            }

        }

        BackgroundWorker f_BG 
= new BackgroundWorker();
        
private void bgWorkerAnalyze_DoWork(object sender, DoWorkEventArgs e)
        ...
{
            
while (true)
            ...
{
                
if (f_SB.Length < 58)
                ...
{
                    System.Threading.Thread.Sleep(
200);
                }

                
else
                ...
{
                    
lock (f_SB)
                    ...
{
                        
while (f_SB.Length>=58)
                        ...
{
                            
if (f_SB.ToString().Substring(0,4).ToUpper() == "ACAC")
                            ...
{
                                SaveGPSData(f_SB.ToString().Substring(
058));
                                f_SB.Remove(
058);
                            }

                            
else
                            ...
{
                                f_SB.Remove(
04);
                            }

                        }
                  
                    }

                }

            }

        }

        
void SPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        ...
{
            System.IO.Ports.SerialPort serialPort 
= (System.IO.Ports.SerialPort)sender;
            
            
//此处 在测试时不能够确定出发的次数 故做了长度判断当长度为0时 不予考虑
            int len = serialPort.BytesToRead;
            
byte[] bs = new byte[len];
            serialPort.Read(bs, 
0, len);
            
if (len == 0)
            ...
{
                
return;
            }

            
else
            ...
{
                BackgroundWorker bgWorker 
= new BackgroundWorker();
                bgWorker.DoWork 
+= new DoWorkEventHandler(bgWorker_DoWork);
                bgWorker.RunWorkerAsync(bs);
                
return;
            }

        }


        
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        ...
{
            
byte[] bs = (byte[])e.Argument;
            AddReceiveData(bs);
        }


        
delegate void ShowGPSDataCallback(string text);

        
private void ShowGPSData(string message)
        ...
{
            
if (tbView.InvokeRequired == true)
            ...
{
                ShowGPSDataCallback callback 
= new ShowGPSDataCallback(ShowGPSData);
                tbView.Invoke(callback, message);
            }

            
else
            ...
{
                tbView.Text 
+= message;
            }

        }


        
private void SaveGPSData(string data)
        ...
{
            GPSData gpsData 
= new GPSData();

            设置数据
设置数据

            
switch (data.Substring(182))
            ...
{
                
case "89":
                    
if (IsShowData)
                    ...
{
                        ShowGPSData(
                            
"终端" + gpsData.Terminal_Id +
                            
" 北纬" + gpsData.Latitude +
                            
" 东经" + gpsData.Longitude+
                            
" 海拔" + gpsData.Height.ToString() +
                            
" 时间" + gpsData.AddTime.ToString("hh:mm:ss" + newline())
                            );
                    }

                    
break;
                
case "4B":
                    ShowGPSData(gpsData.Terminal_Id 
+ "时间设置成功" + newline());
                    
return;
                
case "50":
                    ShowGPSData(gpsData.Terminal_Id 
+ "已停止回送数据" + newline());
                    
return;
                
default:
                    
return;
            }


            SaveData(gpsData);
        }



        
/**//// <summary>
        
/// 保存接收到的GPS数据
        
/// </summary>
        
/// <param name="bs"></param>

        private void SaveGPSData(byte[] bs)
        ...
{
            GPSData gpsData 
= new GPSData();
           
            设置数据
设置数据

            
switch (Convert.ToString(bs[9], 16).ToUpper())
            ...
{
                
case "89":
                    
if (IsShowData)
                    ...
{
                        ShowGPSData(
                            
"终端" + Convert.ToInt32(Convert.ToString(bs[5], 16+ Convert.ToString(bs[6], 16+ Convert.ToString(bs[7], 16+ Convert.ToString(bs[8], 16)).ToString() +
                            
" 北纬" + gpsData.Latitude +
                            
" 东经" + gpsData.Longitude +
                            
" 海拔" + gpsData.Height.ToString() +
                            
" 时间" + gpsData.AddTime.ToString("hh:mm:ss" + newline())
                            );
                    }

                    
break;
                
case "4B":
                    ShowGPSData(Convert.ToInt32(
                 Convert.ToString(bs[
5], 16+ Convert.ToString(bs[6], 16+
                 Convert.ToString(bs[
7], 16+ Convert.ToString(bs[8], 16)).ToString() + "时间设置成功" + newline());
                    
return;
                
case "50":
                    ShowGPSData(Convert.ToInt32(
                 Convert.ToString(bs[
5], 16+ Convert.ToString(bs[6], 16+
                 Convert.ToString(bs[
7], 16+ Convert.ToString(bs[8], 16)).ToString() + "已停止回送数据" + newline());
                    
return;
                
default:
                    
return;
            }


            SaveData(gpsData);
        }


        
private void SaveData(GPSData data)
        ...
{
            Database db 
= DatabaseFactory.CreateDatabase();
            StringBuilder sb 
= new StringBuilder();
            sb.Append(
"INSERT INTO [soft_sailingservice].[dbo].[NS_GPS_Data]");
            sb.Append(
"([TerminalNumber],[Latitude],[OriginalLatitude],[Longitude],[OriginalLongitude],[Height],[AddTime])");
            sb.Append(
"VALUES (");
            sb.Append(data.Terminal_Id 
+ ",'");
            sb.Append(data.Latitude 
+ "',");
            sb.Append(data.OriginalLatitude 
+ ",'");
            sb.Append(data.Longitude 
+ "',");
            sb.Append(data.OriginalLongitude 
+ ",");
            sb.Append(data.Height 
+ ",'");
            sb.Append(data.AddTime 
+ "')");
            db.ExecuteNonQuery(CommandType.Text, sb.ToString());
        }


        按钮事件处理
按钮事件处理
    }

}

 

源码下载路径:http://download.csdn.net/source/161373

<script type="text/javascript"><!-- google_ad_client = "pub-8490214103805077"; google_ad_width = 468; google_ad_height = 60; google_ad_format = "468x60_as"; google_ad_type = "text_image"; google_ad_channel = ""; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

ARM通过usb转串口实现接收GPS数据

前期工作是:搭建交叉编译链,网络文件系统 内核版本:linux-3.4.2 我使用的linux内核版本是linux-3.4.2,其他内核版本也可以实现。 我的usb转串口线的芯片是pl2303,使用...

Java实现串口数据读写

最近的一个项目中需要将扫码枪读取到的数据取出,但是因为扫码枪是用串口传输数据的,比较少见,所以记录一下,当给自己做一个小结,有需要的同学也能参考一下。public class receiveData ...

全面解析ASP.NET2.0下的URL重写

本文将通过实例比较ASP.NET下的三种典型URL重写方案——ISAPI重写(使用开源组件IIRF),ASP.NET2.0内置的urlMappings和基于自定义HTTPModule的URL重写(使用...

串口数据读写.vi

  • 2014-04-26 10:05
  • 16KB
  • 下载

MyComm,读写串口数据

  • 2002-12-13 00:00
  • 166KB
  • 下载

用Java语言向串口读写数据的方法

应用软件开发中java 是重要的一个方面,下面介绍用Java语言向串口读写数据的方法: Java对串口通信的支持 常见的Java串口包        SUN的CommAPI分别提供了对常...

GPS串口数据接收源码

  • 2015-02-26 16:12
  • 293KB
  • 下载

读写串口的实现(一)

* Windows自动识别串口的实现,以下是基于MFC开发的,以下所说都是建立在串口注册表上说的 *实现Windows系统下自动识别串口需要调用三个Windows API函数,它们是: //主要用于打...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)