VB与单片机的串行通信问题:怎么发送一个十六进制数据给单片机,比如0x4

来源:百度知道 编辑:UC知道 时间:2024/05/11 02:51:20
我曾定义过variant,byte型数据,但都显示无效的属性值。请问各位大哥是什么原因?是不是单片机串口初始化设置有问题?
static void serial0IntServer(void) interrupt 4 using 2
{
if(TI)
{
TI = FALSE;
if(uartTRPointer < uartTRLength)
{
SBUF = uartBuff[uartTRPointer];
uartTRPointer ++;
}
else uartTRPointer = 0;
}

void recCommDeal(void)
{
if(RI)
{
RI = FALSE;
uartRecSign = TRUE;
uartkeyBuff = SBUF;
uartOutTime = 4;

if (uartkeyBuff == 0x4)
RESETKEY = 0x4;
if (uartkeyBuff == 0x8)
SETKEY = 0x8;
if (uartkeyBuff == 0x10)
UPKEY = 0x10;
if (uartkeyBuff == 0x20)
DOWNKEY = 0x20;
}
}

请问你德代码是VB6还是VB.net的?
下面给出VB6的代码:
Private Sub Command1_Click()
Dim sj(3) As Byte
sj(0) = &H1
sj(1) = &H16
sj(2) = &HFF
sj(3) = &H3
MSComm1.Output = sj
End Sub

Private Sub Form_Load()
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
End Sub

...第四次回答同一个问题。。呵呵

....首先,串口所发的数据是有码制变化的。。分为2种情况,字符串和16进制发送。

两种方式的发送和接受方法是不同的。

先说16进制的

Dim sData() As Byte
Dim bData(1 To 6) As Byte
Dim strBuff As String

MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.RThreshold = 1
MSComm1.PortOpen = True

bData(1) = &HE0
bData(2) = &H0
bData(3) = &H3F
bData(4) = &H0
bData(5) = &H0
bData(6) = &H1F

MSComm1.InputMode = comInputModeBinary

必须把数据先存进一个字符数组中。

下面是接收的代码
Private Sub MSComm1_OnComm()
If MSComm1.CommEvent = 2 Then