虚拟串口及C#串口

虚拟串口软件使用:

http://developer.51cto.com/art/200908/146713.htm

 

VB串口通讯参考:

http://www.vbgood.com/viewthread.php?tid=14561

(4#)

 

http://baike.baidu.com/view/1649952.htm百科中的介绍,VB代码排版下:

代码
Private   Sub  cmdTestClick ( )                           ' 打开串口 
  MSComml.CommPort  = 2                                  ' 设定Com2 
   If  MSComml.PortOpen  =   False   Then  
        MSComm1.Settings 
=   " 9600,n,8,1 "                      ' 9600波特率,无校验,8位数据位,1位停止位 
        MSComm1.PortOpen  =   True                              ' 打开串口 
   End   if  
  MSComm1.OutBufferCount 
=   0                     ' 清空发送缓冲区 
  MSComm1.InBufferCount  =   0                      ' 滑空接收缓冲区 
   ' 发送字符数据时注意必须用回车符(vbcr)结束 
  MSComm1.Output = " This is a qood book !  "   & vbCr 
  
' 泼打电话号码或发送AT命令 
  MSComm1.Output  =   " ATDT 05778191898 , & vbCr 
   ' 发送字符数组数据时注意ByteArray必须事先定义赋值 

  
Dim  ByteArray  as   byte ( ) 
  
' 定义动态数组 
   ReDim  ByteArray( 1
  
' 重定义数组大小 
  ByteArray (  0  )  = 0  
  ByteArray ( 
1  )  =   1  
  MSComm1.Output 
=  ByteArray 
End Sub  

private   Sub  MScommEvent( ) 
  
Select   Case  MSComm1.CommEvent 
      
Case  comEvReceive 
            
Dim  Buffer  As  Variant 
            MSComm1.InputLen 
=   0  
           
' 接收二进制数据 
           MSComm1.InputMode =  ComInputModeBinary 
           Buffer
= MSComm1.Input 
            
' 接收字符数据 
           MSComm1.InputMode = comInputModeText 
          Buffer 
=  MSComml.Input 
      
Case   else  
  
End   Select  
End sub  

 

 VB代码

代码
' 一些参数说明

Private   Sub  Form_Load()            ' 程序加载时执行的函数
  MSComm1. InBufferCount  =   0           ' 设置读缓冲区为空
  MSComm1. OutBufferCount  =   0         ' 设置写缓冲区为空

  MSComm1. Settings 
=  “ 9600 ,E, 7 , 1 ”           ' 串口通讯参数,波特率:9600;校验:偶校验;数据位:7;停止位:1
  MSComm1. CommPort  =   1                       ' 通讯端口:com1
  MSComm1. PortOpen  =   True                    ' 端口打开
  cmdReadRequst. Enabled  =   False   
  cmdReadAnswer. Enabled 
=   False  
  cmdWriteRequst. Enabled 
=   False  
  cmdWriteAnswer. Enabled 
=   False  
End Sub  

Private   Sub  cmdTest_ Click()         
   Dim  instring  As   String                ' 声明一个字符串变量instring
  MSComm1.Output  =   Chr  ( 5 )             ' 串口输出ascii为5 的字符
  instring  =  MSComm1.  Input             ' 读取串口返回,赋值给instring
   If  instring  =   Chr  ( 6 Then           '如果返回值为 ascii = 6的字符,则
    
MsgBox  “连接成功!”                    
  
End   If  
End Sub  

 

发送大于128的数据,VB代码

 

代码
Dim  cc( 255 As   Byte  
  
For  i  =   0   To   255  
     cc(i) 
=  i 
  
Next  i 
  MSComm1.Output 
=  cc 
  
Do  
  DoEvents 
  
Loop   Until  MSComm1.OutBufferCount  =   0  
  
' 接收过程 MSComm1_OnComm() 
   Select   Case  MSComm1.CommEvent 
  
Case  comEvReceive 
     
Dim  Buffer  As  Variant, b1,i 
     MSComm1.InputMode
= comInputModeBinery 
     MSComm1.InputLen 
=   0  
     Buffer 
=  MSComm1.Input 
     
For  i = LBound  (Buffer)  To   UBound  (Buffer ) 
         Debug.Print Buffer ( i ) ; 
     
Next  i 
  
Case  . . . . . 

 

 字符串与字节的转换,C#代码

代码
             byte [] data  =  System.Text.Encoding.ASCII.GetBytes(textBox1.Text);   

            
string  sTest  =  System.Text.Encoding.ASCII.GetString(data);
            
// 一重载
            
// string sTest = System.Text.Encoding.ASCII.GetString(data, 0, data.Length); 

 

MSDN提供的一些字符串编码转换,C#

http://msdn.microsoft.com/zh-cn/library/system.text.encoding(VS.80).aspx

代码
using  System;
using  System.Text;

namespace  ConvertExample
{
   
class  ConvertExampleClass
   {
      
static   void  Main()
      {
         
string  unicodeString  =   " This string contains the unicode character Pi(\u03a0) " ;

         
//  Create two different encodings.
         Encoding ascii  =  Encoding.ASCII;
         Encoding unicode 
=  Encoding.Unicode;

         
//  Convert the string into a byte[].
          byte [] unicodeBytes  =  unicode.GetBytes(unicodeString);

         
//  Perform the conversion from one encoding to the other.
          byte [] asciiBytes  =  Encoding.Convert(unicode, ascii, unicodeBytes);
            
         
//  Convert the new byte[] into a char[] and then into a string.
         
//  This is a slightly different approach to converting to illustrate
         
//  the use of GetCharCount/GetChars.
          char [] asciiChars  =   new   char [ascii.GetCharCount(asciiBytes,  0 , asciiBytes.Length)];
         ascii.GetChars(asciiBytes, 
0 , asciiBytes.Length, asciiChars,  0 );
         
string  asciiString  =   new   string (asciiChars);

         
//  Display the strings created before and after the conversion.
         Console.WriteLine( " Original string: {0} " , unicodeString);
         Console.WriteLine(
" Ascii converted string: {0} " , asciiString);
      }
   }
}

 

 

 

你可能感兴趣的:(C#)