VB的代码段:
Dim av(7) As Byte
Dim bv As Variant
Dim temp(20) As Byte
'Dim rebuffer As Variant
'***********************************************发送的数据
Private Sub Command1_Click()
av(0) = &HE1
av(1) = &HE2
av(2) = Val("&H" & Text1.Text)
av(3) = &H88
av(4) = &H33
av(5) = &HAA
av(6) = &H44
av(7) = &HCC
For i = 0 To 7
buffer = buffer & "0x" & Hex(av(i)) & " "
Next
'Text1.Text = buffer
MSComm1.Output = av
Shape1.FillColor = vbRed
End Sub
'显示接收的数据
Private Sub Command2_Click()
'Text2.Text = rebuffer & "****"
End Sub
’********************************************串口通信部件设置
Private Sub Command3_Click()
Command3.Caption = "连接"
With MSComm1
If (.PortOpen = False) Then
.CommPort = Combo1.Text
End If
.Settings = "9600,N,8,1"
.InputLen = 1
.InputMode = comInputModeBinary
.InBufferCount = 0
.InBufferSize = 80
.OutBufferSize = 1
.OutBufferCount = 0
.RThreshold = 1
.SThreshold = 1
.PortOpen = True
End With
End Sub
Private Sub Form_Load()
Form1.WindowState = 2
Command1.Caption = "发送接收数据"
Command2.Caption = "没有就一直按↑"
Command3.Caption = "断开"
Shape1.FillColor = vbBlack
End Sub
'************************************************通信代码
Private Sub MSComm1_OnComm()
rebuffer = ""
With MSComm1
Select Case .CommEvent
Case comEvReceive
bv = .Input
temp(0) = bv(0)
If (temp(0) = &H1) Then
.RThreshold = 0
Do
DoEvents
Loop Until (.InBufferCount >= 2)
bv = .Input
temp(1) = bv(0)
If (temp(1) = &H2) Then
Do
DoEvents
Loop Until (.InBufferCount >= 6)
For w = 2 To 7
bv = .Input
temp(w) = bv(0)
Next
If (temp(7) = &HCC) Then
Shape1.FillColor = vbGreen
For w = 0 To 7
rebuffer = rebuffer & "0x" & Hex(temp(w)) & " "
Next
Text2.Text = rebuffer & "****"
End If
End If
'For i = 0 To 1
' rebuffer = "0x" & Hex(temp(0)) & "," & "0x" & Hex(temp(1))
MSComm1.RThreshold = 1
End If
MSComm1.InBufferCount = 0
Case Else
End Select
End With
End Sub
单片机的代码段:
#include <REG938.h>
#define UCHAR unsigned char
#define UINT unsigned int
//******************************************************************
#define FOSC_HZ 7372800//6250000//12000000// // 单片机工作时钟频率(Hz)
//******************************************************************
//串口
//******************************************************************
#define BAUDRATE 9600 // 波特率,bps
#ifdef BAUDRATE
#define BRG_V (FOSC_HZ / BAUDRATE - 16)
#define BRGR1_V (BRG_V / 0x100)
#define BRGR0_V (BRG_V % 0x100)
#endif
void UART_Init()
{
EA = 0;
SCON = 0x50; // 模式1,使能串行接收
SSTAT = 0x00; // 在停止位的开始产生TX中断,RX/TX中断独立
BRGCON = 0x02; // BRGEN清零,配置波特率
BRGR1 = BRGR1_V; // 设置波特率为9600bps,计算公式为:
BRGR0 = BRGR0_V; // BRGR1 * 256 + BRGR0 = 参考时钟 / 波特率 - 16
BRGCON = 0x03; // 选择内部BRG 为UART 波特率发生器
ESR = 1;
EST = 0;
EA = 1;
}
void IO_Init()
{
P0M1 = 0x00; // 定义P0口为仅为输入 //PxM1 PxM2 端口输出模式
P0M2 = 0x00; // 0 0 准双向口
P1M1 = 0x00; // 定义P1口为准双向口 // 0 1 推挽
P1M2 = 0x00; // 1 0 仅为输入(高阻)
P2M1 = 0x00; // 定义P2口为准双向口 // 0 1 推挽
P2M2 = 0x00;
}
void delay(UCHAR time)
{
UCHAR i;
while(time)
{ for(i=0;i<time;i++);
time--;
}
}
UCHAR recv[20],send[20],rtemp[20];
void fun(void) interrupt 4{
static UCHAR send_temp = 0x11;
static UCHAR recv_temp = 0x11;
UCHAR temp;
if (RI == 1){
switch (send_temp) {
case 0x11:
temp = SBUF;
if(temp = 0xE1){
send_temp = 0x22;
TI = 1 ;
}
else
send_temp = 0x11;
break;
case 0x22:
temp = SBUF;
if(temp = 0xE2){
send_temp = 0;
TI = 1;
}
else
send_temp = 0x11;
break;
default:
send[send_temp] = SBUF; //必须放到这,不能放下面
if(send_temp < 5) {
delay(20);
TI = 1;
send_temp++;
}
// else if(send_temp = 5){
// send[send_temp] = SBUF;
// TI = 1;
// break;
// }
else
send_temp = 0x11;
break;
}
RI = 0;
}
if(TI == 1){
switch (recv_temp){
case 0x11:
SBUF = 0x01;
recv_temp = 0x22;
break;
case 0x22:
SBUF = 0x02;
recv_temp = 0x0;
break;
default :
if (recv_temp <= 5){
delay(20);
SBUF = send[recv_temp];
recv_temp++;
}
else
recv_temp = 0x11;
break;
}
TI = 0;
}
}
void main(){
UART_Init();
IO_Init();
while(1){
}
}
vb操作界面
有时会收不到数据,需要多次点击发送。这个问题需要解决,单片机代码的问题!