Arduino Python3脚本

时间:2014-07-10 17:01:29

标签: python-3.x serial-port arduino pyserial

我试图使用Python3脚本来控制Arduino Mega。这是一个简单的脚本,可以从键盘上取一条线并通过Arduino回显它。我从http://petrimaki.wordpress.com/2013/04/28/reading-arduino-serial-ports-in-windows-7/开始使用Python 2脚本。我似乎无法收到我发回的字符,这可能是格式化问题。

这是格式化问题吗? unicode到ASCII问题? 如何使用Python 3和pySerial读/写二进制/十六进制数据和ASCII文本? 欢迎任何有关Python新手的建议。

Python 3脚本:

import serial
import time

ser = serial.Serial('COM8', 9600, timeout=0)
var = input("Enter something: ")
print(var)
ser.write(bytes(var.encode('ascii')))
while 1:
    try:
        print(ser.readline())
        time.sleep(1)
    except ser.SerialTimeoutException:
        print(('Data could not be read'))

Arduino代码:

int incomingByte=0;

void setup() {
  // Open serial connection.
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    // Read the incoming byte.
    incomingByte = Serial.read();

    // Echo what you got.
    Serial.print("I got: ");
    Serial.println(incomingByte);
  }
}

输入: 快速的红狐狸

输出:

b''
b'I got: 84\r\n'
b'I got: 104\r\n'
b'I got: 101\r\n'

等等。

1 个答案:

答案 0 :(得分:1)

@implementation LocationsViewController //******************************************************************************************************************************* - (void)viewDidLoad { [super viewDidLoad]; self.tableView.delegate = self; self.tableView.dataSource = self; } //******************************************************************************************************************************* - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } //******************************************************************************************************************************* -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 30.0f; } //******************************************************************************************************************************* #pragma mark - Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //******************************************************************************************************************************* - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { AppDelegate * delegate = [AppDelegate sharedInstance]; NSLog(@"%lu", (unsigned long)[delegate.locations count]); return [delegate.locations count]; } //******************************************************************************************************************************* - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //Cell setup AppDelegate * delegate = [AppDelegate sharedInstance]; static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } //Edit cell CLGeocoder *ceo = [[CLGeocoder alloc]init]; CLLocation * location = delegate.locations[indexPath.row]; [ceo reverseGeocodeLocation:location completionHandler:^(NSArray * placemarks, NSError *error) { CLPlacemark *placemark = [placemarks objectAtIndex:0]; //String to hold address cell.textLabel.text = [NSString stringWithFormat:@"%F, %F", location.coordinate.latitude, location.coordinate.longitude]; cell.detailTextLabel.text = [NSString stringWithFormat:@"%@, %@", placemark.locality, placemark.administrativeArea]; } ]; return cell; } 似乎没必要,只需使用- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { sharedInstance = self; _datahub = [[Datahub alloc] init]; _locations = [[NSMutableArray alloc] init]; _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; _locationsViewController = (LocationsViewController*) [storyboard instantiateViewControllerWithIdentifier:@"locationsViewController"]; _timePickViewController = (TimePickViewController*) [storyboard instantiateViewControllerWithIdentifier:@"timePickViewController"]; if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [_locationManager requestAlwaysAuthorization]; } [_locationManager startMonitoringSignificantLocationChanges]; return YES; } //******************************************************************************************************************************* - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { _currentLocation = _locationManager.location; [_locations addObject:_currentLocation]; [_locationsViewController.tableView reloadData]; } 方法或bytes(var.encode('ascii'))函数,两者都不需要。您还可以对收到的数据使用.encode()

例外bytes()raised on write timeouts,与阅读无关。

在Arduino代码中,尝试使用Serial.write()发回数据。