Set, read, and analyze um220 module data from the serial port, um220 Module
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 <449811900@qq.com> * */#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;}
"Reading single-chip microcomputer data from the serial port is displayed on the PC, analyzing and reading the data to control the single-chip microcomputer"
Two aspects of PC microcontroller programming
Recommended reading
Visual C ++ _ Turbo C serial communication programming practices
Visual_Basic and _ RS-232 _ Serial Communication Control
Delphi Serial Communication Engineering Development Instance navigation
Of course, there are also MCU books
Serial assistant, only reading single-chip microcomputer port data, code
Single-chip serial port, you should know-I do not know it should be able to search, too many online!
The PC serial port is connected to the USB to TTL serial port module. The RX module is connected to the P3 ^ 1 (11 feet) of the single chip microcomputer, And the TX module is connected to the P3 ^ 0 (10 feet) of the single chip microcomputer)
Serial assistant, XP Super Terminal, and telnet can be displayed on the PC. For Development, you need to use components or WIN32 APIs to write code and read the programming language.