无法从串行端口读取字符串 [英] could not read a string from serialport

查看:141
本文介绍了无法从串行端口读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

资深会员
我的C#程序将字符串="123"作为密码发送给pic
pic16f883的工作是读取此字符串并确保其为"123"
我做不到
那是我的问题
1-我没有办法让C#程序通过串口发送号码
我只能发送字符串
2-在另一侧,pic16f883无法读取字符串,只能读取数字

以下是我的C#程序和pic程序的一部分
任何帮助请

//c#程序

hellow members
My c# program sent a string="123" as a password to pic
The pic16f883 job is to read this string and insure that it is "123"
I could not do that
Those are my problem
1- i guss there is no way to have the c# program sent a number throgh serialport
i can only sent string
2- in the other side the pic16f883 can not read a string,it can only read a number

the following is part of my c# program and pic program
any help please

//c# program

sp.WriteLine("123");





;pic program
list p=16f883	;,R=DEC  ;Use decimal system
#include <p16f883.inc>
s0	equ 20
t0 	equ 21
;
val	equ 22
.
.
.
	movf	RCREG,W		;flush receive data register buffer
	movf	RCREG,W		;flush receive data register buffer
	movf	RCREG,W		;flush receive data register buffer
	bsf		PORTC,4

	;Assume the Computer has has sent a string="123" through serialport
	
	movlw	.123				;Pic know the password = 123
	movwf	val
	call	Rci				;Dose Pc sent 123 ?

;
Rci	nop	
up	call	Rs
	movwf	PORTB
	goto	up
Rs  clrwdt
	btfss	PIR1,RCIF	;Bit5 check for receiced data
	goto	up
	movf	RCREG,W		;save received data in w
	SUBLW   val			;password from c# program
	btfss	STATUS,Z
	goto	up		;Z=0,continue waitting for number val from pc
	return 	;W-val=0  ,Z=1
	;
	END

推荐答案

1)使用.NET(无论是否为#C)向COM端口发送任何类型的数据绝对不是问题. ):
http://msdn.microsoft.com/en-us/library/system. io.ports.serialport.aspx [ ^ ],请参见Write成员.

2)如果某些东西可以读取数字(虽然是什么大小?),则表示它可以读取字符串.它取决于字符串编码.如果可以使用ASCII,则每个字符都编码为一个字节,并且所有字符串编码格式均为Unicode UTF; UTF88是面向字节的,UTF16将字符表示为一个16位字或一对(称为代理对),UTF32将字符表示为一个32位字.编写WritelLine("123")时,将发送5个16位字(行用字符13、10(LF,CR)表示),因为在CLI内存中字符串以UTF16LE编码表示.最有可能的是,这不是预期的结果.您可以始终使用System.Text.Encoding类的on序列化所需的字符串.请参阅:
http://msdn.microsoft.com/en-us/library/system.text. encoding.aspx [^ ].

—SA
1) It''s absolutely not a problem to send any kind of data to a COM port using .NET (#C or not):
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^], see Write members.

2) If something can read a number (what size, though?), it means it can read a string. It depends on string encoding though. If you can use ASCII, each character is encoded as one byte, and all string encoding formats are Unicode UTFs; UTF88 is byte-oriented, UTF16 presents a character as one 16-bit word or a pair of them (called surrogate pair), UTF32 presents a character as one 32-bit word. When you write WritelLine("123") you send 5 16-bit words (line is represented with the characters 13, 10 (LF, CR)), because in CLI memory strings are represented in UTF16LE encoding. Most likely, this is not what expected. You can always serialize a string as required using on of the System.Text.Encoding classes. Please see:
http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx[^].

—SA


这篇关于无法从串行端口读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆