Serial communication between two computers and reading and display of dot matrix font

catalogue

1, Serial communication between two computers  

  1.1 serial communication

1.2 subject requirements

1.3 experimental communication

2, Reading and display of dot matrix font

2.1 Chinese character dot matrix principle

2.1.1 digital code (location code)

2.1.2 digital code (internal code)

2.1.3 lattice font structure

2.1.4 acquisition of Chinese character dot matrix

2.2 using C/C + + to call opencv to display pictures and text under Ubuntu

3, Summary

4, References

1, Serial communication between two computers  

  1.1 serial communication

        The two most important aspects of communication: information representation, analysis method + information transmission method. Both communication parties need to agree on the expression and analysis methods of information in advance to achieve consistency, otherwise the information cannot be transmitted effectively. The transmission method of communication refers to the process in which the encoded communication information is transmitted on the transmission medium.

        Serial communication includes asynchronous, serial and differential.

            1) Asynchronous

                  There is no unified clock signal between the sender and receiver of serial communication.

            2) Difference

                  Serial communication appeared earlier, the rate was lower, the transmission distance was closer, and all the interference was less obvious. Therefore, level signal transmission was used at that time. The later transmission protocols have been changed to differential signal transmission.

            3) Serial communication

                  Serial communication can only transmit 1 binary bit at a time.

1.2 subject requirements

        Two laptops, with   usb to rs232   Module and DuPont line to establish serial port connection. Then use tools and software such as serial port assistant (with file transfer function) to transfer a large file (picture, video and compressed package software) on one laptop to another computer, budget the relationship between file size, baud rate and transmission time, and compare the actual transmission time.

1.3 experimental communication

Prepare two computers and two usb to serial ports and DuPont cables, and then connect:

3V3-3V3

GND-GND

TXD-RXD

RXD-TXD
After the computer and DuPont cable are connected, open the serial port debugging assistant, set the baud rate to 115200, click to open the serial port, then click to open the file, select the file you want to send, and click to send the file. After sending:

Receiver display:  

Then select the opening method to open the file:

2, Reading and display of dot matrix font

2.1 Chinese character dot matrix principle

2.1.1 digital code (location code)

        It is stipulated in the national standard GD2312-80 that all national standard Chinese characters and symbols are allocated in a square matrix with 94 rows and 94 columns. Each row of the square matrix is called an "area", numbered from 01 to 94, and each column is called a "bit", numbered from 01 to 94, The area code and tag number of each Chinese character and symbol in the square array are combined to form four Arabic numerals, which are their "location code". The first two digits of the location code are its area code and the last two digits are its bit code. A Chinese character or symbol can be uniquely identified with a location code.

2.1.2 digital code (internal code)

        The internal code of Chinese characters refers to the code that represents a Chinese character in the computer. The area code and bit code of Chinese location code are between 1 ~ 94. If the location code is directly used as the internal code, it will be confused with the basic ASCII code. In order to avoid the conflict between the internal code and the basic ASCII code, it is necessary to avoid the control code (00H~1FH) in the basic ASCII code and distinguish it from the characters in the basic ASCII code. In order to achieve these two points, 20H can be added to the area code and bit code respectively, and 80h can be added on this basis (here "H" means that the first two digits are hexadecimal numbers). After these processes, it takes two bytes to represent a Chinese character with internal code, which are called high byte and low byte respectively. The internal code of these two bytes is represented according to the following rules:
High byte = area code + 20h + 80h (or area code + A0H)
Low byte = bit code + 20h + 80h (or bit code + AOH)
        Since the hexadecimal numbers in the value range of area code and bit code of Chinese characters are 01h ~ 5eh (i.e. 01 ~ 94 in decimal system), the value range of high-order byte and low-order byte of Chinese characters is a1h ~ FEH (i.e. 161 ~ 254 in decimal system).

2.1.3 lattice font structure

        In the dot matrix font of Chinese characters, each bit of each byte represents a point of a Chinese character. Each Chinese character is composed of a rectangular dot matrix, 0   Means no, 1   Represents a little, will   0   and   one   Draw with different colors to form a Chinese character. The commonly used dot matrix is   12*12, 14*14, 16*16   Three word libraries. The font can be divided into horizontal matrix and vertical matrix according to the different points represented by bytes.

2.1.4 acquisition of Chinese character dot matrix

        The Chinese character dot matrix font is stored according to the sequence of location codes. Therefore, we can obtain the dot matrix of a font according to location. Its calculation formula is as follows:

(location code)

Lattice start position  = ((   Area code  - 1)*94 + (   Bit code  – 1)) *   Number of Chinese character dot matrix bytes

(internal code)

Area code = high byte of internal code - A0H

Bit code = low byte of internal code - AOH

2.2 using C/C + + to call opencv to display pictures and text under Ubuntu

        Under Ubuntu, use C/C + + (or python) to call opencv library to program and display a picture, open a text file named "logo.txt" (there is only one line of text file, including your own name and student number), and read the font data of the corresponding characters in the Chinese character 24 * 24 dot matrix font library (file HZKf2424.hz in the compressed package) according to the name and student number,

Write text file: (select ANSI for encoding)

  Save text files and pictures to the virtual machine:

  Create a word.cpp file and enter the code:

#include<iostream>
#include<opencv/cv.h>
#include"opencv2/opencv.hpp"
#include<opencv/cxcore.h>
#include<opencv/highgui.h>
#include<math.h>

using namespace cv;
using namespace std;

void paint_chinese(Mat& image,int x_offset,int y_offset,unsigned long offset);
void paint_ascii(Mat& image,int x_offset,int y_offset,unsigned long offset);
void put_text_to_image(int x_offset,int y_offset,String image_path,char* logo_path);

int main(){
    String image_path="bo.jpg";//Name of picture
    char* logo_path="bo.txt";//Name of Chinese character file
    put_text_to_image(200,350,image_path,logo_path);//change txt place
    return 0;
}

void paint_ascii(Mat& image,int x_offset,int y_offset,unsigned long offset){
    //Coordinates of the starting point of the drawing
	Point p;
	p.x = x_offset;
	p.y = y_offset;
	 //Storing ascii word film
	char buff[16];           
	//Open ascii font file
	FILE *ASCII;

	if ((ASCII = fopen("Asci0816.zf", "rb")) == NULL){
		printf("Can't open ascii.zf,Please check the path!");
		//getch();
		exit(0);
	}

	fseek(ASCII, offset, SEEK_SET);
	fread(buff, 16, 1, ASCII);

	int i, j;
	Point p1 = p;
	for (i = 0; i<16; i++)                  //Sixteen char s
	{
		p.x = x_offset;
		for (j = 0; j < 8; j++)              //One char and eight bit s
		{
			p1 = p;
			if (buff[i] & (0x80 >> j))    /*Test whether the current bit is 1*/
			{
				/*
					Because the original ascii word film was 8 * 16, it was not large enough,
					So the original pixel is replaced by four pixels,
					After replacement, there are 16 * 32 pixels
					ps: I think it's unnecessary to write code like this, but I only think of this method for the time being
				*/
				circle(image, p1, 0, Scalar(0, 0, 255), -1);
				p1.x++;
				circle(image, p1, 0, Scalar(0, 0, 255), -1);
				p1.y++;
				circle(image, p1, 0, Scalar(0, 0, 255), -1);
				p1.x--;
			   circle(image, p1, 0, Scalar(0, 0, 255), -1);
			}						
            p.x+=2;            //One pixel becomes four, so x and y should both be + 2
		}
		p.y+=2;
	}
}
void paint_chinese(Mat& image,int x_offset,int y_offset,unsigned long offset){//Draw Chinese characters on the picture
    Point p;
    p.x=x_offset;
    p.y=y_offset;
    FILE *HZK;
    char buff[72];//72 bytes for storing Chinese characters

    if((HZK=fopen("HZKf2424.hz","rb"))==NULL){
        printf("Can't open HZKf2424.hz,Please check the path!");
        exit(0);//sign out
    }
    fseek(HZK, offset, SEEK_SET);/*Move the file pointer to the offset position*/
    fread(buff, 72, 1, HZK);/*Read 72 bytes from the offset position, and each Chinese character occupies 72 bytes*/
    bool mat[24][24];//Define a new matrix to store the transposed text film
    int i,j,k;
    for (i = 0; i<24; i++)                 /*24x24 Dot matrix Chinese characters, a total of 24 lines*/
	{
        	for (j = 0; j<3; j++)                /*There are 3 bytes in the horizontal direction, and the value of each byte is determined by cycle*/
			for (k = 0; k<8; k++)              /*Each byte has 8 bits, and the loop judges whether each byte is 1*/
				if (buff[i * 3 + j] & (0x80 >> k))    /*Test whether the current bit is 1*/
				{
					mat[j * 8 + k][i] = true;          /*1 is stored in a new word film*/
				}
				else {
					mat[j * 8 + k][i] = false;
				}
	}
	
    for (i = 0; i < 24; i++)
	{
		p.x = x_offset;
		for (j = 0; j < 24; j++)
		{		
			if (mat[i][j])
				circle(image, p, 1, Scalar(255, 0, 0), -1);		  //Write (replace) pixels
			p.x++;                                                //Shift right one pixel
		}
		p.y++;                                                    //Move down one pixel
	}
}

void put_text_to_image(int x_offset,int y_offset,String image_path,char* logo_path){//Put Chinese characters on the picture
//x and y are the starting coordinates of the first word on the picture
    //Get pictures through picture path
    Mat image=imread(image_path);
    int length=19;//The length of characters to be printed (the length can be as many bytes as you print, which can be adjusted according to your own situation)
    unsigned char qh,wh;//Define area code and tag number
    unsigned long offset;//Offset
    unsigned char hexcode[30];//Hexadecimal used to store Notepad reading. Remember to use unsigned
    FILE* file_logo;

    if ((file_logo = fopen(logo_path, "rb")) == NULL){
		printf("Can't open txtfile,Please check the path!");
		//getch();
		exit(0);
	}

    fseek(file_logo, 0, SEEK_SET);
    fread(hexcode, length, 1, file_logo);
    int x =x_offset,y = y_offset;//x. Y: the starting coordinate of the text drawn on the picture

    for(int m=0;m<length;){
        if(hexcode[m]==0x23){
            break;//It ends when the # number is read
        }
        else if(hexcode[m]>0xaf){
            qh=hexcode[m]-0xaf;//The font used starts with Chinese characters, not Chinese symbols
            wh=hexcode[m+1] - 0xa0;//Calculation bit code
            offset=(94*(qh-1)+(wh-1))*72L;
            paint_chinese(image,x,y,offset);
            /*
            Calculate the offset in the Chinese character library
            Each Chinese character is represented by a 24 * 24 dot matrix
            A line has three bytes, a total of 24 lines, so 72 bytes are required
            */

            m=m+2;//The internal code of a Chinese character occupies two bytes,
            x+=24;//A Chinese character has 24 * 24 pixels. Because it is placed horizontally, it moves 24 pixels to the right
        }

        else{
        //When the read character is ASCII
        wh=hexcode[m];
        offset=wh*16l;//Calculate the offset of English characters
        paint_ascii(image,x,y,offset);
        m++;//English characters only occupy one byte in the file, so just move back one bit
        x+=16;
        }

    }

    cv::imshow("image", image);
    cv::waitKey();
}

Save the word.cpp file, and then close the word.cpp file. Open the execution window with ctrl+alt+T and run:

g++ word.cpp -o word `pkg-config --cflags --libs opencv`

Run after no error is reported:

./word

Operation effect:

3, Summary

        If two computers transmit txt files through serial port communication, the speed is very fast and can be opened directly. However, if video and pictures are transmitted, the transmission time may be very long, and special methods are required to open them, and the opening may be disordered. Therefore, the larger the transmitted file is, the longer the transmission time is, and the baud rate also has an impact on the transmission. For files of the same size, When transmitting at a larger baud rate, the transmission time will be reduced, but this is only applicable to a certain range. In dot matrix Chinese character coding, it will be much easier as long as you learn the coding rules of Chinese character dot matrix, but you need to pay attention to the path problem when compiling the code. If the path is wrong, there may be errors all the time, indicating that the file cannot be found.

4, References

(57 messages) principle of Chinese character dot matrix font library_ Xiamen Yingzhi CSDN blog_ Chinese character lattice

[embedded 13] serial communication between two computers_ Poof poof jar blog - CSDN blog

Tags: C C++

Posted on Thu, 18 Nov 2021 11:44:40 -0500 by cjkeane