Ⅰ 單片機與pc機串口通信完整代碼
#include"reg51.h"
unsigned int rt=0,tt=0,dema,temp;
unsigned int re_buf[3]={0};初值
unsigned char send_buf[4]={'O','K','O','K'};//返回
unsigned char code leds_a[]={0xC0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //0-9共陽極
sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3; //數碼管選位
bit L11;
void rs232_S(void);//串口初始化
void disp(void);//數碼管顯示
void main(void)
{
rs232_S();
while(1)
{
disp();
if(L11)
{
tt=0;
TI=1;//直接觸發中斷
L11=0;
}
}
}
void rs232_S(void)
{
TMOD=0X21;//T0用於定時作延時用 方式1
TH1=0XE6;
TL1=0XE6;//波特率2400
TH0=0Xf8;
TL0=0X30; //晶振12M初值2mS
TR0=1;
ET0=1;
SCON=0X50;
PCON=0X80;//smod加倍
TI=0;
RI=0;
L11=0;
ET1=1;
TR1=1;
ES=1;
EA=1;
}
void RS232(void) interrupt 4
{
if(RI)
{
RI=0;
rt++;
if(rt<5)
{
re_buf[rt-1]=SBUF;
if(rt>=4)
{
L11=1;
rt=0;
}
}
}
else if(TI)
{
TI=0;
if(tt<4)
{
SBUF=send_buf[tt];
tt++;
}
}
}
void timer0(void) interrupt 1 //中斷進程
{
TR0=0;
TH0=0Xf8;
TL0=0X30; //重裝初值
TR0=1;
if(dema)
{
dema--; //2mS延時,延時採用定時中斷初值2mS
}
}
void disp(void)
{
P23=0;
temp=re_buf[3];
temp=temp&0x0f;
P0=leds_a[temp];
dema=3; //延時6mS
while(dema!=0);
P23=1;
P22=0;
temp=re_buf[2];
temp=temp&0x0f;
P0=leds_a[temp];
dema=3;
while(dema!=0);
P22=1;
P21=0;
temp=re_buf[1];
temp=temp&0x0f;
P0=leds_a[temp]; //串口發送10進制數 通過轉換二進制 保留低4位形成10進制數 選擇0-9
dema=3;
while(dema!=0);
P21=1;
P20=0;
temp=re_buf[0];
temp=temp&0x0f;
P0=leds_a[temp];
dema=3;
while(dema!=0);
P20=1;
}
用串口調試助手發送0000-9999四位數(十進制),單片機接收顯示在4位數碼管上並返回OKOK
這是我剛調試完成的,希望對你有幫助。
Ⅱ 各位大俠幫忙:求串口調試源碼,最好是C或VB的
是用VB調試精靈的源代碼改過來的,以16進制方式顯示發送:
Option Explicit
Dim intTime As Integer
Private strSendText As String '發送文本數據
Private bytSendByte() As Byte '發送二進制數據
Private blnReceiveFlag As Boolean
Private blnAutoSendFlag As Boolean
Private intPort As Integer
Private strSet As String
Private intReceiveLen As Integer
Private bytReceiveByte() As Byte
Private strAscii As String '設置初值
Private strHex As String
Private intHexWidth As Integer
Private intLine As Integer
Private m As Integer
Private strAddress As String
'字元表示的十六進制數轉化為相應的整數,錯誤則返回 -1
Function ConvertHexChr(str As String) As Integer
Dim test As Integer
test = Asc(str)
If test >= Asc("0") And test <= Asc("9") Then
test = test - Asc("0")
ElseIf test >= Asc("a") And test <= Asc("f") Then
test = test - Asc("a") + 10
ElseIf test >= Asc("A") And test <= Asc("F") Then
test = test - Asc("A") + 10
Else
test = -1 '出錯信息
End If
ConvertHexChr = test
End Function
'字元串表示的十六進制數據轉化為相應的位元組串,返回轉化後的位元組數
Function strHexToByteArray(strText As String, bytByte() As Byte) As Integer
Dim HexData As Integer '十六進制(二進制)數據位元組對應值
Dim hstr As String * 1 '高位字元
Dim lstr As String * 1 '低位字元
Dim HighHexData As Integer '高位數值
Dim LowHexData As Integer '低位數值
Dim HexDataLen As Integer '位元組數
Dim StringLen As Integer '字元串長度
Dim Account As Integer
Dim n As Integer
'計數
'txtSend = "" '設初值
HexDataLen = 0
strHexToByteArray = 0
StringLen = Len(strText)
Account = StringLen \ 2
ReDim bytByte(Account)
For n = 1 To StringLen
Do '清除空格
hstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While hstr = " "
Do
lstr = Mid(strText, n, 1)
n = n + 1
If (n - 1) > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
Loop While lstr = " "
n = n - 1
If n > StringLen Then
HexDataLen = HexDataLen - 1
Exit For
End If
HighHexData = ConvertHexChr(hstr)
LowHexData = ConvertHexChr(lstr)
If HighHexData = -1 Or LowHexData = -1 Then '遇到非法字元中斷轉化
HexDataLen = HexDataLen - 1
Exit For
Else
HexData = HighHexData * 16 + LowHexData
bytByte(HexDataLen) = HexData
HexDataLen = HexDataLen + 1
End If
Next n
If HexDataLen > 0 Then '修正最後一次循環改變的數值
HexDataLen = HexDataLen - 1
ReDim Preserve bytByte(HexDataLen)
Else
ReDim Preserve bytByte(0)
End If
If StringLen = 0 Then '如果是空串,則不會進入循環體
strHexToByteArray = 0
Else
strHexToByteArray = HexDataLen + 1
End If
End Function
Private Sub cmdManualSend_Click()
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Call ctrTimer_Timer
If Not blnAutoSendFlag Then
Me.MSComm.PortOpen = False
End If
End Sub
Private Sub cmdAutoSend_Click()
If blnAutoSendFlag Then
Me.ctrTimer.Enabled = False
If Not blnReceiveFlag Then
Me.MSComm.PortOpen = False
End If
Me.cmdAutoSend.Caption = "自動發送"
Else
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Me.ctrTimer.Interval = intTime
Me.ctrTimer.Enabled = True
Me.cmdAutoSend.Caption = "停止發送"
End If
blnAutoSendFlag = Not blnAutoSendFlag
End Sub
Private Sub ctrTimer_Timer()
Dim longth As Integer
strSendText = Me.txtSend.Text
longth = strHexToByteArray(strSendText, bytSendByte())
If longth > 0 Then
Me.MSComm.Output = bytSendByte
End If
End Sub
'輸入處理,處理接收到的位元組流,並保存在全局變數
Private Sub InputManage(bytInput() As Byte, intInputLenth As Integer)
Dim n As Integer '定義變數及初始化
ReDim Preserve bytReceiveByte(intReceiveLen + intInputLenth)
For n = 1 To intInputLenth Step 1
bytReceiveByte(intReceiveLen + n - 1) = bytInput(n - 1)
Next n
intReceiveLen = intReceiveLen + intInputLenth
End Sub
'為輸出准備文本,保存在全局變數
'總行數保存在intLine
Public Sub GetDisplayText()
Dim n As Integer
Dim intValue As Integer
Dim intHighHex As Integer
Dim intLowHex As Integer
Dim strSingleChr As String * 1
Dim intAddress As Integer
Dim intAddressArray(8) As Integer
Dim intHighAddress As Integer
strAscii = "" '設置初值
strHex = ""
strAddress = ""
'獲得16進制碼和ASCII碼的字元串
For n = 1 To intReceiveLen
intValue = bytReceiveByte(n - 1)
If intValue < 32 Or intValue > 128 Then '處理非法字元
strSingleChr = Chr(46) '對於不能顯示的ASCII碼,
Else '用"."表示
strSingleChr = Chr(intValue)
End If
strAscii = strAscii + strSingleChr
intHighHex = intValue \ 16
intLowHex = intValue - intHighHex * 16
If intHighHex < 10 Then
intHighHex = intHighHex + 48
Else
intHighHex = intHighHex + 55
End If
If intLowHex < 10 Then
intLowHex = intLowHex + 48
Else
intLowHex = intLowHex + 55
End If
strHex = strHex + Chr$(intHighHex) + Chr$(intLowHex) + " "
If (n Mod intHexWidth) = 0 Then
strAscii = strAscii + Chr$(13) + Chr$(10)
strHex = strHex + Chr$(13) + Chr$(10)
Else
End If
Next n
txtAsc = strAscii 'Ascii
txtHex = strHex '16進制
'獲得地址字元串
intLine = intReceiveLen \ intHexWidth
If (intReceiveLen - intHexWidth * intLine) > 0 Then
intLine = intLine + 1
End If
'設置換行
For n = 1 To intLine
intAddress = (n - 1) * intHexWidth
intHighAddress = 8
intAddressArray(0) = intAddress
For m = 1 To intHighAddress
intAddressArray(m) = intAddressArray(m - 1) \ 16
Next m
For m = 1 To intHighAddress
intAddressArray(m - 1) = intAddressArray(m - 1) - intAddressArray(m) * 16
Next m
For m = 1 To intHighAddress
If intAddressArray(intHighAddress - m) < 10 Then
intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("0")
Else
intAddressArray(intHighAddress - m) = intAddressArray(intHighAddress - m) + Asc("A") - 10
End If
strAddress = strAddress + Chr$(intAddressArray(intHighAddress - m))
Next m
strAddress = strAddress + Chr$(13) + Chr$(10)
Next n
txtAdd = strAddress '地址
End Sub
Private Sub cmdReceive_Click()
If blnReceiveFlag Then
If Not blnReceiveFlag Then
Me.MSComm.PortOpen = False
End If
Me.cmdReceive.Caption = "開始接收"
Else
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Me.MSComm.InputLen = 0
Me.MSComm.InputMode = 0
Me.MSComm.InBufferCount = 0
Me.MSComm.RThreshold = 1
Me.cmdReceive.Caption = "停止接收"
End If
blnReceiveFlag = Not blnReceiveFlag
End Sub
Private Sub Form_Load()
intHexWidth = 8
txtAdd = ""
txtHex = ""
txtAsc = ""
txtSend = "11"
txtAdd.Width = 1335
txtHex.Width = 2535
txtAsc.Width = 1215
'設置默認發送接收關閉狀態
blnAutoSendFlag = False
blnReceiveFlag = False
'接收初始化
intReceiveLen = 0
'默認發送方式為16進制
'intOutMode = 1
'初始化串列口
intPort = 1
intTime = 1000
strSet = "9600,n,8,1"
Me.MSComm.InBufferSize = 1024
Me.MSComm.OutBufferSize = 512
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
Me.MSComm.PortOpen = False
End Sub
Private Sub cmdClear_Click()
Dim bytTemp(0) As Byte
ReDim bytReceiveByte(0)
intReceiveLen = 0
Call InputManage(bytTemp, 0)
Call GetDisplayText
Call disPlay
End Sub
Private Sub MsComm_OnComm()
Dim bytInput() As Byte
Dim intInputLen As Integer
Select Case Me.MSComm.CommEvent
Case comEvReceive
If blnReceiveFlag Then
If Not Me.MSComm.PortOpen Then
Me.MSComm.CommPort = intPort
Me.MSComm.Settings = strSet
Me.MSComm.PortOpen = True
End If
'此處添加處理接收的代碼
Me.MSComm.InputMode = comInputModeBinary '二進制接收
intInputLen = Me.MSComm.InBufferCount
ReDim bytInput(intInputLen)
bytInput = Me.MSComm.Input
Call InputManage(bytInput, intInputLen)
Call GetDisplayText
'Call disPlay
If Not blnReceiveFlag Then
Me.MSComm.PortOpen = False
End If
End If
End Select
End Sub
Private Sub disPlay()
txtHex = ""
txtAsc = ""
txtAdd = ""
End Sub
Ⅲ 求串口調試助手的VC++源代碼
那個很簡單啊 自己寫一個也行 就是creatfile 然後把串口參數設好就行了