Party A and Party B adopt Mode 1 two-way serial communication

dbafj1314 注册会员
2023-02-25 21:23

Part of this answer quotes GPT, GPT_Pro to solve the problem better
Computer A and computer B use two-way serial communication for control.

First of all, K1 button of machine A controls LED1 on machine B and LED2 off, K2 button controls LED1 off machine B and LED2 point high, K3 button controls LED1 and LED2 on machine B, which can be realized by the following code:

#define K1_ON 0x01 
#define K1_OFF 0x02
#define K2_ON 0x03 
#define K2_OFF 0x04
#define K3_ON 0x05 
#define K3_OFF 0x06
uint8_t k1,k2,k3; 
uint8_t send_data; 
void setup(){ 
void loop(){ 
    k1 = digitalRead(K1); 
    k2 = digitalRead(K2); 
    k3 = digitalRead(K3); 

    if (k1 == HIGH) { //K1按键按下,向乙机发送K1_ON信号 
        send_data = K1_ON; 
    } else { //K1按键松开,向乙机发送K1_OFF信号 
        send_data = K1_OFF;  

    if (k2 == HIGH) { //K2按键按下,向乙机发送K2_ON信号 
        send_data = K2_ON;  
    } else { //K2按键松开,向乙机发送K2_OFF信号  
        send_data = K2_OFF;  

    if (k3 == HIGH) { //K3按键按下,向乙机发送K3_ON信号  
        send_data = K3_ON;  
    } else { //K3按键松开,向乙机发送K3_OFF信号  
        send_data = K3_OFF;  

    delay(200); //延时200ms  

//乙机代码 //定义控制字符 #define K1_ON 0x01 #define K1_OFF 0x02 #define K2_ON 0x03 #define K2_OFF 0x04 #define K3_ON 0x05 #define K3_OFF 0x06 //定义读取数据变量 uint8_t readData; void setup(){ //串行口初始化 Serial.begin(9600); //IO端口初始化 pinMode(LED1,OUTPUT); pinMode(LED2,OUTPUT); } void loop(){ //读取串行数据 if (Serial.available() > 0){ readData = Serial.read(); if (readData == K1_ON){ digitalWrite(LED1,HIGH); digitalWrite(LED2,LOW); } else if (readData == K1_OFF){ digitalWrite(LED1,LOW); digitalWrite(LED2,HIGH); } else if (readData == K3_ON){ digitalWrite(LED1,HIGH); digitalWrite(LED2,HIGH); } else if (readData == K3_OFF){ digitalWrite(LED1,LOW); digitalWrite(LED2,LOW); } } delay(200); //延时200ms }

Secondly: the K4 button of machine B can control the number of k4 button presses sent from the serial port to machine A, and it is displayed on the digital tube of machine A's PO port. The code is as follows: // A code // define control character #define K4 0x07 // define counter uint16_t count=0; // Define the number uint8_t number=0; void setup(){// Initialize Serial.begin(9600); // The digital disk initializes pinMode(DIGIT0, OUTPUT); pinMode(DIGIT1, OUTPUT); pinMode(DIGIT2, OUTPUT); pinMode(DIGIT3, OUTPUT); pinMode(PO0, OUTPUT); pinMode(PO1, OUTPUT); pinMode(PO2, OUTPUT); pinMode(PO3, OUTPUT); pinMode(PO4, OUTPUT); pinMode(PO5, OUTPUT); pinMode(PO6, OUTPUT); pinMode(PO7, OUTPUT); } void loop(){// Read the Serial data if(serial.available() > 0) { uint8_t readData = Serial.read(); if(readData == K4) { count++; } } if(count > = 10000) count = 0; number = count % 10; digitalWrite(DIGIT0, LOW); digitalWrite(DIGIT1, LOW); digitalWrite(DIGIT2, LOW); digitalWrite(DIGIT3, LOW); digitalWrite(PO0, number & 0x01 ); digitalWrite(PO1, number & 0x02 ); digitalWrite(PO2, number & 0x04 ); digitalWrite(PO3, number & 0x08 ); delay(5 ); number =(count / 10) % 10; digitalWrite(DIGIT0, HIGH ); digitalWrite(DIGIT1, LOW ); digitalWrite(DIGIT2, LOW ); digitalWrite(DIGIT3, LOW ); digitalWrite(PO4 , number & 0x01 ); digitalWrite
If the answer is helpful, please accept it.

About the Author

Question Info

Publish Time
2023-02-25 21:23
Update Time
2023-02-25 21:23

Related Question


Symfit CallableNumericalModel.with_dependencies中的奇怪连接映射


[Error]unrecognized command line option'-std=c++'


Android Studio创建项目时报错,如何解决?(开发工具-ide)

Android 12 TextClassifier超时

Android Studio:共享文件夹不再工作


Does angular datatables(npm Package) support for ionic components too. For ion-row and ion-col