winform和STM32单片机串口数据收发通信程序,MODBUS RTU协议,端口1 波特率9600,8位,无检验,停止位1位
时间: 2024-05-14 11:11:59 浏览: 146
通过自定义串口通信协议实现STM32采集的图像发送到上位机查看
5星 · 资源好评率100%
以下是C# Winform和STM32单片机串口数据收发通信程序,使用MODBUS RTU协议,端口1 波特率9600,8位,无检验,停止位1位。
C# Winform程序:
```csharp
using System;
using System.IO.Ports;
using System.Windows.Forms;
namespace WinformSerialCommunication
{
public partial class Form1 : Form
{
private SerialPort _serialPort = new SerialPort();
public Form1()
{
InitializeComponent();
// 初始化串口
_serialPort.PortName = "COM1";
_serialPort.BaudRate = 9600;
_serialPort.DataBits = 8;
_serialPort.Parity = Parity.None;
_serialPort.StopBits = StopBits.One;
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
// 打开串口
try
{
_serialPort.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonSend_Click(object sender, EventArgs e)
{
// 发送数据
try
{
byte[] sendData = new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A };
_serialPort.Write(sendData, 0, sendData.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonReceive_Click(object sender, EventArgs e)
{
// 接收数据
try
{
byte[] receiveData = new byte[8];
_serialPort.Read(receiveData, 0, receiveData.Length);
textBoxReceive.Text = BitConverter.ToString(receiveData);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
```
STM32单片机程序:
```c
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "misc.h"
void NVIC_Configuration(void);
void USART_Configuration(void);
void delay(uint32_t nCount);
int main(void)
{
NVIC_Configuration();
USART_Configuration();
while (1)
{
}
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
// USART1_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void delay(uint32_t nCount)
{
for (; nCount != 0; nCount--);
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint16_t data = USART_ReceiveData(USART1);
USART_SendData(USART1, data);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
```
以上代码仅供参考,具体实现还需要根据实际情况进行修改和优化。
阅读全文