美文网首页
[革命]来自人工智能的技术 c# 进入meta模式读取imei

[革命]来自人工智能的技术 c# 进入meta模式读取imei

作者: 吉凶以情迁 | 来源:发表于2023-02-16 09:57 被阅读0次
using System;
using System.IO.Ports;

class Program
{
    static void Main(string[] args)
    {
        // 创建串口对象
        SerialPort serialPort = new SerialPort();
        serialPort.PortName = "COM3";
        serialPort.BaudRate = 115200;
        serialPort.Parity = Parity.None;
        serialPort.DataBits = 8;
        serialPort.StopBits = StopBits.One;
        serialPort.Handshake = Handshake.None;

        // 打开串口
        serialPort.Open();

        // 进入 META 模式
        serialPort.WriteLine("AT+EDL");

        // 等待设备进入 META 模式
        while (true)
        {
            string line = serialPort.ReadLine().Trim();
            if (line == "META MODE")
            {
                break;
            }
        }

        // 发送 AT 命令读取 IMEI 号
        serialPort.WriteLine("AT+EGMR=1,7");

        // 读取设备返回的 IMEI 号
        while (true)
        {
            string line = serialPort.ReadLine().Trim();
            if (line.StartsWith("+EGMR"))
            {
                string imei = line.Split(':')[1].Trim();
                Console.WriteLine("IMEI: " + imei);
                break;
            }
        }

        // 关闭串口
        serialPort.Close();
    }
}

python

import serial

# 设置串口参数
ser = serial.Serial(port='COM3', baudrate=115200, bytesize=8, parity='N', stopbits=1)

# 进入 META 模式
ser.write(b'AT+EDL\r\n')

# 等待设备进入 META 模式
while True:
    line = ser.readline().decode().strip()
    if line == "META MODE":
        break

# 发送 AT 命令读取 IMEI 号
ser.write(b'AT+EGMR=1,7\r\n')

# 读取设备返回的 IMEI 号
while True:
    line = ser.readline().decode().strip()
    if line.startswith("+EGMR"):
        imei = line.split(':')[1].strip()
        print("IMEI: ", imei)
        break


开机状态AT读取

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>

int main()
{
    int fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
    if (fd < 0) {
        perror("open serial port failed");
        return -1;
    }

    struct termios opt;
    tcgetattr(fd, &opt);
    opt.c_cflag &= ~CSTOPB;
    opt.c_cflag &= ~CSIZE;
    opt.c_cflag |= CS8;
    opt.c_cflag |= CLOCAL | CREAD;
    opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    opt.c_iflag &= ~(INPCK | ISTRIP);
    opt.c_oflag &= ~OPOST;
    opt.c_cc[VTIME] = 0;
    opt.c_cc[VMIN] = 0;
    cfsetispeed(&opt, B115200);
    cfsetospeed(&opt, B115200);
    tcsetattr(fd, TCSANOW, &opt);

    // 发送 AT 命令
    write(fd, "AT+CGSN\r", strlen("AT+CGSN\r"));

    // 读取响应
    char buf[64] = {0};
    int len = read(fd, buf, sizeof(buf));
    if (len <= 0) {
        printf("read imei failed\n");
        return -1;
    }

    // 解析响应,获取 IMEI 号
    char *imei = strtok(buf, "\r\n");
    if (imei == NULL) {
        printf("parse imei failed\n");
        return -1;
    }

    printf("imei: %s\n", imei);

    close(fd);

    return 0;
}

java版

import java.io.*;
import java.util.*;
import javax.comm.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // 获取可用的串口列表
        Enumeration<CommPortIdentifier> portList = CommPortIdentifier.getPortIdentifiers();

        // 选择要使用的串口
        CommPortIdentifier portIdentifier = null;
        while (portList.hasMoreElements()) {
            CommPortIdentifier currPortIdentifier = portList.nextElement();
            if (currPortIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL && currPortIdentifier.getName().equals("COM3")) {
                portIdentifier = currPortIdentifier;
                break;
            }
        }

        if (portIdentifier == null) {
            System.out.println("串口不存在");
            return;
        }

        // 打开串口并设置参数
        SerialPort serialPort = (SerialPort) portIdentifier.open("SerialTest", 2000);
        serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

        // 获取串口输入输出流
        InputStream inputStream = serialPort.getInputStream();
        OutputStream outputStream = serialPort.getOutputStream();

        // 进入 META 模式
        outputStream.write("AT+EDL\r\n".getBytes());

        // 等待设备进入 META 模式
        byte[] buffer = new byte[1024];
        int len;
        while (true) {
            len = inputStream.read(buffer);
            String response = new String(buffer, 0, len).trim();
            if (response.equals("META MODE")) {
                break;
            }
        }

        // 发送 AT 命令读取 IMEI 号
        outputStream.write("AT+EGMR=1,7\r\n".getBytes());

        // 读取设备返回的 IMEI 号
        while (true) {
            len = inputStream.read(buffer);
            String response = new String(buffer, 0, len).trim();
            if (response.startsWith("+EGMR")) {
                String imei = response.split(":")[1].trim();
                System.out.println("IMEI: " + imei);
                break;
            }
        }

        // 关闭串口
        serialPort.close();
    }
}


dart

import 'dart:io';

void main(List<String> args) async {
  // 打开串口
  var serialPort = SerialPort('COM3', 115200);
  await serialPort.open();

  // 进入 META 模式
  var command = 'AT+EDL\r\n';
  await serialPort.write(Uint8List.fromList(command.codeUnits));
  await serialPort.flush();

  // 等待设备进入 META 模式
  var response = '';
  while (response != 'META MODE') {
    response = String.fromCharCodes(await serialPort.read(1024));
  }

  // 发送 AT 命令读取 IMEI 号
  command = 'AT+EGMR=1,7\r\n';
  await serialPort.write(Uint8List.fromList(command.codeUnits));
  await serialPort.flush();

  // 读取设备返回的 IMEI 号
  response = '';
  while (!response.startsWith('+EGMR')) {
    response += String.fromCharCodes(await serialPort.read(1024));
  }
  var imei = response.split(':')[1].trim();
  print('IMEI: $imei');

  // 关闭串口
  await serialPort.close();
}


相关文章

网友评论

      本文标题:[革命]来自人工智能的技术 c# 进入meta模式读取imei

      本文链接:https://www.haomeiwen.com/subject/ypkpkdtx.html