您的位置:程序门 -> .net技术 -> asp.net



读串口信息的问题.


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


读串口信息的问题.[已结贴,结贴人:renren6250]
发表于:2007-06-06 15:31:58 楼主
有一个pos机通过一个数据线与一台pc机中的串口连接,pos机有一个功能可以把本机的一些信息写到串口里,我想用c#写一个客户端软件,用serialport   类里的readline()函数去接受信息,可是一直接受不到.我现在能知道的信息   波特率是57600,我还需要其他什么信息才能接受这个串口信息啊.
发表于:2007-06-06 15:36:101楼 得分:10
帮顶一下:
发表于:2007-06-06 15:44:212楼 得分:60
除了波特率一直外,再检查其它的参数,校验位之类的都要一直。
可以用read()方法试下,看能否调到。或者下载个通讯调试助手,来查看接收的数据。
发表于:2007-06-06 17:05:253楼 得分:0
谢谢楼上的兄弟,我现在用serialport1.readexisting()读出了一些数据,可是数据变成了   "~?}#}   }!d}   }\ "}6t~ "   十六进制显示的字符串,我怎么给它还原成我要的字符串啊.
发表于:2007-06-06 17:17:084楼 得分:10
lz我们行业似乎很接近,但我做b/s   没有通讯这块。帮你问问同事吧..
发表于:2007-06-06 17:18:495楼 得分:10
检查数据位,停止位,校验位的设置
首先确认打开串口是否成功,然后再接收数据
发表于:2007-06-06 17:19:456楼 得分:10
转换16进制数据是用动态库里的函数,这个没办法了...
估计只能根据你的pos机写自己的转换方法了
发表于:2007-06-06 17:24:287楼 得分:0
谢谢大家了,问题已经解决.
发表于:2007-06-06 17:29:198楼 得分:0
贴个串口读写类给你吧:
using   system;
using   system.runtime.interopservices;

namespace   busapp
{
///   <summary>
///  
///   </summary>
public   class   mycom
{
public   mycom()
{
//  
//   todo:   在此处添加构造函数逻辑
//
}
public   int   portnum;   //1,2,3,4
public   int   baudrate;   //1200,2400,4800,9600
public   byte   bytesize;   //8   bits
public   byte   parity;   //   0-4=no,odd,even,mark,space  
public   byte   stopbits;   //   0,1,2   =   1,   1.5,   2  
public   int   readtimeout;   //10
   
//comm   port   win32   file   handle
private   int   hcomm   =   -1;
   
public   bool   opened   =   false;
     
//win32   api   constants
private   const   uint   generic_read   =   0x80000000;
private   const   uint   generic_write   =   0x40000000;
private   const   int   open_existing   =   3;    
private   const   int   invalid_handle_value   =   -1;
   
[structlayout(layoutkind.sequential)]
private   struct   dcb  
{
//taken   from   c   struct   in   platform   sdk  
public   int   dcblength;                       //   sizeof(dcb)  
public   int   baudrate;                         //   current   baud   rate  
public   int   fbinary;                     //   binary   mode,   no   eof   check  
public   int   fparity;                     //   enable   parity   checking  
public   int   foutxctsflow;             //   cts   output   flow   control  
public   int   foutxdsrflow;             //   dsr   output   flow   control  
public   int   fdtrcontrol;               //   dtr   flow   control   type  
public   int   fdsrsensitivity;       //   dsr   sensitivity  
public   int   ftxcontinueonxoff;   //   xoff   continues   tx  
public   int   foutx;                     //   xon/xoff   out   flow   control  
public   int   finx;                       //   xon/xoff   in   flow   control  
public   int   ferrorchar;           //   enable   error   replacement  
public   int   fnull;                     //   enable   null   stripping  
public   int   frtscontrol;           //   rts   flow   control  
public   int   fabortonerror;       //   abort   on   error  
public   int   fdummy2;                 //   reserved  
public   ushort   wreserved;                     //   not   currently   used  
public   ushort   xonlim;                           //   transmit   xon   threshold  
public   ushort   xofflim;                         //   transmit   xoff   threshold  
public   byte   bytesize;                       //   number   of   bits/byte,   4-8  
public   byte   parity;                           //   0-4=no,odd,even,mark,space  
public   byte   stopbits;                       //   0,1,2   =   1,   1.5,   2  
public   char   xonchar;                         //   tx   and   rx   xon   character  
public   char   xoffchar;                       //   tx   and   rx   xoff   character  
public   char   errorchar;                     //   error   replacement   character  
public   char   eofchar;                         //   end   of   input   character  
public   char   evtchar;                         //   received   event   character  
public   ushort   wreserved1;                   //   reserved;   do   not   use  
}

[structlayout(layoutkind.sequential)]
private   struct   commtimeouts  
{    
public   int   readintervaltimeout;  
public   int   readtotaltimeoutmultiplier;  
public   int   readtotaltimeoutconstant;  
public   int   writetotaltimeoutmultiplier;  
public   int   writetotaltimeoutconstant;  
}    

[structlayout(layoutkind.sequential)]  
private   struct   overlapped  
{  
public   int     internal;  
public   int     internalhigh;  
public   int     offset;  
public   int     offsethigh;  
public   int   hevent;  
}    
   
[dllimport( "kernel32.dll ")]
private   static   extern   int   createfile(
string   lpfilename,                                                   //   file   name
uint   dwdesiredaccess,                                             //   access   mode
int   dwsharemode,                                                     //   share   mode
int   lpsecurityattributes,   //   sd
int   dwcreationdisposition,                                 //   how   to   create
int   dwflagsandattributes,                                   //   file   attributes
int   htemplatefile                                                 //   handle   to   template   file
);
[dllimport( "kernel32.dll ")]
private   static   extern   bool   getcommstate(
int   hfile,     //   handle   to   communications   device
ref   dcb   lpdcb         //   device-control   block
);  
[dllimport( "kernel32.dll ")]
private   static   extern   bool   buildcommdcb(
string   lpdef,     //   device-control   string
ref   dcb   lpdcb           //   device-control   block
);
[dllimport( "kernel32.dll ")]
private   static   extern   bool   setcommstate(
int   hfile,     //   handle   to   communications   device
ref   dcb   lpdcb         //   device-control   block
);
[dllimport( "kernel32.dll ")]
private   static   extern   bool   getcommtimeouts(
int   hfile,                                     //   handle   to   comm   device
ref   commtimeouts   lpcommtimeouts     //   time-out   values
);  
[dllimport( "kernel32.dll ")]  
private   static   extern   bool   setcommtimeouts(
int   hfile,                                     //   handle   to   comm   device
ref   commtimeouts   lpcommtimeouts     //   time-out   values
);
[dllimport( "kernel32.dll ")]
private   static   extern   bool   readfile(
int   hfile,                                 //   handle   to   file
byte[]   lpbuffer,                           //   data   buffer
int   nnumberofbytestoread,     //   number   of   bytes   to   read
ref   int   lpnumberofbytesread,   //   number   of   bytes   read
ref   overlapped   lpoverlapped         //   overlapped   buffer
);
[dllimport( "kernel32.dll ")]  
private   static   extern   bool   writefile(
int   hfile,                                         //   handle   to   file
byte[]   lpbuffer,                                 //   data   buffer
int   nnumberofbytestowrite,           //   number   of   bytes   to   write
ref   int   lpnumberofbyteswritten,     //   number   of   bytes   written
ref   overlapped   lpoverlapped                 //   overlapped   buffer
);
[dllimport( "kernel32.dll ")]
private   static   extern   bool   closehandle(
int   hobject       //   handle   to   object
);
   
public   void   open()  
{
     
dcb   dcbcommport   =   new   dcb();
commtimeouts   ctocommport   =   new   commtimeouts();  
     
     
//   open   the   comm   port.

       
hcomm   =   createfile( "com "   +   portnum   ,generic_read   ¦   generic_write,0,   0,open_existing,0,0);
   
//   if   the   port   cannot   be   opened,   bail   out.
if(hcomm   ==   invalid_handle_value)  
{
throw(new   applicationexception( "comm   port   can   not   be   opened "));
}
   
//   set   the   comm   timeouts.
     
getcommtimeouts(hcomm,ref   ctocommport);
ctocommport.readtotaltimeoutconstant   =   readtimeout;
ctocommport.readtotaltimeoutmultiplier   =   0;
ctocommport.writetotaltimeoutmultiplier   =   0;
ctocommport.writetotaltimeoutconstant   =   0;    
setcommtimeouts(hcomm,ref   ctocommport);
   
//   set   baud   rate,   parity,   word   size,   and   stop   bits.
//   there   are   other   ways   of   doing   setting   these   but   this   is   the   easiest.
//   if   you   want   to   later   add   code   for   other   baud   rates,   remember
//   that   the   argument   for   buildcommdcb   must   be   a   pointer   to   a   string.
//   also   note   that   buildcommdcb()   defaults   to   no   handshaking.
   
dcbcommport.dcblength   =   marshal.sizeof(dcbcommport);
getcommstate(hcomm,   ref   dcbcommport);
dcbcommport.baudrate=baudrate;
dcbcommport.parity=parity;
dcbcommport.bytesize=bytesize;
dcbcommport.stopbits=stopbits;
setcommstate(hcomm,   ref   dcbcommport);
       
opened   =   true;
       
}
   
public   void   close()  
{
if   (hcomm!=invalid_handle_value)  
{
closehandle(hcomm);
opened=false;
}
}
   
public   byte[]   read(int   numbytes)  
{
byte[]   bufbytes;
byte[]   outbytes;
bufbytes   =   new   byte[numbytes];
if   (hcomm!=invalid_handle_value)  
{
overlapped   ovlcommport   =   new   overlapped();
int   bytesread=0;
readfile(hcomm,bufbytes,numbytes,ref   bytesread,ref   ovlcommport);
outbytes   =   new   byte[bytesread];
array.copy(bufbytes,outbytes,bytesread);
}  
else  
{
throw(new   applicationexception( "comm   port   not   open "));
}
return   outbytes;
}
   
public   int   write(byte[]   writebytes)  
{
int   byteswritten   =   0;
if   (hcomm!=invalid_handle_value)  
{
overlapped   ovlcommport   =   new   overlapped();
writefile(hcomm,writebytes,writebytes.length,ref   byteswritten,ref   ovlcommport);
}
else  
{
throw(new   applicationexception( "comm   port   not   open "));
}    
return   byteswritten;
}
}
}




快速检索

最新资讯
热门点击