
上位机与下位机串口通信,串口工作方式1每次只能发1个字节的数据,如何发送多字节数据,具体如下
当往 写入1个八位数据时,启动发送操作,数据从端实现异步发送。
发送完一帧数据后自动置=1,请求中断。
要继续发送时,清零便可。
你可以自己写一个连续发送的函数:只要发送前9个数据后,清零就成。
或者写一个一次之功能发送一个字节的函数,然后利用循环调用前面的函数,实现10个数据的发送。
。
。
大多数情况都用后者吧。
。
。
上位机是如何采集下位机发送的串口数据的
程序中发送数据语句是sbuf=a[p][q];,这数组是字符吗
二进制数
这决定单片机发送的数据格什么样的,是字符就是发送ascii码,否则发送的二进制数(即十六进制)。
上位机是用串口调试助手接收的,还是用超级终端接收的,用超级端...
谁能给我一个上位机控制下位机的串口通信程序(C语言编写吧)
这个是VC++ 的 只要你会建一个相应的变量和对话框空间就行啦 这个网站很清晰
求上位机串口程序代码
以下是根据“人民邮电出版社”的“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字,请另行提问给于补充.
PLC怎么与上位机进行通讯
【摘要】本文以三菱可编程控制器FX2N系列为例的PLC与上拉机的通信实现,提出了一种价格低、体积小的接口方案,讨论了其硬件和软件的设计思想,该接口装置与控制室和实验室环境下运行可靠。
【关键词】上位机,通信,接口,PLCPLC作为一种高性能的控制装置,在分布式系统中得到了越来越广泛的应用。
在这种控制方式中,上位监控机系统是其中重要的组成部分。
PLC可以多种方式如直接采用现有的组态监控软件与上位监迭机通信,但针对小规模的控制系统,找到一种高性能价格比的通信方法,具有积极的实际意义。
本文就日本三菱公司生产的FX2N可编程控制器与PC机通信方式的实现,从软、硬件两个方面来说明这个问题。
1.上位机与PLC通讯的原理在利用外部设备与可编程控制器之间进行通讯前,必须首先规定一些参数,如数据长度,奇偶校验、停止位以及波特率等,即通讯格式。
通讯格式是通过一个四位的16进制数而设定,这里设定为H6880,其含义是数据长度为7位,终结符为7位,奇偶校验为无,控制线为RS232接口,停止位为1位,和校验为自动添加,波特率为9600BPS,协议为专用协议,标题无,传输控制协议为协议格式1。
通讯格式是在PLC中首先将其设置好的。
其中协议格式的通讯格式依次规定了控制代码、站号、PC号、命令、消息等待以及和校验代码等。
在FX系列可编
串口自动接到0xFF
上电时RXD引脚是低电平,相当于检测到起始位了,接到的就是0xff。
不想接到的话,尝试延长上电复位延时,在初始化串口前加上几十毫秒的延时,初始化串口时先读一下串口接收寄存器,将接收寄存器清空。
在线等,两台机器分别串口通讯,用超级终端看到的数据不一样,一台为WN0001,一台为wn0001,请问这对上位机
是神化的部分,适宜并且具有原始态,小心风格源于此。
它们不同于理智的喀喀哒哒,操作的闪闪烁烁。
但神化不是主要人物的起源。
他来到
用电脑上位机通过串口调试助手向51单片机发送指令如果是1单片机选择模式一,然后在发送1或2 单片
下位机,串口的收发,设置好波特率,然后等待串口中断接收数据并处理接受代码是if(RI){RI=0;处理;}上位机,用串口控件,vc和vb比较简单,vb只是一个控件,加上处理函数,初始化,接收和发送vc的话mfc的串口,百度搜,mfc串口上上位机,就有教程,然后改下串口初始化,发送接收的就行了中间的数据是有时候需要按格式处理的,主要在于asm码表的转换



