[good]串口读取

发布时间 2023-12-05 10:20:14作者: 风中狂笑
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <string.h>
#include <windows.h>
#include "serialport.h"

int main()
{
    // word byte
    WORD_BYTE wb;
    wb.word = 0x1234;
    printf("wb.word = %x\n", wb.word);
    wb.byte.high = 0x56;
    wb.byte.low = 0x78;
    printf("wb.byte.high = %x\n", wb.byte.high);
    printf("wb.byte.low = %x\n", wb.byte.low);
    print_bits(wb);
    // serial port
    HANDLE phSerial = NULL;
    char *port_name = "COM8";
    openPort(&phSerial, port_name);
    configPort(&phSerial);
    char *msg_send = "Hello, world!\n";

    for (int i = 0; i < 100; i++)
    {
        sendPort(&phSerial, msg_send);
    }
    // while (1)
    // {
    //     receivePort(phSerial);
    // }

    return 0;
}

 

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <string.h>
#include <stdint.h>
#include <windows.h>
#define BUF_SIZE 100
typedef union
{
    uint16_t word;
    struct
    {
        uint8_t low;
        uint8_t high;
    } byte;
    struct
    {
        uint8_t bit0 : 1;
        uint8_t bit1 : 1;
        uint8_t bit2 : 1;
        uint8_t bit3 : 1;
        uint8_t bit4 : 1;
        uint8_t bit5 : 1;
        uint8_t bit6 : 1;
        uint8_t bit7 : 1;
        uint8_t bit8 : 1;
        uint8_t bit9 : 1;
        uint8_t bit10 : 1;
        uint8_t bit11 : 1;
        uint8_t bit12 : 1;
        uint8_t bit13 : 1;
        uint8_t bit14 : 1;
        uint8_t bit15 : 1;
    } BIT;
} WORD_BYTE;

unsigned int crc_chk(unsigned char *data, unsigned char length)
{

    // from delta help
    int j;
    unsigned int reg_crc = 0xffff;

    while (length--)
    {
        reg_crc ^= *data++;
        for (j = 0; j < 8; j++)
        {
            if (reg_crc & 0x01)
                reg_crc = (reg_crc >> 1) ^ 0xa001;
            else
                reg_crc = reg_crc >> 1;
        }
    }

    return reg_crc;
}

void print_bits(WORD_BYTE wb)
{
    unsigned int bits[16] = {
        wb.BIT.bit15, wb.BIT.bit14, wb.BIT.bit13, wb.BIT.bit12,
        wb.BIT.bit11, wb.BIT.bit10, wb.BIT.bit9, wb.BIT.bit8,
        wb.BIT.bit7, wb.BIT.bit6, wb.BIT.bit5, wb.BIT.bit4,
        wb.BIT.bit3, wb.BIT.bit2, wb.BIT.bit1, wb.BIT.bit0};

    printf("wb.word.bit is: ");
    for (int i = 0; i < 16; i++)
    {
        printf("%x", bits[i]);
    }
    printf("\n");
}

void openPort(HANDLE *hport, const char *com)
{
    *hport = CreateFile(com,                          // 串口名称
                        GENERIC_READ | GENERIC_WRITE, // 可读、可写
                        0,                            // No Sharing
                        NULL,                         // No Security
                        OPEN_EXISTING,                // Open existing port only
                        FILE_ATTRIBUTE_NORMAL,        // Non Overlapped I/O
                        NULL);                        // Null for Comm Devices
    if (*hport == INVALID_HANDLE_VALUE)
    {
        fprintf(stderr, "Error opening serial port\n");
        perror("Error opening serial port");
    }
    else
    {
        printf("open serial port success!\n");
    }
}

void configPort(HANDLE *hport)
{
    // 配置串行端口
    DCB dcbSerialParams = {0};
    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    if (!GetCommState(*hport, &dcbSerialParams))
    {
        fprintf(stderr, "Error getting serial port state\n");
        perror("Error opening port");
    }
    dcbSerialParams.BaudRate = CBR_9600;   // 设置波特率
    dcbSerialParams.ByteSize = 8;          // 设置数据位
    dcbSerialParams.StopBits = ONESTOPBIT; // 设置停止位
    dcbSerialParams.Parity = NOPARITY;     // 设置奇偶校验位
    if (!SetCommState(*hport, &dcbSerialParams))
    {
        fprintf(stderr, "Error setting serial port state\n");
    }
    else
    {
        printf("config serial port success!\n");
    }
}

void sendPort(HANDLE *hport, const char *dataToSend)
{
    DWORD bytesWritten;
    int len = strlen(dataToSend);
    // 发送数据
    if (!WriteFile(*hport, dataToSend, len, &bytesWritten, NULL))
    {
        fprintf(stderr, "Error writing to serial port\n");
    }
    else
    {
        printf("send data success!\n");
    }
}

void receivePort(HANDLE *hport)
{
    // 读取返回的数据
    char buffer[BUF_SIZE];
    DWORD bytesRead;
    if (!ReadFile(*hport, buffer, sizeof(buffer), &bytesRead, NULL))
    {
        fprintf(stderr, "Error reading from serial port\n");
    }
    else
    {
        // 打印返回的数据
        printf("Received: %.*s\n", (int)bytesRead, buffer);
    }
}