【C#】Arduino에서 취득한 센서값을 시리얼 통신으로부터 CSV 출력한다
개요
제목대로
역시 모두 파이썬을 좋아하는 것 같고, C#에서 하고 있는 사람은 적었기 때문에 정보 공유
이번에는 Arduino의 센서 출력을 Windows 콘솔 응용 프로그램을 사용하여 CSV로 출력 할 때까지의 길입니다.
 환경
windows10
Visual Studio 2019
Arduino IDE
Arduino Pro mini
적합한 광 센서
 구현
 Arduino IDE 측
const int analogInPin = A0; 
int sensorValue = 0;   
void setup() {
  Serial.begin(9600);
}
void loop() {
  sensorValue = analogRead(analogInPin);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\n");
  delay(2);
}
 
시리얼 모니터 결과는 이런 느낌
이 윈도우 이름대로 이번 시리얼 포트는 CM3
도구 탭을 눌러 직렬 포트를 볼 수 있습니다.
나중에 bps는 9600임을 기억합니다.
 Visual Studio 측
먼저 이번에 필요한 패키지를 .NET에서 설치합니다.
 ①System.Text.Encoding.CodePages
CSV로 출력 할 때 Shift-Jis를 사용합니다.
그리고 안의 정 Shift-Jis는 적합하지 않으면 화나
Windows의 저주는 무서운
그리고 조사한 결과,
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
그리고 마법의 말을 치면 괜찮은 것 같습니다 (던지기)
1. VS 프로젝트 탭에서 NuGet 패키지 관리 선택
2. 찾아보기 탭의 검색 상자에 System.Text.Encoding.CodePages를 입력합니다.
3. 맨 위의 녀석을 설치, 내가 ver.4.7.1
 ②System.IO.Ports
내 System.IO.Ports는 구 버전이었던 것처럼 컴파일시에 SerialPort 형이 네임 스페이스에 없다고 분노했습니다.
최신 ver의 분은 필요 없다고 생각합니다만 나와 같은 증상이 나왔다면 인스톨 해 봐 주세요
1. VS 프로젝트 탭에서 NuGet 패키지 관리 선택
2. 찾아보기 탭의 검색 상자에 System.IO.Ports를 입력합니다.
3. 맨 위의 녀석을 설치, 내가 ver.4.7.0
 소스 코드
ArduinoSerial_DataToCSV.csusing System;
using System.IO.Ports;
using System.Text;
namespace ArduinoSerial_DataToCSV
{
    class Program
    {
        static void Main(string[] args)
        {
            //キー入力に使用
            var outChar = "";
            //現在時刻
            DateTime dt = DateTime.Now;
            String now_time = dt.ToString();
            now_time = now_time.Replace("/", ".").Replace(" ","_").Replace(":","-");
            String PATH = @"C:/Users/○○/~/"+now_time+"_SensorData.csv";
            //EncodeをShift-Jisに対応
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            //シリアルポート取得
            SerialPort sp = new SerialPort("COM3");
            //csv出力
            var sw = new System.IO.StreamWriter(PATH, false, System.Text.Encoding.GetEncoding("shift_jis"));
            //Arduino側のサンプリング周波数
            sp.BaudRate = 9600;
            sp.Open();
            while (true)
            {
                //センサ値1行分取得
                string data = sp.ReadLine();
                //csv出力
                sw.WriteLine(data);
                Console.WriteLine(data);
                //Q入力でプログラム終了
                if (Console.KeyAvailable)
                {
                    outChar = Console.ReadKey().Key.ToString();
                    if (outChar == "Q")
                    {
                        return;
                    }
                }
            }
        }
    }
}
 실행 결과
 
 
첫 번째 줄이 잘못되는 문제가 있지만 잘 모르겠습니다.
피곤해서 수정하면 다시
 참고 자료
Arduino 일본어 참조
 h tp // w w. 무사시 전파. 이 m/아 r즈이노/레 f/이여 x. php
.NET Core에서 Shift-JIS를 처리하는 방법
 htps : // bg. 히트진. jp/엔트리/2019/01/27/200055
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(【C#】Arduino에서 취득한 센서값을 시리얼 통신으로부터 CSV 출력한다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/miwazawa/items/6ae8c92b02333c004c7f
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
windows10
Visual Studio 2019
Arduino IDE
Arduino Pro mini
적합한 광 센서
구현
 Arduino IDE 측
const int analogInPin = A0; 
int sensorValue = 0;   
void setup() {
  Serial.begin(9600);
}
void loop() {
  sensorValue = analogRead(analogInPin);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\n");
  delay(2);
}
 
시리얼 모니터 결과는 이런 느낌
이 윈도우 이름대로 이번 시리얼 포트는 CM3
도구 탭을 눌러 직렬 포트를 볼 수 있습니다.
나중에 bps는 9600임을 기억합니다.
 Visual Studio 측
먼저 이번에 필요한 패키지를 .NET에서 설치합니다.
 ①System.Text.Encoding.CodePages
CSV로 출력 할 때 Shift-Jis를 사용합니다.
그리고 안의 정 Shift-Jis는 적합하지 않으면 화나
Windows의 저주는 무서운
그리고 조사한 결과,
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
그리고 마법의 말을 치면 괜찮은 것 같습니다 (던지기)
1. VS 프로젝트 탭에서 NuGet 패키지 관리 선택
2. 찾아보기 탭의 검색 상자에 System.Text.Encoding.CodePages를 입력합니다.
3. 맨 위의 녀석을 설치, 내가 ver.4.7.1
 ②System.IO.Ports
내 System.IO.Ports는 구 버전이었던 것처럼 컴파일시에 SerialPort 형이 네임 스페이스에 없다고 분노했습니다.
최신 ver의 분은 필요 없다고 생각합니다만 나와 같은 증상이 나왔다면 인스톨 해 봐 주세요
1. VS 프로젝트 탭에서 NuGet 패키지 관리 선택
2. 찾아보기 탭의 검색 상자에 System.IO.Ports를 입력합니다.
3. 맨 위의 녀석을 설치, 내가 ver.4.7.0
 소스 코드
ArduinoSerial_DataToCSV.csusing System;
using System.IO.Ports;
using System.Text;
namespace ArduinoSerial_DataToCSV
{
    class Program
    {
        static void Main(string[] args)
        {
            //キー入力に使用
            var outChar = "";
            //現在時刻
            DateTime dt = DateTime.Now;
            String now_time = dt.ToString();
            now_time = now_time.Replace("/", ".").Replace(" ","_").Replace(":","-");
            String PATH = @"C:/Users/○○/~/"+now_time+"_SensorData.csv";
            //EncodeをShift-Jisに対応
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            //シリアルポート取得
            SerialPort sp = new SerialPort("COM3");
            //csv出力
            var sw = new System.IO.StreamWriter(PATH, false, System.Text.Encoding.GetEncoding("shift_jis"));
            //Arduino側のサンプリング周波数
            sp.BaudRate = 9600;
            sp.Open();
            while (true)
            {
                //センサ値1行分取得
                string data = sp.ReadLine();
                //csv出力
                sw.WriteLine(data);
                Console.WriteLine(data);
                //Q入力でプログラム終了
                if (Console.KeyAvailable)
                {
                    outChar = Console.ReadKey().Key.ToString();
                    if (outChar == "Q")
                    {
                        return;
                    }
                }
            }
        }
    }
}
 실행 결과
 
 
첫 번째 줄이 잘못되는 문제가 있지만 잘 모르겠습니다.
피곤해서 수정하면 다시
 참고 자료
Arduino 일본어 참조
 h tp // w w. 무사시 전파. 이 m/아 r즈이노/레 f/이여 x. php
.NET Core에서 Shift-JIS를 처리하는 방법
 htps : // bg. 히트진. jp/엔트리/2019/01/27/200055
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(【C#】Arduino에서 취득한 센서값을 시리얼 통신으로부터 CSV 출력한다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/miwazawa/items/6ae8c92b02333c004c7f
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
const int analogInPin = A0; 
int sensorValue = 0;   
void setup() {
  Serial.begin(9600);
}
void loop() {
  sensorValue = analogRead(analogInPin);
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\n");
  delay(2);
}
using System;
using System.IO.Ports;
using System.Text;
namespace ArduinoSerial_DataToCSV
{
    class Program
    {
        static void Main(string[] args)
        {
            //キー入力に使用
            var outChar = "";
            //現在時刻
            DateTime dt = DateTime.Now;
            String now_time = dt.ToString();
            now_time = now_time.Replace("/", ".").Replace(" ","_").Replace(":","-");
            String PATH = @"C:/Users/○○/~/"+now_time+"_SensorData.csv";
            //EncodeをShift-Jisに対応
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            //シリアルポート取得
            SerialPort sp = new SerialPort("COM3");
            //csv出力
            var sw = new System.IO.StreamWriter(PATH, false, System.Text.Encoding.GetEncoding("shift_jis"));
            //Arduino側のサンプリング周波数
            sp.BaudRate = 9600;
            sp.Open();
            while (true)
            {
                //センサ値1行分取得
                string data = sp.ReadLine();
                //csv出力
                sw.WriteLine(data);
                Console.WriteLine(data);
                //Q入力でプログラム終了
                if (Console.KeyAvailable)
                {
                    outChar = Console.ReadKey().Key.ToString();
                    if (outChar == "Q")
                    {
                        return;
                    }
                }
            }
        }
    }
}


첫 번째 줄이 잘못되는 문제가 있지만 잘 모르겠습니다.
피곤해서 수정하면 다시
참고 자료
Arduino 일본어 참조
 h tp // w w. 무사시 전파. 이 m/아 r즈이노/레 f/이여 x. php
.NET Core에서 Shift-JIS를 처리하는 방법
 htps : // bg. 히트진. jp/엔트리/2019/01/27/200055
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(【C#】Arduino에서 취득한 센서값을 시리얼 통신으로부터 CSV 출력한다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/miwazawa/items/6ae8c92b02333c004c7f
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
Reference
이 문제에 관하여(【C#】Arduino에서 취득한 센서값을 시리얼 통신으로부터 CSV 출력한다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/miwazawa/items/6ae8c92b02333c004c7f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)