ChatGPT win下C语言串口通信

文章目录

    • 1.ChatGPT
    • 2.对ChatGPT进行代码封装

关键词:win环境 C语言 串口通信 ChatGPT

1.ChatGPT

- com<=com9可以用直接赋值 COMx
- com>com9需要  \\\\.\\COMx
重点//eg:COM1,COM2 ······,  COM10 use \\\\.\\COM10
ChatGPT:Write a serial communication using c in win
#include <windows.h>
#include <stdio.h>int main()
{HANDLE hSerial;DCB dcbSerialParams = {0};COMMTIMEOUTS timeouts = {0};char port[] = "COM1"; // specify the COM port you want to useDWORD bytes_written, bytes_read;char data[] = "Hello world!"; // data to sendchar incoming_data[256]; // buffer to store incoming dataint i;// open the serial porthSerial = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);if (hSerial == INVALID_HANDLE_VALUE) {printf("Error opening serial port.\n");return 1;}// set the serial port parametersdcbSerialParams.DCBlength = sizeof(dcbSerialParams);if (!GetCommState(hSerial, &dcbSerialParams)) {printf("Error getting serial port state.\n");CloseHandle(hSerial);return 1;}dcbSerialParams.BaudRate = CBR_9600;dcbSerialParams.ByteSize = 8;dcbSerialParams.StopBits = ONESTOPBIT;dcbSerialParams.Parity = NOPARITY;if (!SetCommState(hSerial, &dcbSerialParams)) {printf("Error setting serial port state.\n");CloseHandle(hSerial);return 1;}// set the timeoutstimeouts.ReadIntervalTimeout = 50;timeouts.ReadTotalTimeoutConstant = 50;timeouts.ReadTotalTimeoutMultiplier = 10;timeouts.WriteTotalTimeoutConstant = 50;timeouts.WriteTotalTimeoutMultiplier = 10;if (!SetCommTimeouts(hSerial, &timeouts)) {printf("Error setting timeouts.\n");CloseHandle(hSerial);return 1;}// send dataif (!WriteFile(hSerial, data, sizeof(data), &bytes_written, NULL)) {printf("Error writing to serial port.\n");CloseHandle(hSerial);return 1;}printf("Sent %d bytes: %s\n", bytes_written, data);// read incoming dataif (!ReadFile(hSerial, incoming_data, sizeof(incoming_data), &bytes_read, NULL)) {printf("Error reading from serial port.\n");CloseHandle(hSerial);return 1;}printf("Received %d bytes: ", bytes_read);for (i = 0; i < bytes_read; i++) {printf("%c", incoming_data[i]);}printf("\n");// close the serial portCloseHandle(hSerial);return 0;
}

2.对ChatGPT进行代码封装

main.c

#include <windows.h>
#include <stdio.h>#include "serial.h"int main()
{const char* com = "\\\\.\\COM10";char data[] = "maosql\r\nHello world!\r\n"; // data to sendwq_open_serial_port(com, CBR_115200);wq_serial_port_write(data, sizeof(data));wq_serial_port_read();wq_close_serial_port();
}
  • serial.h
#ifndef __SERIAL_H__
#define __SERIAL_H__HANDLE wq_open_serial_port(const char* com, int BaudRate);void wq_close_serial_port(void);HANDLE wq_serial_port_write(char* data, UINT32 date_len);HANDLE wq_serial_port_read(void);#endif
  • serial.c
#include <windows.h>
#include <stdio.h>HANDLE hSerial;
HANDLE comHandle = INVALID_HANDLE_VALUE;
DWORD bytes_written, bytes_read;
char incoming_data[256]; // buffer to store incoming data/** param com:       //eg:COM1,COM2 , >COM9 use \\\\.\\COMx* param BaudRate:  //Common value:CBR_9600、CBR_19200、CBR_38400、CBR_115200、CBR_230400、CBR_460800*/
HANDLE wq_open_serial_port(const char* com, int BaudRate)
{DCB dcbSerialParams = { 0 };COMMTIMEOUTS timeouts = { 0 };// open the serial porthSerial = CreateFile(com, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);if (hSerial == INVALID_HANDLE_VALUE) {printf("Error opening serial port.\n");return comHandle;}// get the serial port parametersdcbSerialParams.DCBlength = sizeof(dcbSerialParams);if (!GetCommState(hSerial, &dcbSerialParams)) {printf("Error getting serial port state.\n");CloseHandle(hSerial);return comHandle;}dcbSerialParams.BaudRate = BaudRate;dcbSerialParams.Parity = NOPARITY;dcbSerialParams.ByteSize = 8;dcbSerialParams.StopBits = ONESTOPBIT;// set the serial port parametersif (!SetCommState(hSerial, &dcbSerialParams)) {printf("Error setting serial port state.\n");CloseHandle(hSerial);return comHandle;}// set the timeoutstimeouts.ReadIntervalTimeout = 50;timeouts.ReadTotalTimeoutConstant = 50;timeouts.ReadTotalTimeoutMultiplier = 10;timeouts.WriteTotalTimeoutConstant = 50;timeouts.WriteTotalTimeoutMultiplier = 10;if (!SetCommTimeouts(hSerial, &timeouts)) {printf("Error setting timeouts.\n");CloseHandle(hSerial);return comHandle;}return 0;
}void wq_close_serial_port(void)
{// close the serial portCloseHandle(hSerial);
}HANDLE wq_serial_port_write(char* data, UINT32 date_len)
{// send dataif (!WriteFile(hSerial, data, date_len, &bytes_written, NULL)) {printf("Error writing to serial port.\n");CloseHandle(hSerial);return comHandle;}printf("Sent %d bytes: %s\n", bytes_written, data);return 0;
}HANDLE wq_serial_port_read(void)
{UINT32 i;// read incoming dataif (!ReadFile(hSerial, incoming_data, sizeof(incoming_data), &bytes_read, NULL)) {printf("Error reading from serial port.\n");CloseHandle(hSerial);return comHandle;}printf("Received %d bytes: ", bytes_read);for (i = 0; i < bytes_read; i++) {printf("%c", incoming_data[i]);}printf("\n");return 0;
}

本文链接:https://my.lmcjl.com/post/9243.html

展开阅读全文

4 评论

留下您的评论.