Set, read, and analyze um220 module data from the serial port

Source: Internet
Author: User

I haven't written a blog for a long time. Let's start a new journey from a small program;

The latest project mainly uses the Linux system. This article mainly introduces how to set and read the BD + GPS module (um220), which is actually mainly for serial port (UART) operations.

/* * gps.c * * um220 test * * Author: Wang Zhengkai <[email protected]> * */#include <stdio.h>#include <termios.h>#include <strings.h>#include <string.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>
/* I used a Ubuntu computer for testing, the serial port used is ttys0 */# define dev_node "/dev/ttys0" # define runtime 1024/* initialize serial port options */static void settermios (struct termios * pnewtio, int ubaudrate) {bzero (pnewtio, sizeof (struct termios);/* clear struct for new port settings * // 8n1pnewtio-> c_cflag = ubaudrate | cs8 | cread | clocal; pnewtio-> c_iflag = ignpar; pnewtio-> c_oflag = 0; pnewtio-> c_lflag = 0; // non Icanon}/* set the baud rate of the um220 serial port to 9600 and refresh to make it take effect immediately */void um220_uart_init (INT ttyfd, struct termios * oldtio, struct termios * newtio) {tcgetattr, oldtio);/* save current serial port settings */settermios (newtio, b9600); tcflush (ttyfd, tciflush); tcsetattr (ttyfd, tcsanow, newtio );} /* analysis data of um220 */void parsedata (char * BUF) {int NQ, NN, Nb, NC; char CX, Cy, cm1, cm2; float ftime, FX, FY, FP, FH, FB, FD; If (Buf = NULL) {printf ("error: Can't Get Buf! \ N "); return;} sscanf (BUF," $ gngga, % F, % F, % C, % F, % C, % d, % 02d, % F, % F, % C, % F, % C, % F, % 04d % 02x ", & ftime, & FX, & CX, & fy, & cy, & NQ, & NN, & FP, & FH, & cm1, & FB, & cm2, & FD, & nb, & NC); printf ("X: % C % F, Y: % C % F, H % F, satellite: % d \ n ", CX, FX, Cy, fy, FH, NN );
/* CX: N or S; FX: latitude; Cy: E or W; FY: longitude; FH: height; NN: number of satellites */} int main (void) {int Nb, command; int um220_fd =-1; char newbuf [max_packet_size]; char MSG [20], * ret = NULL; struct termios oldtio, newtio; /* Open um220 module */If (um220_fd = open (dev_node, o_rdwr) <0) {printf ("error: Can't open serial port % s! \ N ", dev_node); Return-1;}/* init UART for um220 */um220_uart_init (um220_fd, & oldtio, & newtio ); /* Set um220 options */printf ("Please select modules of um220 \ n"); printf ("1.bd module \ n"); printf ("2.gps module \ n "); printf ("3.bd+ GPS module \ n"); If (scanf ("% d", & command )! = 1) {printf ("error: input is wrong! \ N ");} switch (command) {Case 1: memset (MSG, 0, sizeof (MSG); strcpy (MSG," $ inclusys, h01 "); if (write (um220_fd, MSG, sizeof (MSG) <0) printf ("failed to set BD modules! \ N "); break; Case 2: memset (MSG, 0, sizeof (MSG); strcpy (MSG," $ inclusys, H10 "); If (write (um220_fd, MSG, sizeof (MSG) <0) printf ("failed to set GPS modules! \ N "); break; Case 3: memset (MSG, 0, sizeof (MSG); strcpy (MSG," $ inclusys, H11 "); If (write (um220_fd, MSG, sizeof (MSG) <0) printf ("failed to set BD + GPS modules! \ N "); break; default: printf (" can't identify command, set BD + GPS modules! \ N "); memset (MSG, 0, sizeof (MSG); strcpy (MSG," $ inclusys, H11 "); If (write (um220_fd, MSG, sizeof (MSG) <0) printf ("failed to set BD + GPS modules! \ N ") ;}for (;) {/* read data from um220 */memset (newbuf, 0, 1024); NB = read (um220_fd, newbuf, max_packet_size ); if (NB =-1) {perror ("read UART error"); Return-1;} If (ret = strstr (newbuf, "$ gngga "))! = NULL) {/* analysis data */parsedata (RET);} Sleep (1);}/* recover settings of serial port */tcsetattr (um220_fd, tcsanow, & oldtio);/* close um220_fd */close (um220_fd );
return 0;}


Set, read, and analyze um220 module data from the serial port

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.