ARM7串口通讯程序设计
关键字:ARM 主机 计算机 通讯程序 UART 串口 接口 源码
1 引言
背景:期末课程设计,要求设计实现ARM板到主机的网络通信
意义和目的:借此课程设计,使得我们能够更好掌握网络通讯技术与网络编程技巧。
2 总体设计
2.1提供配置与器件
ARM7板一块,串口线一条,JATA线一条,电源线一条。PC端开发平台为C#.Net平台。
2.2 系统框架设计
大致是通过在ARM板的RS232接口接收计算机传过来的数据,然后再通过RS232回传对计算机,并在计算机上显示出来。另外,如果数据需要保存,可以保存到一个txt文本文档中。
主文件MAIN.C代码:
#include "option.h"
#include "def.h"
#include "44b.h"
#include "44blib.h"
void Main(void)
{
unsigned char aa;
char *string;
rSYSCFG=CACHECFG; // Using 8KB Cache//
Port_Init();
ChangePllValue( 70, 3, 1 ) ; //设置CPU频率为78M
Uart_Init(0,57600);
Led_Display(0x0);
Delay(10);
Uart_Select(0); //Select UART0
Uart_Printf("\nARM通讯程序设计");
Uart_Printf("\nPlease Press A String");
while(1){
Uart_GetString(string); //从串口读取字符串
Uart_SendString(string); //发送字符串
}
}
PC端C#.net开发主界面代码:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
namespace Frontwork_WindowsForms
{
public partial class Form1 : Form
{
static System.IO.Ports.SerialPort _serialPort;
public Form1()
{
InitializeComponent();
_serialPort = new System.IO.Ports.SerialPort("COM1");
_serialPort.Parity = Parity.None; //设置校验位
_serialPort.StopBits = StopBits.One; //设置停止位
_serialPort.BaudRate = 57600; //设置波特率
_serialPort.DataBits = 8; //设置数据位
_serialPort.Open();
}
private void button1_Click(object sender, EventArgs e)
{
Write();
}
public void Write()
{
try
{
string message = textBox2.Text;
textBox2.Text = "";
_serialPort.Write(message); //向串口发送字符串message
Thread.Sleep(10);
string message1 = _serialPort.ReadExisting(); //读取串口字符串
if (message1 != "")
textBox1.Text += message1 + "\r\n";
}
catch (TimeoutException)
{
}
}
private void button2_Click(object sender, EventArgs e)
{
Testread.hello(textBox1.Text); //保存数据到arm.txt
MessageBox.Show("保存成功");
}
}
}
数据保存功能代码
//文本文件操作:创建/读取/拷贝/删除
using System;
using System.IO;
class Testread
{
public static void hello(string aa)
{
//创建并写入(将覆盖已有文件)
using (StreamWriter sw = File.CreateText("arm.txt"))
{
sw.WriteLine(aa);
}
}
}
PC端项目源文件下载:
1230131716_43784b9c
ARM7源文件下载:
1230131716_6437cd36
实验报告文件下载:
1238048063_5704a02c
此项目开发人员piikee LALA XIXI对此项目保留所有权,请勿用于商业用途!