欢迎来到一句话经典语录网
我要投稿 投诉建议
当前位置:一句话经典语录 > 口号 > 多串口上位机自动定位串口号

多串口上位机自动定位串口号

时间:2018-01-14 07:18

上位机是如何采集下位机发送的串口数据的

程序中发送数据语句是sbuf=a[p][q];,这数组是字符吗

二进制数

这决定单片机发送的数据格什么样的,是字符就是发送ascii码,否则发送的二进制数(即十六进制)。

上位机是用串口调试助手接收的,还是用超级终端接收的,用超级端...

上位机与下位机串口通信,串口工作方式1每次只能发1个字节的数据,如何发送多字节数据,具体如下

当往 写入1个八位数据时,启动发送操作,数据从端实现异步发送。

发送完一帧数据后自动置=1,请求中断。

要继续发送时,清零便可。

你可以自己写一个连续发送的函数:只要发送前9个数据后,清零就成。

或者写一个一次之功能发送一个字节的函数,然后利用循环调用前面的函数,实现10个数据的发送。

大多数情况都用后者吧。

上位机 串口编程,需要设置起始位吗

需要设置:波特率、位数、有无校验位及校验形式、停止位的位数。

--要设置起始位吗

设置起始位

有地方设置吗

如果能设置,那就设置吧。

单片机怎样通过串口发送数据给上位机,上位机是一个显控触摸屏

这可办哪,接收方收数据后返回一个确认代码就行了,至于什么表示确认代码,自己确定一个就好了,例如返回“OK”,或回车符0x0D。

对于发送方,发完数据,就等待对方返回确认代码,如果收到,说明对方已经收到数据,如果没收到确认码,就重新发一次,不过,这种情况极少,除非两者连接线有问题。

至等待时间,可以根据波特率和发送的数据长度估算一下。

上位机串口的数据时串口数据大量丢失;用的是qserialport怎么办

用qserialport是会出现这个问题,最好用vc 使用底层api CreateFile ReadFile WriteFile进行串口编程做成dll或ocx以供调用

求上位机串口程序代码

以下是根据“人民邮电出版社”的“VISUAL BASIC 串口通讯实例导航”一书的第一章代码修改用于发送和接收十六进制的数据流实用可运行代码.标准模块:Option ExplicitPublic fMainForm As frmMainPublic yibiao_weizhi(10) As IntegerPublic dizhi1 As String * 2Public main_i As IntegerPublic i As IntegerPublic j As IntegerPublic fasong_sj(10, 5) As StringPublic xh As IntegerPublic dizhi As IntegerPublic sj_bm(10, 5) As SinglePublic number As BytePublic setMingling(10) As String * 16Public alame(10) As String * 1Public record_jm(5) As SinglePublic a As DoublePublic PRINT_Cs(14) As StringPublic PRINT_WzCs(12) As StringPublic shiYAnH As StringPublic shiYAnTime As StringPublic shiyan_sj(4) As StringPublic print_fg As ByteOption ExplicitDim sum_zsDim xuhao_zs As String * 2Dim i As ByteDim j As ByteDim ccl(2) As String * 1Dim blL As String * 2Dim bl As String * 1Dim cclL(2) As String * 4Dim bl_dm As String * 4Dim zt_dm1 As String * 8Dim jieshou_sj As String * 6Dim sum As ByteDim sum1 As ByteDim xuhao As String * 2Dim fa0 As String * 2Dim HexStr1 As String * 20' 基本设置Private intPort As Integer '串行口号Private strSet As String '协议设置Private intTime As Integer '发送时间间隔'发送与接收标志Private blnAutoSendFlag As Boolean '发送标志Private blnAutoSendFlag1 As Boolean '发送标志Private blnReceiveFlag As Boolean '接收标志'发送模块Private intOutMode As Integer '发送模式Private strSendText As String '发送文本数据Private bytSendByte() As Byte '发送二进制数据'显示标志Private intHexChk As Integer '十六进制编码标志Private intAsciiChk As Integer 'ASCII码标志Private intAddressChk As Integer '地址标志Private intAdd48Chk As Integer '4\\\/8位地址标志'接收模块Private bytReceiveByte() As Byte '接收到的字节Private intReceiveLen As Integer '接收到的字节数Private strTestn As String'显示模块Private strAddress As String '地址信息Private strHex As String '十六进制编码Private strAscii As String 'ASCII码Private intHexWidth As Integer '显示列数'Private intOriginX As Long '横向原点(像素)Private intOriginY As Integer '纵向原点(行)Private intLine As Integer '总行数'Dim m As IntegerDim blnChakanFlag As Boolean'显示常量Private Const ChrWidth = 105 '单位宽度Private Const ChrHeight = 2 * ChrWidth '单位高度Private Const BorderWidth = 210 '预留边界Private Const LineMax = 16 '最大显示行数'输入处理'处理接收到的字节流,并保存在全局变量'bytReceiveRyte()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 + intInputLenthEnd Sub'为输出准备文本'保存在全局变量'strText'strHex'strAddress'总行数保存在intLinePrivate 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 Dim HexStr As String On Error GoTo abc 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 HexStr = HexStr & Chr$(intHighHex) & Chr$(intLowHex) HexStr1 = HexStr '传递数据 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 '获得地址字符串 intLine = intReceiveLen \\\\ intHexWidth If (intReceiveLen - intHexWidth * intLine) > 0 Then intLine = intLine + 1 End If For n = 1 To intLine intAddress = (n - 1) * intHexWidth If intAdd48Chk = 1 Then intHighAddress = 8 Else intHighAddress = 4 End If 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 'Text1 = Ok Exit Subabc:'Text1 = Error ResumeEnd Sub'显示输出Private Sub display() Dim intViewWidth As Long '横向宽度(像素) Dim intViewLine As Integer '纵向宽度(行) Dim strDisplayAddress As String Dim strDisplayHex As String Dim strDisplayAscii As String strDisplayAddress = strDisplayHex = strDisplayAscii = Dim intStart As Integer Dim intLenth As Integer '调整显示页面大小,设置滚动位置宽度 If intAdd48Chk = 1 Then frmMain.txtHexEditAddress.Width = 8 * ChrWidth + BorderWidth Else frmMain.txtHexEditAddress.Width = 4 * ChrWidth + BorderWidth End If frmMain.txtHexEditHex.Width = intHexWidth * 4 * ChrWidth + BorderWidth frmMain.txtHexEditText.Width = intHexWidth * ChrWidth + BorderWidth frmMain.txtBlank.Width = BorderWidth intViewWidth = frmMain.txtHexEditAddress.Width * intAddressChk + frmMain.txtHexEditHex.Width * intHexChk + frmMain.txtHexEditText.Width * intAsciiChk If intViewWidth <= frmMain.fraHexEditBackground.Width And intLine < LineMax Then frmMain.txtBlank.Width = frmMain.fraHexEditBackground.Width - intViewWidth frmMain.hsclHexEdit.Visible = False frmMain.vsclHexEdit.Visible = False intViewWidth = frmMain.fraHexEditBackground.Width intViewLine = intLine intOriginX = 0 intOriginY = 0 ElseIf intViewWidth > frmMain.fraHexEditBackground.Width And intLine < LineMax - 1 Then frmMain.hsclHexEdit.Visible = True frmMain.vsclHexEdit.Visible = False frmMain.hsclHexEdit.Width = frmMain.fraHexEditBackground.Width intViewLine = intLine intOriginY = 0 If intOriginX > intViewWidth - frmMain.fraHexEditBackground.Width Then intOriginX = intViewWidth - frmMain.fraHexEditBackground.Width End If ElseIf intViewWidth < (frmMain.fraHexEditBackground.Width - frmMain.vsclHexEdit.Width) And intLine >= LineMax Then frmMain.vsclHexEdit.Visible = True frmMain.hsclHexEdit.Visible = False frmMain.txtBlank.Width = frmMain.fraHexEditBackground.Width - intViewWidth intViewWidth = frmMain.fraHexEditBackground.Width intViewLine = LineMax intOriginX = 0 If intOriginY > intLine - LineMax Then intOriginY = intLine - LineMax End If Else frmMain.hsclHexEdit.Visible = True frmMain.vsclHexEdit.Visible = True frmMain.hsclHexEdit.Width = frmMain.fraHexEditBackground.Width - frmMain.vsclHexEdit.Width intViewLine = LineMax - 1 If intOriginX > intViewWidth - frmMain.fraHexEditBackground.Width Then intOriginX = intViewWidth - frmMain.fraHexEditBackground.Width End If If intOriginY > intLine - LineMax + 1 Then intOriginY = intLine - LineMax + 1 End If End If frmMain.txtHexEditAddress.Left = intOriginX frmMain.txtHexEditHex.Left = intOriginX + frmMain.txtHexEditAddress.Width * intAddressChk frmMain.txtHexEditText.Left = intOriginX + frmMain.txtHexEditAddress.Width * intAddressChk + frmMain.txtHexEditHex.Width * intHexChk frmMain.txtBlank.Left = intOriginX + frmMain.txtHexEditAddress.Width * intAddressChk + frmMain.txtHexEditHex.Width * intHexChk + frmMain.txtHexEditText.Width * intAsciiChk intStart = intOriginY * (6 + 4 * intAdd48Chk) + 1 intLenth = intViewLine * (6 + 4 * intAdd48Chk) strDisplayAddress = Mid(strAddress, intStart, intLenth) intStart = intOriginY * (intHexWidth * 4 + 2) + 1 intLenth = intViewLine * (intHexWidth * 4 + 2) strDisplayHex = Mid(strHex, intStart, intLenth) intStart = intOriginY * (intHexWidth + 2) + 1 intLenth = intViewLine * (intHexWidth + 2) strDisplayAscii = Mid(strAscii, intStart, intLenth) '设置滚动条 frmMain.vsclHexEdit.Max = intLine - intViewLine frmMain.hsclHexEdit.Max = (intViewWidth - frmMain.fraHexEditBackground.Width) \\\\ ChrWidth + 1 '显示输出 frmMain.txtHexEditHex.Text = strDisplayHex frmMain.txtHexEditText.Text = strDisplayAscii frmMain.txtHexEditAddress.Text = strDisplayAddressEnd Sub'文本无变化的刷新Private Sub ScrollRedisplay() Call displayEnd Sub'文本发生变化的刷新Private Sub SlideRedisplay() Call GetDisplayText Call displayEnd Sub'字符表示的十六进制数转化为相应的整数,错误则返回 -1Function 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 = testEnd 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 Long '计数 strTestn = '设初值 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 IfEnd FunctionPublic Function Hex_bin()'输出口状态鉴别For i = 1 To 2ccl(i) = Mid(blL, i, 1)If ccl(i) >= Chr(48) And ccl(i) <= Chr(57) Or ccl(i) >= Chr(65) And ccl(i) <= Chr(70) Then ccl(i) = ccl(i)Else Exit Function '退出过程函数ccl(i) = 0End IfNext iFor j = 1 To 2bl = ccl(j)If bl = F Thenbl_dm = 1111ElseIf bl = E Thenbl_dm = 1110ElseIf bl = D Thenbl_dm = 1101ElseIf bl = C Thenbl_dm = 1100ElseIf bl = B Thenbl_dm = 1011ElseIf bl = A Thenbl_dm = 1010ElseIf bl = 9 Thenbl_dm = 1001ElseIf bl = 8 Thenbl_dm = 1000ElseIf bl = 7 Thenbl_dm = 0111ElseIf bl = 6 Thenbl_dm = 0110ElseIf bl = 5 Thenbl_dm = 0101ElseIf bl = 4 Thenbl_dm = 0100ElseIf bl = 3 Thenbl_dm = 0011ElseIf bl = 2 Thenbl_dm = 0010ElseIf bl = 1 Thenbl_dm = 0001ElseIf bl = 0 Thenbl_dm = 0000Else:bl_dm = End IfcclL(j) = bl_dmNext jzt_dm1 = cclL(1) + cclL(2)For i = 1 To 8'zt_dm(i) = Mid$(zt_dm1, i, 1)Next iEnd FunctionPrivate Sub cboHexAscii_Click() If frmMain.cboHexAscii.Text = 按ASCII码 Then intOutMode = 0 Else intOutMode = 1 End IfEnd SubPrivate Sub chkAddress_Click() If chkAddress.Value = 0 Then intAddressChk = 0 Else intAddressChk = 1 End If Call ScrollRedisplayEnd SubPrivate Sub chkAddress48_Click() If chkAddress48.Value = 1 Then intAdd48Chk = 1 Else intAdd48Chk = 0 End If Call SlideRedisplayEnd SubPrivate Sub chkAscii_Click() If chkAscii.Value = 1 Then intAsciiChk = 1 Else intAsciiChk = 0 End If Call ScrollRedisplayEnd SubPrivate Sub chkHex_Click() If chkHex.Value = 0 Then intHexChk = 0 Else intHexChk = 1 End If Call ScrollRedisplayEnd SubPrivate Sub cmdAutoSend_Click() If blnAutoSendFlag Then frmMain.ctrTimer.Enabled = False If Not blnReceiveFlag Then frmMain.ctrMSComm.PortOpen = False End If frmMain.cmdAutoSend.Caption = 自动寻址 Else If Not frmMain.ctrMSComm.PortOpen Then frmMain.ctrMSComm.CommPort = intPort frmMain.ctrMSComm.Settings = strSet frmMain.ctrMSComm.PortOpen = True End If frmMain.ctrTimer.Interval = intTime frmMain.ctrTimer.Enabled = True frmMain.cmdAutoSend.Caption = 停止寻址 End If blnAutoSendFlag = Not blnAutoSendFlagEnd SubPrivate Sub cmdAutoSend1_Click()'用于设置参数 If blnAutoSendFlag1 Then Call cmdAutoSend_Click frmMain.ctrTimer1.Enabled = False frmMain.cmdAutoSend1.Caption = 自动设置 Else If Not frmMain.ctrMSComm.PortOpen Then frmMain.ctrMSComm.CommPort = intPort frmMain.ctrMSComm.Settings = strSet frmMain.ctrMSComm.PortOpen = True End If Call cmdAutoSend_Click frmMain.cmdAutoSend1.Caption = 停止设置 frmMain.ctrTimer1.Enabled = True End If blnAutoSendFlag1 = Not blnAutoSendFlag1End SubPrivate Sub cmdChakan_Click()If blnChakanFlag Then frmMain.cmdChakan.Caption = 查看 frmMain.Height = 2800Else frmMain.cmdChakan.Caption = 恢复 frmMain.Height = 6700End If blnChakanFlag = Not blnChakanFlagEnd SubPrivate Sub cmdClear_Click() Dim bytTemp(0) As Byte ReDim bytReceiveByte(0) intReceiveLen = 0 Call InputManage(bytTemp, 0) Call GetDisplayText Call displayEnd SubPrivate Sub cmdManualSend_Click() If Not frmMain.ctrMSComm.PortOpen Then frmMain.ctrMSComm.CommPort = intPort frmMain.ctrMSComm.Settings = strSet frmMain.ctrMSComm.PortOpen = True End If Call ctrTimer_Timer If Not blnAutoSendFlag Then frmMain.ctrMSComm.PortOpen = False End IfEnd SubPrivate Sub cmdReceive_Click() If blnReceiveFlag Then If Not blnAutoSendFlag And Not blnReceiveFlag Then frmMain.ctrMSComm.PortOpen = False End If frmMain.cmdReceive.Caption = 开始接收 Else If Not frmMain.ctrMSComm.PortOpen Then frmMain.ctrMSComm.CommPort = intPort frmMain.ctrMSComm.Settings = strSet frmMain.ctrMSComm.PortOpen = True End If frmMain.ctrMSComm.InputLen = 0 frmMain.ctrMSComm.InputMode = 0 frmMain.ctrMSComm.InBufferCount = 0 frmMain.ctrMSComm.RThreshold = 10 frmMain.cmdReceive.Caption = 停止接收 End If blnReceiveFlag = Not blnReceiveFlagEnd Sub因长度超10000字,请另行提问给于补充.

声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。联系xxxxxxxx.com

Copyright©2020 一句话经典语录 www.yiyyy.com 版权所有

友情链接

心理测试 图片大全 壁纸图片