หน้า: [1]   ลงล่าง
ผู้เขียน หัวข้อ: อยากทราบวิธีการตัดคำค่าที่อ่านมาจากพอร์ตอนุกรมครับ  (อ่าน 183 ครั้ง)
mmc01
Newbie
**

ความนิยม: +0/-0
กระทู้: 4


« เมื่อ: มกราคม 30, 2010, 06:21:10 AM »

ผมส่งค่าเข้าไปยังพอร์ตอนุกรมด้วยภาษาซีด้วยคำสั่งนี้ครับ
printf("T%3.2f,H%3.2f",fTemp_true,fRh_true); พอใช้โปรแกรม Hyperterminal รับค่าจะได้แบบนี้ครับ T29.11,H67.88 ซึ่งค่ามันอาจจะเป็นเลข 3 หลักเช่น T129.11,H100 ก็ได้ครับก็เลยใช้ substring ไม่ได้ ผมต้องการตัดเอาเฉพาะค่า 129.11 กับ 100 เพื่อนำไปแสดงที่ text box1 กับ text box2 ต้องทำยังไงครับ ใน c# ผมใช้ ReadExisting(); อ่านค่าจากพอร์ตอนุกรมครับ ถ้าใช้ ReadLine โปรแกรมมันจะค้างทุกทีเลยครับไม่รู้ทำไม แต่พอผมใช้ Hyperterminal ก็ไม่เห็นมีอะไรผิดปกติเลยครับ ผมใช้โปรแกรม Visual C# 2008 ครับ
แจ้งลบกระทู้นี้หรือติดต่อผู้ดูแล   บันทึกการเข้า

magicstudent
Administrator
Hero Member
*****

ความนิยม: +2/-0
กระทู้: 717


เว็บไซต์
« ตอบ #1 เมื่อ: มกราคม 30, 2010, 08:25:10 PM »

ก่อนตัด string ก็ทำการ check length มันก่อนก็ได้นี่ครับ ถ้าเอาง่ายๆก็ string.split(',') จะได้ array 2 ตัว
จากนั้นค่อยตัดจากหลังมาหน้าลบทิ้งไปหนึ่ง โดยใช้ fungtion Right ถึงใน C# จะไม่มีเขียนเอาก็ได้ แต่ผมหามาฝากแล้ว

จากเว็ป นี้ http://www.csharp411.com/c-string-tips/

static string Right( string s, int count )
{
    string newString = String.Empty;
    if (s != null && count > 0)
    {
        int startIndex = s.Length - count;
        if (startIndex > 0)
            newString = s.Substring( startIndex, count );
        else
            newString = s;
    }
    return newString;
}
แจ้งลบกระทู้นี้หรือติดต่อผู้ดูแล   บันทึกการเข้า

mmc01
Newbie
**

ความนิยม: +0/-0
กระทู้: 4


« ตอบ #2 เมื่อ: กุมภาพันธ์ 02, 2010, 05:58:18 AM »

ผมลองเปลี่ยนโค๊ดภาษาซีที่ไมโครคอนโทรลเลอร์ printf("%3.2f,%3.2f",fTemp_true,fRh_true); แล้วครับค่าที่ส่งมาจะได้ไม่มี T กับ H ข้างหน้า แต่พอรันแล้วโปรแกรมมันค้างครับผมลองเขียนแบบอื่นๆโปรแกรมมันก็ค้างตลอดเลยครับ ไม่ทราบว่าต้องแก้ยังไงครับ

โค๊ด:
namespace TestSerial
{
    public partial class Form1 : Form
    {
        private System.IO.Ports.SerialPort ComPort;
        public Form1()
        {
            InitializeComponent();
            try
            {
                this.ComPort = new System.IO.Ports.SerialPort("COM6", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                this.ComPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(ComPort_DataReceived);
                this.ComPort.Open();
            }
            catch (System.Exception e)
            {
                //Error Message e
                Console.WriteLine("Exception caught here" + e.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

string strData="";

private void ComPort_DataReceived(Object sender,System.IO.Ports.SerialDataReceivedEventArgs e)
{
   strData = this.ComPort.ReadExisting();
    this.Invoke(new EventHandler(Test));
}
private void Test(object sender,EventArgs e){
    string[] strOut = strData.Split(',');
    textBox1.Text = strOut[0];
    textBox2.Text = strOut[1];
}
โค๊ดนี้ตอนรันโปรแกรมแล้วมันค้างแล้วมีไดอะล็อกเขียนว่า Index was outside the bounds of the array. ครับ ผมลองเขียนอีกแบบนึงตามโค๊ดด้านล่างครับแต่ก็ยังค้างครับคนละที่กัน

โค๊ด:
namespace TestSerial
{
    public partial class Form1 : Form
    {
        private System.IO.Ports.SerialPort ComPort;
        public Form1()
        {
            InitializeComponent();
            try
            {
                this.ComPort = new System.IO.Ports.SerialPort("COM6", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                this.ComPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(ComPort_DataReceived);
                this.ComPort.Open();
            }
            catch (System.Exception e)
            {
                //Error Message e
                Console.WriteLine("Exception caught here" + e.ToString());
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void ComPort_DataReceived(Object sender, EventArgs e)
        {

            if (this.InvokeRequired == true)
            {
                try
                {
                    this.BeginInvoke(new EventHandler<EventArgs>(ComPort_DataReceived), new object[] { sender, e });

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.InnerException.Message);
                }
            }
            else
            {
                string strData = this.ComPort.ReadExisting();

                string[] strOut = strData.Split(',') ;
                textBox1.Text = strOut[0] ;
                textBox2.Text = strOut[1];
             }

โค๊ดนี้ตอนรันแล้วโปรแกรมมันจะค้างที่ไฟล์ Program.cs แล้วมีไดอะล็อกเขียนว่า Exception has been thrown by the target of an invocation. ครับ
แจ้งลบกระทู้นี้หรือติดต่อผู้ดูแล   บันทึกการเข้า

magicstudent
Administrator
Hero Member
*****

ความนิยม: +2/-0
กระทู้: 717


เว็บไซต์
« ตอบ #3 เมื่อ: กุมภาพันธ์ 02, 2010, 03:18:03 PM »

ต้องให้อ.สันติต์มาตอบให้นะครับ แต่เท่าที่ดูมีหลายส่วนยังไม่น่าจะถูกนะครับ โดยเฉพะการเปิด port ยังกำหนดค่าพารามิเตอร์ยังไม่น่าครอบคลุมและการปิด port(ก็ยังไม่เห็นมี) port ถ้าเปิดไปแล้ว ไปเปิดซ้ำมันน่าจะค้างครับ ก่อนอ.สันต์ต์มาลองดูจากตัวอย่างนี้ไปก่อนนะครับ

http://www.dreamincode.net/code/snippet2764.htm
แจ้งลบกระทู้นี้หรือติดต่อผู้ดูแล   บันทึกการเข้า

sunexpert
Administrator
Sr. Member
*****

ความนิยม: +0/-0
กระทู้: 301


เว็บไซต์
« ตอบ #4 เมื่อ: กุมภาพันธ์ 02, 2010, 10:54:41 PM »

อธิบายเรื่องนี้ยาวครับ เนื่องจากไม่ได้ใช้งานติดต่อกับ Hardware มานานแล้วครับเนื่องจาก Code ของ Delphi สั้นกว่าเยอะรวมถึงโปรแกรม ที่ได้จาก Delphi ก็ประมวลผลเร็วกว่า (มากๆ เพราะไม่ได้ผ่าน .net) แต่จะ Guide ให้เพื่อที่จะได้ไปค้นคว้าเพิ่มเติมได้

ท้าวความจากที่ดู String ของคำถามน่าจะเป็นการถอด code string ที่ได้จากการวัดความชื้น และ อุณหภูมิ โดยค่าที่ส่งมาจากอุปกรณ์ผ่าน RS232 มาที่คอมมักมี 2 แบบ
1. String + ASCII code อย่างเช่น ตย. ในนส Delphi for Engineering การถอดค่าอุณหภูมิ โดยค่าอุณภูมิที่ส่งมาเป็น ASCII ก็จะใช้การถอด Code แบบนึง

2. เป็น String ล้วนๆ (ถึงแม้ว่าเราจะเห็นเป็น ตัวเลขก็ตาม) ตัวอย่าง การถอดรหัสประเภทนี้จะคล้ากับ การถอดรหัสที่ได้จาก GPS ซึ่งสามารถดู ตย ได้จาก ใน หน้าเวป TDE เรา (มีถึง 3 ตอน)

เมื่อรู้หลักการแล้ว ไม่ว่าจะเขียนด้วย Delphi หรือ C# ก็สามารถถอด Code ออกมาได้ไม่ยากครับ (แต่ C# อาจเขียนยาวกว่านิด  ยิ้มกว้างๆ)
แจ้งลบกระทู้นี้หรือติดต่อผู้ดูแล   บันทึกการเข้า

หน้า: [1]   ขึ้นบน
พิมพ์
กระโดดไป: