简体   繁体   English

将QBasic CHR $(13)+ CHR $(10)转换为C#

[英]Converting QBasic CHR$(13)+CHR$(10) to C#

I'm trying to pass a straight ASCII text command through my serial port, something like this: 我试图通过我的串口传递一个直接的ASCII文本命令,如下所示:

string cmd = "<ID00><PA>Hello World. ";
template.Serial.WriteLine(cmd);

Serial being a SerialPort property reference. Serial是SerialPort属性引用。 I've also tried 'Write(cmd)' but even though the serial port is open the command never seems to go through. 我也试过'Write(cmd)'但是即使串口是打开的,命令似乎永远不会通过。 I've found that I'm supposed to add a Carriage return (cr) and a Line Feed (lf) to the end of the message, but I don't know how to do this in C# short of converting everything to bytes but It needs to be passed as ASCII Text from my understanding of the protocol. 我发现我应该在消息的末尾添加一个回车符(cr)和一个换行符(lf),但我不知道如何在C#中将所有内容转换为字节但是它需要从我对协议的理解中作为ASCII文本传递。

I found someone's QBasic source that looks like this: 我发现某人的QBasic源代码如下:

100 OPEN "COM1:9600,N,8,1,CS,DS,CD" AS 1
200 PRINT #1,"<ID00>";:REM SIGN ADDRESS, 00 FOR ALL
210 PRINT #1,"<PA>";:REM PAGE "A" (MESSAGE NUMBER, A-Z)
220 PRINT #1,"<FQ>";:REM OPTIONAL DISPLAY MODE, (FA-FZ), "APPEAR"
230 PRINT #1,"<CB>";:REM OPTIONAL COLOR CHANGE, (CA-CZ), "RED"
240 PRINT #1,"Hello World";:REM TEXT
250 PRINT #1, CHR$(13)+CHR$(10);:REM MUST END IN CARRIAGE RETURN/LINE FEED

So how would you convert CHR$(13)+CHR$(10) to characters that you append to the end of a string line in c# code to be sent through a serial port? 那么如何将CHR $(13)+ CHR $(10)转换为附加到c#代码中字符串行末尾的字符,以便通过串口发送?

在字面上, CHR$(13)+CHR$(10)((char)13) + ((char)10) ,虽然为了易读,最好使用字符串"\\r\\n"

Append System.Environment.NewLine to your string. System.Environment.NewLine附加到您的字符串。 This is the equivalent of "\\r\\n" on Windows systems (but it will not work on Unix systems). 这相当于Windows系统上的“\\ r \\ n”(但它不适用于Unix系统)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM